FantasyAsset/Assets/Scripts/Teleportation/TeleportScreenFader.cs

29 lines
706 B
C#

using System.Collections;
using UnityEngine;
[RequireComponent(typeof(CanvasGroup))]
public class TeleportScreenFader : MonoBehaviour
{
private CanvasGroup _cg;
private void Awake()
{
_cg = GetComponent<CanvasGroup>();
}
public IEnumerator FadeTo(float targetAlpha, float duration)
{
float start = _cg.alpha;
float t = 0f;
_cg.blocksRaycasts = targetAlpha > 0.01f;
while (t < duration)
{
t += Time.unscaledDeltaTime;
_cg.alpha = Mathf.Lerp(start, targetAlpha, t / duration);
yield return null;
}
_cg.alpha = targetAlpha;
_cg.blocksRaycasts = targetAlpha > 0.01f;
}
}