Skip to content

Instantly share code, notes, and snippets.

@beardordie
Created March 24, 2020 01:50
Show Gist options
  • Save beardordie/b459b081f121ed966e36adb8a7926681 to your computer and use it in GitHub Desktop.
Save beardordie/b459b081f121ed966e36adb8a7926681 to your computer and use it in GitHub Desktop.
Example from Unity's Gamekit2D to do fade to Black, Loading, and GameOver screens using multiple CanvasGroups, appearing on top and blocking raycasts beneath
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
namespace Gamekit2D
{
public class ScreenFader : MonoBehaviour
{
public enum FadeType
{
Black, Loading, GameOver,
}
public static ScreenFader Instance
{
get
{
if (s_Instance != null)
return s_Instance;
s_Instance = FindObjectOfType<ScreenFader> ();
if (s_Instance != null)
return s_Instance;
return s_Instance;
}
}
public static bool IsFading
{
get { return Instance.m_IsFading; }
}
protected static ScreenFader s_Instance;
public CanvasGroup faderCanvasGroup;
public CanvasGroup loadingCanvasGroup;
public CanvasGroup gameOverCanvasGroup;
public float fadeDuration = 1f;
protected bool m_IsFading;
const int k_MaxSortingLayer = 32767;
void Awake ()
{
if (Instance != this)
{
Destroy (gameObject);
return;
}
DontDestroyOnLoad (gameObject);
}
protected IEnumerator Fade(float finalAlpha, CanvasGroup canvasGroup)
{
m_IsFading = true;
canvasGroup.blocksRaycasts = true;
float fadeSpeed = Mathf.Abs(canvasGroup.alpha - finalAlpha) / fadeDuration;
while (!Mathf.Approximately(canvasGroup.alpha, finalAlpha))
{
canvasGroup.alpha = Mathf.MoveTowards(canvasGroup.alpha, finalAlpha,
fadeSpeed * Time.deltaTime);
yield return null;
}
canvasGroup.alpha = finalAlpha;
m_IsFading = false;
canvasGroup.blocksRaycasts = false;
}
public static void SetAlpha (float alpha)
{
Instance.faderCanvasGroup.alpha = alpha;
}
public static IEnumerator FadeSceneIn ()
{
CanvasGroup canvasGroup;
if (Instance.faderCanvasGroup.alpha > 0.1f)
canvasGroup = Instance.faderCanvasGroup;
else if (Instance.gameOverCanvasGroup.alpha > 0.1f)
canvasGroup = Instance.gameOverCanvasGroup;
else
canvasGroup = Instance.loadingCanvasGroup;
yield return Instance.StartCoroutine(Instance.Fade(0f, canvasGroup));
canvasGroup.gameObject.SetActive (false);
}
public static IEnumerator FadeSceneOut (FadeType fadeType = FadeType.Black)
{
CanvasGroup canvasGroup;
switch (fadeType)
{
case FadeType.Black:
canvasGroup = Instance.faderCanvasGroup;
break;
case FadeType.GameOver:
canvasGroup = Instance.gameOverCanvasGroup;
break;
default:
canvasGroup = Instance.loadingCanvasGroup;
break;
}
canvasGroup.gameObject.SetActive (true);
yield return Instance.StartCoroutine(Instance.Fade(1f, canvasGroup));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment