Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class ScreenFader : MonoBehaviour {
- static ScreenFader instance;
- static CanvasGroup group;
- public int sortOrder = 99;
- public float speed = 3f;
- public Color fadeColour = Color.black;
- static Coroutine fadeOutCR;
- static Coroutine fadeInCR;
- void Awake()
- {
- if(instance != null)
- {
- DestroyImmediate(gameObject);
- return;
- }
- instance = this;
- // Create the canvas and set it to screen space overlay mode
- // --------------------------------------------------------------------------------
- var canvas = new GameObject("Canvas", typeof(Canvas)).GetComponent<Canvas>();
- canvas.transform.SetParent(transform, false);
- canvas.renderMode = RenderMode.ScreenSpaceOverlay;
- canvas.sortingOrder = sortOrder;
- // Add a CanvasGroup component so we can make it non-blocking and control the alpha
- // -----------------------------------------------------------------------------------------------------------------------
- group = canvas.gameObject.AddComponent<CanvasGroup>();
- group.alpha = 0f;
- group.interactable = false;
- group.blocksRaycasts = false;
- group.gameObject.SetActive(false);
- // Add an image, set it's colour and scale it to ensure there is no border
- // ------------------------------------------------------------------------------------------------
- var image = new GameObject("image", typeof(Image)).GetComponent<Image>();
- image.transform.SetParent(canvas.transform, false);
- image.color = fadeColour;
- image.transform.localScale = new Vector2(1.25f, 1.25f);
- // Set the image to be centred and fill the screen, and set anchors to stretch the image if screen size changes
- // ---------------------------------------------------------------------------------------------------------------------------------------------------
- var imageRect = image.GetComponent<RectTransform>();
- imageRect.anchorMin = Vector2.zero;
- imageRect.anchorMax = Vector2.one;
- imageRect.anchoredPosition = Vector2.zero;
- imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,Screen.width);
- imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical,Screen.height);
- }
- public static void FadeOut(float delayBefore = 0, float delayAfter = 0, System.Action callback = null)
- {
- if (callback == null) callback = DefaultCallback;
- group.gameObject.SetActive(true);
- StopActiveFades();
- fadeOutCR = instance.StartCoroutine(FadeOutCoRoutine(delayBefore, delayAfter,callback));
- }
- public static void FadeIn(float delayBefore = 0, float delayAfter = 0, System.Action callback = null)
- {
- if (callback == null) callback = DefaultCallback;
- group.gameObject.SetActive(true);
- StopActiveFades();
- fadeInCR = instance.StartCoroutine(instance.FadeInCoRoutine(delayBefore, delayAfter,callback));
- }
- static IEnumerator FadeOutCoRoutine(float delayBefore, float delayAfter, System.Action callback)
- {
- yield return new WaitForSeconds(delayBefore);
- while(group.alpha < 1)
- {
- group.alpha = Mathf.MoveTowards(group.alpha, 1f, instance.speed * Time.deltaTime);
- yield return null;
- }
- yield return new WaitForSeconds(delayAfter);
- if (callback != null) callback();
- }
- IEnumerator FadeInCoRoutine(float delayBefore, float delayAfter, System.Action callback)
- {
- yield return new WaitForSeconds(delayBefore);
- while (group.alpha > 0)
- {
- group.alpha = Mathf.MoveTowards(group.alpha, 0f, instance.speed * Time.deltaTime);
- yield return null;
- }
- yield return new WaitForSeconds(delayAfter);
- if (callback != null) callback();
- group.gameObject.SetActive(false);
- }
- static void StopActiveFades()
- {
- if(fadeInCR != null)
- {
- instance.StopCoroutine(fadeInCR);
- fadeInCR = null;
- }
- if(fadeOutCR != null)
- {
- instance.StopCoroutine(fadeOutCR);
- fadeOutCR = null;
- }
- }
- static void DefaultCallback(){}
- }
Advertisement
Add Comment
Please, Sign In to add comment