using System; using System.Collections; using UnityEngine; /// /// This class is used to control the fade in and fade out animations of a CanvasGroup. /// public class FadeCanvas : MonoBehaviour { // Current alpha value of the CanvasGroup private float _currentAlpha; // Event called when the Active Fade is completed. public Action OnCurrentFadeCompleted; /// /// Starts the fade in animation for the given CanvasGroup. /// /// The CanvasGroup to fade in. /// The duration of the fade in animation. public void StartFadeIn(CanvasGroup canvasGroup, float duration) { StopAllCoroutines(); StartCoroutine(FadeIn(canvasGroup, duration)); } /// /// Starts the fade out animation for the given CanvasGroup. /// /// The CanvasGroup to fade out. /// The duration of the fade out animation. public void StartFadeOut(CanvasGroup canvasGroup, float duration) { StopAllCoroutines(); StartCoroutine(FadeOut(canvasGroup, duration)); } /// /// Starts a sequence of fade in and fade out animations with a gap in between for the given CanvasGroup. /// /// The CanvasGroup to animate. /// The duration of the fade in animation. /// The duration of the fade out animation. /// The duration of the gap between the fade in and fade out animations. public void StartFadeInFadeOutWithGap(CanvasGroup canvasGroup, float fadeInDuration, float fadeOutDuration, float gapDuration) { StopAllCoroutines(); StartCoroutine(FadeInFadeOutWithGap(canvasGroup, fadeInDuration, fadeOutDuration, gapDuration)); } /// /// Starts a sequence of fade out and fade in animations with a gap in between for the given CanvasGroup. /// /// The CanvasGroup to animate. /// The duration of the fade in animation. /// The duration of the fade out animation. /// The duration of the gap between the fade out and fade in animations. public void StartFadeOutFadeInWithGap(CanvasGroup canvasGroup, float fadeInDuration, float fadeOutDuration, float gapDuration) { StopAllCoroutines(); StartCoroutine(FadeOutFadeInWithGap(canvasGroup, fadeInDuration, fadeOutDuration, gapDuration)); } /// /// Sets the alpha value of the given CanvasGroup. /// /// The CanvasGroup to set the alpha value for. /// The alpha value to set. private void SetAlpha(CanvasGroup canvasGroup, float value) { _currentAlpha = value; canvasGroup.alpha = _currentAlpha; } /// /// Coroutine for the fade in animation. /// /// The CanvasGroup to fade in. /// The duration of the fade in animation. private IEnumerator FadeIn(CanvasGroup canvasGroup, float duration) { float elapsedTime = 0.0f; // Gradually increase alpha from 0 to 1 while (_currentAlpha <= 1.0f) { SetAlpha(canvasGroup, elapsedTime / duration); elapsedTime += Time.deltaTime; yield return null; } OnCurrentFadeCompleted?.Invoke(); } /// /// Coroutine for the fade out animation. /// /// The CanvasGroup to fade out. /// The duration of the fade out animation. private IEnumerator FadeOut(CanvasGroup canvasGroup, float duration) { float elapsedTime = 0.0f; // Gradually decrease alpha from 1 to 0 while (_currentAlpha >= 0.0f) { SetAlpha(canvasGroup, 1 - elapsedTime / duration); elapsedTime += Time.deltaTime; yield return null; } OnCurrentFadeCompleted?.Invoke(); } /// /// Coroutine for a sequence of fade in and fade out animations with a gap in between. /// /// The CanvasGroup to animate. /// The duration of the fade in animation. /// The duration of the fade out animation. /// The duration of the gap between the fade in and fade out animations. private IEnumerator FadeInFadeOutWithGap(CanvasGroup canvasGroup, float fadeInDuration, float fadeOutDuration, float gapDuration) { float elapsedTime = 0.0f; // Gradually increase alpha from 0 to 1 while (_currentAlpha <= 1.0f) { SetAlpha(canvasGroup, elapsedTime / fadeInDuration); elapsedTime += Time.deltaTime; yield return null; } // Pause for a specified gap duration yield return new WaitForSeconds(gapDuration); elapsedTime = 0.0f; // Gradually decrease alpha from 1 to 0 while (_currentAlpha >= 0.0f) { SetAlpha(canvasGroup, 1 - elapsedTime / fadeOutDuration); elapsedTime += Time.deltaTime; yield return null; } OnCurrentFadeCompleted?.Invoke(); } /// /// Coroutine for a sequence of fade out and fade in animations with a gap in between. /// /// The CanvasGroup to animate. /// The duration of the fade in animation. /// The duration of the fade out animation. /// The duration of the gap between the fade out and fade in animations. private IEnumerator FadeOutFadeInWithGap(CanvasGroup canvasGroup, float fadeInDuration, float fadeOutDuration, float gapDuration) { float elapsedTime = 0.0f; // Gradually decrease alpha from 1 to 0 while (_currentAlpha >= 0.0f) { SetAlpha(canvasGroup, 1 - elapsedTime / fadeOutDuration); elapsedTime += Time.deltaTime; yield return null; } // Pause for a specified gap duration yield return new WaitForSeconds(gapDuration); elapsedTime = 0.0f; // Gradually increase alpha from 0 to 1 while (_currentAlpha <= 1.0f) { SetAlpha(canvasGroup, elapsedTime / fadeInDuration); elapsedTime += Time.deltaTime; yield return null; } OnCurrentFadeCompleted?.Invoke(); } }