using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using Sirenix.OdinInspector; using KairoEngine.Core; using UniRx; namespace KairoEngine.UI { // Todo: Send events when fade starts and ends /// /// Controls a fade to black and fade from black curtain to use as transitions in the game. /// [HideMonoScript] public class TransitionController : MonoBehaviour { public static TransitionController instance; [InfoBox("The TransitionController is a full screen dissolve fader. It can transition the game to a black screen and back again.")] [Tooltip("An image component with no sprite and painted black. The object needs to cover the hole screen.")] public Image image; public CanvasGroup canvasGroup; private float timer; private float counter; private bool fadeIn = false; private bool fadeOut = false; private float alpha; private LTDescr _tweenObject = new LTDescr(); void Awake() { if(instance == null) instance = this; else Destroy(this); } void OnEnable() { TransitionEvents.OnFadeIn += FadeIn; TransitionEvents.OnFadeOut += FadeOut; } void OnDisable() { TransitionEvents.OnFadeIn -= FadeIn; TransitionEvents.OnFadeOut -= FadeOut; } void Start() { image.gameObject.SetActive(false); } void Update() { // counter += Time.deltaTime; // if(fadeIn) alpha = Mathf.Lerp(0, 255, counter / timer); // if(fadeOut) alpha = Mathf.Lerp(255, 0, counter / timer); // image.color = new Color32(0, 0, 0, (byte)Mathf.FloorToInt(alpha)); // if(counter > timer) // { // counter = 0; // if(fadeOut) image.gameObject.SetActive(false); // fadeIn= false; // fadeOut = false; // TransitionEvents.TransitionFinish(); // } } [Button("Fade In"), ButtonGroup, PropertyTooltip("Transition the game to a black screen. Only works during gameplay.")] public void StartFadeIn(float time = 0.7f) { fadeIn = true; fadeOut = false; timer = time; counter = 0; image.gameObject.SetActive(true); if(_tweenObject == null) _tweenObject = new LTDescr(); canvasGroup.alpha = 0; _tweenObject = LeanTween.alphaCanvas(canvasGroup, 1, time).setIgnoreTimeScale( true ); //Debug.Log("Fading In"); } [Button("Fade Out"), ButtonGroup, PropertyTooltip("Transition from a black screen back to the game. Only works during gameplay.")] public void StartFadeOut(float time = 0.7f) { fadeOut = true; fadeIn= false; timer = time; counter = 0; image.gameObject.SetActive(true); //Debug.Log("Fading Out"); if(_tweenObject == null) _tweenObject = new LTDescr(); canvasGroup.alpha = 1; _tweenObject = LeanTween.alphaCanvas(canvasGroup, 0, time).setIgnoreTimeScale( true ); Timer.Execute(time * 1000, () => { image.gameObject.SetActive(false); }); } public static void FadeIn(float time = 0.7f) { if(instance != null) instance.StartFadeIn(time); } public static void FadeOut(float time = 0.7f) { if(instance != null) instance.StartFadeOut(time); } } }