Gameplay Ingredients是一组用于 Unity 游戏的运行时和编辑器工具:一组脚本的集合,可在制作游戏和原型时简化简单的任务。
您最多选择25个主题 主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

106 行
3.4 KiB

using System.Collections;
using System;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
namespace GameplayIngredients
{
[ManagerDefaultPrefab("FullScreenFadeManager")]
public class FullScreenFadeManager : Manager
{
public enum FadeMode
{
FromBlack = 0,
ToBlack = 1
}
public enum FadeTimingMode
{
UnscaledGameTime,
GameTime,
}
public Image FullScreenFadePlane;
private Coroutine m_Coroutine;
public void Fade(float duration, FadeMode mode, FadeTimingMode timingMode, Callable[] OnComplete, GameObject instigator = null)
{
if (m_Coroutine != null)
{
StopCoroutine(m_Coroutine);
m_Coroutine = null;
}
if (duration <= 0.0f)
{
var color = FullScreenFadePlane.color; ;
switch (mode)
{
case FadeMode.ToBlack:
color.a = 1.0f;
break;
case FadeMode.FromBlack:
color.a = 0.0f;
break;
default: throw new NotImplementedException();
}
FullScreenFadePlane.gameObject.SetActive(color.a == 1.0f);
FullScreenFadePlane.color = color;
Callable.Call(OnComplete, instigator);
}
else
{
switch (mode)
{
case FadeMode.ToBlack:
m_Coroutine = StartCoroutine(FadeCoroutine(duration, 1.0f, 1.0f, timingMode, OnComplete, instigator));
break;
case FadeMode.FromBlack:
m_Coroutine = StartCoroutine(FadeCoroutine(duration, 0.0f, -1.0f, timingMode, OnComplete, instigator));
break;
default: throw new NotImplementedException();
}
}
}
IEnumerator FadeCoroutine(float duration, float target, float sign, FadeTimingMode timingMode, Callable[] OnComplete, GameObject instigator)
{
FullScreenFadePlane.gameObject.SetActive(true);
Color c = FullScreenFadePlane.color;
while (sign > 0 ? FullScreenFadePlane.color.a <= target : FullScreenFadePlane.color.a >= target)
{
float t;
switch (timingMode)
{
case FadeTimingMode.GameTime:
t = Time.deltaTime;
break;
default:
case FadeTimingMode.UnscaledGameTime:
t = Time.unscaledDeltaTime;
break;
}
c = FullScreenFadePlane.color;
c.a += sign * t / duration;
FullScreenFadePlane.color = c;
yield return new WaitForEndOfFrame();
}
Color finalColor = FullScreenFadePlane.color;
finalColor.a = target;
FullScreenFadePlane.color = finalColor;
Callable.Call(OnComplete, instigator);
FullScreenFadePlane.gameObject.SetActive(target != 0.0f);
yield return new WaitForEndOfFrame();
m_Coroutine = null;
}
}
}