Advertisement
Guest User

Untitled

a guest
May 4th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4.  
  5. public class UIFader : MonoBehaviour
  6. {
  7. public float fadeSpeed = 1.5f; // Speed that the screen fades to and from black.
  8. public bool SwitchToMaskEnabled = false;
  9. public float SwitchToMaskThreshold = 0.3f;
  10. public float FadeFromAlpha = 1f;
  11. public float FadeToAlpha = 0;
  12.  
  13. public bool Reverse = false;
  14.  
  15. private CanvasGroup canvasGroup;
  16. void Awake()
  17. {
  18.  
  19. // Set the texture so that it is the the size of the screen and covers it.
  20. canvasGroup = this.GetComponent<CanvasGroup>();
  21.  
  22. }
  23.  
  24. void Start()
  25. {
  26. canvasGroup.alpha = FadeFromAlpha;
  27. }
  28. void Update()
  29. {
  30. float newAlpha = canvasGroup.alpha;
  31. if (!Reverse)
  32. {
  33. newAlpha = Mathf.Lerp(canvasGroup.alpha, FadeToAlpha, fadeSpeed * Time.deltaTime);
  34. }
  35. else
  36. {
  37. newAlpha = Mathf.Lerp(canvasGroup.alpha, FadeFromAlpha, fadeSpeed * Time.deltaTime);
  38. }
  39. // Lerp the colour of the texture between itself and transparent.
  40. canvasGroup.alpha = newAlpha;
  41.  
  42. if (SwitchToMaskEnabled && ((!Reverse && canvasGroup.alpha <= SwitchToMaskThreshold)||(Reverse && canvasGroup.alpha >= SwitchToMaskThreshold)))
  43. {
  44. canvasGroup.blocksRaycasts = Reverse;
  45. }
  46. }
  47. public void ToggleReverse()
  48. {
  49. SetReverse(!Reverse);
  50. }
  51. public void SetReverse(bool reverse)
  52. {
  53. Reverse = reverse;
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement