32 lines
675 B
C#
32 lines
675 B
C#
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
using System.Collections;
|
|
|
|
public class UIFader : MonoBehaviour
|
|
{
|
|
public CanvasGroup canvasGroup;
|
|
public float fadeSpeed = 5f;
|
|
|
|
public void FadeIn()
|
|
{
|
|
StopAllCoroutines();
|
|
StartCoroutine(Fade(1));
|
|
}
|
|
|
|
public void FadeOut()
|
|
{
|
|
StopAllCoroutines();
|
|
StartCoroutine(Fade(0));
|
|
}
|
|
|
|
IEnumerator Fade(float target)
|
|
{
|
|
while (Mathf.Abs(canvasGroup.alpha - target) > 0.01f)
|
|
{
|
|
canvasGroup.alpha = Mathf.Lerp(canvasGroup.alpha, target, Time.deltaTime * fadeSpeed);
|
|
yield return null;
|
|
}
|
|
|
|
canvasGroup.alpha = target;
|
|
}
|
|
} |