fix(fade-kit): 修复 FadeKit 空引用和零时长异常
- 增加 Image 空引用保护 - 处理 duration 小于等于 0 的边界情况 - 动画开始前调用 DOKill 避免冲突 - 使用 DOFade 替代 DOBlendableColor,行为更明确
This commit is contained in:
@@ -9,42 +9,74 @@ namespace AibisDream.Framework
|
||||
{
|
||||
public static IEnumerator FadeInAsync(this Image image, float duration, bool useUnscaledTime = false)
|
||||
{
|
||||
image.gameObject.SetActive(true);
|
||||
var tweener = image.DOBlendableColor(Color.white, duration);
|
||||
if (useUnscaledTime)
|
||||
var tweener = StartFade(image, 1f, duration, useUnscaledTime);
|
||||
if (tweener != null)
|
||||
{
|
||||
tweener.SetUpdate(true);
|
||||
yield return tweener.WaitForCompletion();
|
||||
}
|
||||
|
||||
yield return tweener.WaitForCompletion();
|
||||
}
|
||||
|
||||
public static void FadeIn(this Image image, float duration)
|
||||
{
|
||||
image.gameObject.SetActive(true);
|
||||
var tweener = image.DOBlendableColor(Color.white, duration);
|
||||
StartFade(image, 1f, duration, false);
|
||||
}
|
||||
|
||||
public static IEnumerator FadeOutAsync(this Image image, float duration, bool useUnscaledTime = false)
|
||||
{
|
||||
image.gameObject.SetActive(true);
|
||||
var tweener = image.DOBlendableColor(Color.clear, duration);
|
||||
if (useUnscaledTime)
|
||||
var completed = false;
|
||||
var tweener = StartFade(image, 0f, duration, useUnscaledTime);
|
||||
if (tweener == null)
|
||||
{
|
||||
tweener.SetUpdate(true);
|
||||
image?.gameObject.SetActive(false);
|
||||
yield break;
|
||||
}
|
||||
|
||||
tweener.OnComplete(() => completed = true);
|
||||
yield return tweener.WaitForCompletion();
|
||||
image.gameObject.SetActive(false);
|
||||
if (completed && image != null)
|
||||
{
|
||||
image.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
public static void FadeOut(this Image image, float duration)
|
||||
{
|
||||
image.gameObject.SetActive(true);
|
||||
image.DOBlendableColor(Color.clear, duration).onComplete += () =>
|
||||
var tweener = StartFade(image, 0f, duration, false);
|
||||
if (tweener == null)
|
||||
{
|
||||
image.gameObject.SetActive(false);
|
||||
};
|
||||
image?.gameObject.SetActive(false);
|
||||
return;
|
||||
}
|
||||
|
||||
tweener.OnComplete(() =>
|
||||
{
|
||||
if (image != null)
|
||||
{
|
||||
image.gameObject.SetActive(false);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static Tweener StartFade(Image image, float targetAlpha, float duration, bool useUnscaledTime)
|
||||
{
|
||||
if (image == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
image.DOKill(false);
|
||||
image.gameObject.SetActive(true);
|
||||
|
||||
duration = Mathf.Max(0f, duration);
|
||||
if (duration <= 0f)
|
||||
{
|
||||
var color = image.color;
|
||||
color.a = targetAlpha;
|
||||
image.color = color;
|
||||
return null;
|
||||
}
|
||||
|
||||
return image.DOFade(targetAlpha, duration).SetUpdate(useUnscaledTime);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user