Munchy2007

screenfader10

May 25th, 2016
4,749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.10 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class ScreenFader : MonoBehaviour {
  6.     static ScreenFader instance;
  7.     static CanvasGroup group;
  8.     public int sortOrder = 99;
  9.     public float speed = 3f;
  10.     public Color fadeColour = Color.black;
  11.  
  12.     static Coroutine fadeOutCR;
  13.     static Coroutine fadeInCR;
  14.  
  15.     void Awake()
  16.     {
  17.         if(instance != null)
  18.         {
  19.             DestroyImmediate(gameObject);
  20.             return;
  21.         }
  22.         instance = this;
  23.  
  24.         // Create the canvas and set it to screen space overlay mode
  25.         // --------------------------------------------------------------------------------
  26.         var canvas = new GameObject("Canvas", typeof(Canvas)).GetComponent<Canvas>();
  27.         canvas.transform.SetParent(transform, false);
  28.         canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  29.         canvas.sortingOrder = sortOrder;
  30.  
  31.         // Add a CanvasGroup component so we can make it non-blocking and control the alpha
  32.         // -----------------------------------------------------------------------------------------------------------------------
  33.         group = canvas.gameObject.AddComponent<CanvasGroup>();
  34.         group.alpha = 0f;
  35.         group.interactable = false;
  36.         group.blocksRaycasts = false;
  37.         group.gameObject.SetActive(false);
  38.  
  39.         // Add an image, set it's colour and scale it to ensure there is no border
  40.         // ------------------------------------------------------------------------------------------------
  41.         var image = new GameObject("image", typeof(Image)).GetComponent<Image>();
  42.         image.transform.SetParent(canvas.transform, false);
  43.         image.color = fadeColour;
  44.         image.transform.localScale =  new Vector2(1.25f, 1.25f);
  45.  
  46.         // Set the image to be centred and fill the screen, and set anchors to stretch the image if screen size changes
  47.         // ---------------------------------------------------------------------------------------------------------------------------------------------------
  48.         var imageRect = image.GetComponent<RectTransform>();
  49.         imageRect.anchorMin = Vector2.zero;
  50.         imageRect.anchorMax  = Vector2.one;
  51.         imageRect.anchoredPosition = Vector2.zero;
  52.         imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,Screen.width);
  53.         imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical,Screen.height);
  54.     }
  55.  
  56.     public static void FadeOut(float delayBefore = 0, float delayAfter = 0, System.Action callback = null)
  57.     {
  58.         if (callback == null) callback = DefaultCallback;
  59.         group.gameObject.SetActive(true);
  60.         StopActiveFades();
  61.         fadeOutCR = instance.StartCoroutine(FadeOutCoRoutine(delayBefore, delayAfter,callback));
  62.     }
  63.  
  64.     public static void FadeIn(float delayBefore = 0, float delayAfter = 0, System.Action callback = null)
  65.     {
  66.         if (callback == null) callback = DefaultCallback;
  67.         group.gameObject.SetActive(true);
  68.         StopActiveFades();
  69.         fadeInCR = instance.StartCoroutine(instance.FadeInCoRoutine(delayBefore, delayAfter,callback));
  70.     }
  71.  
  72.     static IEnumerator FadeOutCoRoutine(float delayBefore, float delayAfter, System.Action callback)
  73.     {
  74.         yield return new WaitForSeconds(delayBefore);
  75.         while(group.alpha < 1)
  76.         {
  77.             group.alpha = Mathf.MoveTowards(group.alpha, 1f, instance.speed * Time.deltaTime);
  78.             yield return null;
  79.         }
  80.         yield return new WaitForSeconds(delayAfter);
  81.         if (callback != null) callback();
  82.     }
  83.  
  84.     IEnumerator FadeInCoRoutine(float delayBefore, float delayAfter, System.Action callback)
  85.     {
  86.         yield return new WaitForSeconds(delayBefore);
  87.         while (group.alpha > 0)
  88.         {
  89.             group.alpha = Mathf.MoveTowards(group.alpha, 0f, instance.speed * Time.deltaTime);
  90.             yield return null;
  91.         }
  92.         yield return new WaitForSeconds(delayAfter);
  93.         if (callback != null) callback();
  94.  
  95.         group.gameObject.SetActive(false);
  96.     }
  97.  
  98.     static void StopActiveFades()
  99.     {
  100.         if(fadeInCR != null)
  101.         {
  102.             instance.StopCoroutine(fadeInCR);
  103.             fadeInCR = null;
  104.         }
  105.  
  106.         if(fadeOutCR != null)
  107.         {
  108.             instance.StopCoroutine(fadeOutCR);
  109.             fadeOutCR = null;
  110.         }
  111.     }
  112.  
  113.     static void DefaultCallback(){}
  114. }
Advertisement
Add Comment
Please, Sign In to add comment