Advertisement
Munchy2007

screenfader5

May 25th, 2016
4,143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.75 KB | None | 0 0
  1.     void Awake()
  2.     {
  3.         if(instance != null)
  4.         {
  5.             DestroyImmediate(gameObject);
  6.             return;
  7.         }
  8.         instance = this;
  9.  
  10.         // Create the canvas and set it to screen space overlay mode
  11.         // ---------------------------------------------------------
  12.         var canvas = new GameObject("Canvas", typeof(Canvas)).GetComponent<Canvas>();
  13.         canvas.transform.SetParent(transform, false);
  14.         canvas.renderMode = RenderMode.ScreenSpaceOverlay;
  15.         canvas.sortingOrder = sortOrder;
  16.  
  17.         // Add a CanvasGroup component so we can make it non-blocking and control the alpha
  18.         // --------------------------------------------------------------------------------
  19.         group = canvas.gameObject.AddComponent<CanvasGroup>();
  20.         group.alpha = 0f;
  21.         group.interactable = false;
  22.         group.blocksRaycasts = false;
  23.         group.gameObject.SetActive(false);
  24.  
  25.         // Add an image, set it's colour and scale it to ensure there is no border
  26.         // -----------------------------------------------------------------------
  27.         var image = new GameObject("image", typeof(Image)).GetComponent<Image>();
  28.         image.transform.SetParent(canvas.transform, false);
  29.         image.color = fadeColour;
  30.         image.transform.localScale =  new Vector2(1.25f, 1.25f);
  31.  
  32.         // Set the image to be centred and fill the screen,
  33.         // and set anchors to stretch the image if screen size changes
  34.         // -------------------------------------------------------------
  35.         var imageRect = image.GetComponent<RectTransform>();
  36.         imageRect.anchorMin = Vector2.zero;
  37.         imageRect.anchorMax  = Vector2.one;
  38.         imageRect.anchoredPosition = Vector2.zero;
  39.         imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal,Screen.width);
  40.         imageRect.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical,Screen.height);
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement