Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections;
- using UnityEngine.UI;
- public class UIFader : MonoBehaviour
- {
- public float fadeSpeed = 1.5f; // Speed that the screen fades to and from black.
- public bool SwitchToMaskEnabled = false;
- public float SwitchToMaskThreshold = 0.3f;
- public float FadeFromAlpha = 1f;
- public float FadeToAlpha = 0;
- public bool Reverse = false;
- private CanvasGroup canvasGroup;
- void Awake()
- {
- // Set the texture so that it is the the size of the screen and covers it.
- canvasGroup = this.GetComponent<CanvasGroup>();
- }
- void Start()
- {
- canvasGroup.alpha = FadeFromAlpha;
- }
- void Update()
- {
- float newAlpha = canvasGroup.alpha;
- if (!Reverse)
- {
- newAlpha = Mathf.Lerp(canvasGroup.alpha, FadeToAlpha, fadeSpeed * Time.deltaTime);
- }
- else
- {
- newAlpha = Mathf.Lerp(canvasGroup.alpha, FadeFromAlpha, fadeSpeed * Time.deltaTime);
- }
- // Lerp the colour of the texture between itself and transparent.
- canvasGroup.alpha = newAlpha;
- if (SwitchToMaskEnabled && ((!Reverse && canvasGroup.alpha <= SwitchToMaskThreshold)||(Reverse && canvasGroup.alpha >= SwitchToMaskThreshold)))
- {
- canvasGroup.blocksRaycasts = Reverse;
- }
- }
- public void ToggleReverse()
- {
- SetReverse(!Reverse);
- }
- public void SetReverse(bool reverse)
- {
- Reverse = reverse;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement