fix(day2sleep): 修复大图虚焦清零后贴图变白
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,5 +1,4 @@
|
||||
using System.Collections;
|
||||
using AibisDream.Utility;
|
||||
using DG.Tweening;
|
||||
using UnityEngine;
|
||||
|
||||
@@ -24,6 +23,7 @@ namespace AibisDream
|
||||
private Coroutine _animationCoroutine;
|
||||
private Sequence _presentationSequence;
|
||||
private int _blurTweenVersion;
|
||||
private int _presentationTweenVersion;
|
||||
|
||||
public bool IsAnimationPlaying => _animationCoroutine != null;
|
||||
public string AnimationPrefix { get; private set; }
|
||||
@@ -131,6 +131,7 @@ namespace AibisDream
|
||||
public IEnumerator AnimatePresentation(float scaleMultiplier, float targetAlpha, float duration)
|
||||
{
|
||||
if (_renderer == null) yield break;
|
||||
var tweenVersion = ++_presentationTweenVersion;
|
||||
KillPresentationTweens();
|
||||
_renderer.gameObject.SetActive(true);
|
||||
TargetScaleMultiplier = Mathf.Max(0.01f, scaleMultiplier);
|
||||
@@ -139,6 +140,7 @@ namespace AibisDream
|
||||
var targetScale = ScaleFor(TargetScaleMultiplier);
|
||||
if (duration <= 0f)
|
||||
{
|
||||
if (tweenVersion != _presentationTweenVersion) yield break;
|
||||
ApplyPresentationImmediate(TargetScaleMultiplier, TargetAlpha);
|
||||
yield break;
|
||||
}
|
||||
@@ -151,12 +153,20 @@ namespace AibisDream
|
||||
_presentationSequence.Join(
|
||||
_renderer.DOFade(TargetAlpha, duration).SetEase(Ease.InOutSine));
|
||||
yield return _presentationSequence.WaitForCompletion();
|
||||
// 被 set_door_closeup / 新 tween 打断后,不要回写 scale。
|
||||
if (tweenVersion != _presentationTweenVersion) yield break;
|
||||
_presentationSequence = null;
|
||||
var transform = _renderer.transform;
|
||||
transform.localPosition = _baseLocalPosition;
|
||||
transform.localRotation = _baseLocalRotation;
|
||||
transform.localScale = targetScale;
|
||||
_renderer.SetAlpha(TargetAlpha);
|
||||
}
|
||||
|
||||
public void ApplyPresentationImmediate(float scaleMultiplier, float alpha)
|
||||
{
|
||||
if (_renderer == null) return;
|
||||
_presentationTweenVersion++;
|
||||
KillPresentationTweens();
|
||||
TargetScaleMultiplier = Mathf.Max(0.01f, scaleMultiplier);
|
||||
TargetAlpha = Mathf.Clamp01(alpha);
|
||||
@@ -169,18 +179,22 @@ namespace AibisDream
|
||||
|
||||
public void SetSpriteImmediate(Sprite sprite)
|
||||
{
|
||||
if (_renderer != null && sprite != null)
|
||||
_renderer.sprite = sprite;
|
||||
if (_renderer == null || sprite == null) return;
|
||||
// Null-assign forces URP SpriteRenderer to refresh _MainTex in its property block.
|
||||
_renderer.sprite = null;
|
||||
_renderer.sprite = sprite;
|
||||
}
|
||||
|
||||
public void StopPresentationAnimation()
|
||||
{
|
||||
_presentationTweenVersion++;
|
||||
KillPresentationTweens();
|
||||
}
|
||||
|
||||
public void ResetPresentationImmediate()
|
||||
{
|
||||
if (_renderer == null) return;
|
||||
_presentationTweenVersion++;
|
||||
KillPresentationTweens();
|
||||
TargetScaleMultiplier = 1f;
|
||||
TargetAlpha = _baseAlpha;
|
||||
@@ -255,8 +269,17 @@ namespace AibisDream
|
||||
amount = Mathf.Clamp01(amount);
|
||||
if (amount <= 0.001f)
|
||||
{
|
||||
_renderer.SetPropertyBlock(null);
|
||||
// SetPropertyBlock(null) can drop SpriteRenderer's internal _MainTex on URP.
|
||||
// Clear blur overrides, restore base material, then force a sprite rebind.
|
||||
_renderer.sharedMaterial = _baseMaterial;
|
||||
_propertyBlock ??= new MaterialPropertyBlock();
|
||||
_renderer.GetPropertyBlock(_propertyBlock);
|
||||
_propertyBlock.SetFloat(BlurAmountId, 0f);
|
||||
_propertyBlock.SetFloat(BlurSizeId, 0f);
|
||||
_propertyBlock.SetFloat(UseSoftFocusId, 0f);
|
||||
_renderer.SetPropertyBlock(_propertyBlock);
|
||||
_renderer.SetPropertyBlock(null);
|
||||
RebindSpriteTexture(_renderer);
|
||||
return;
|
||||
}
|
||||
if (_focusMaterial == null)
|
||||
@@ -270,9 +293,21 @@ namespace AibisDream
|
||||
_propertyBlock.SetFloat(BlurAmountId, amount);
|
||||
_propertyBlock.SetFloat(BlurSizeId, Mathf.Max(0f, blurSize));
|
||||
_propertyBlock.SetFloat(UseSoftFocusId, 1f);
|
||||
// Keep sprite texture in the block so URP SpriteRenderer does not fall back to white.
|
||||
if (_renderer.sprite != null && _renderer.sprite.texture != null)
|
||||
_propertyBlock.SetTexture("_MainTex", _renderer.sprite.texture);
|
||||
_renderer.SetPropertyBlock(_propertyBlock);
|
||||
}
|
||||
|
||||
private static void RebindSpriteTexture(SpriteRenderer renderer)
|
||||
{
|
||||
if (renderer == null) return;
|
||||
var sprite = renderer.sprite;
|
||||
if (sprite == null) return;
|
||||
renderer.sprite = null;
|
||||
renderer.sprite = sprite;
|
||||
}
|
||||
|
||||
private Vector3 ScaleFor(float multiplier)
|
||||
{
|
||||
return new Vector3(
|
||||
|
||||
@@ -126,6 +126,12 @@ namespace AibisDream
|
||||
yield break;
|
||||
}
|
||||
|
||||
// Force _MainTex rebind: assigning the same Sprite after SetPropertyBlock(null)
|
||||
// can leave URP Sprite-Lit with a null MainTex (pure white).
|
||||
var assigned = _largeSpriteRenderer.sprite;
|
||||
_largeSpriteRenderer.sprite = null;
|
||||
_largeSpriteRenderer.sprite = assigned;
|
||||
|
||||
_currentLargePicName = NormalizeDreamPicName(picName);
|
||||
if (CommonUtil.IsZero(duration))
|
||||
_largeSpriteRenderer.SetAlpha(1f);
|
||||
|
||||
Reference in New Issue
Block a user