TransitionController.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using Sirenix.OdinInspector;
  6. using KairoEngine.Core;
  7. using UniRx;
  8. namespace KairoEngine.UI
  9. {
  10. // Todo: Send events when fade starts and ends
  11. /// <summary>
  12. /// Controls a fade to black and fade from black curtain to use as transitions in the game.
  13. /// </summary>
  14. [HideMonoScript]
  15. public class TransitionController : MonoBehaviour
  16. {
  17. public static TransitionController instance;
  18. [InfoBox("The TransitionController is a full screen dissolve fader. It can transition the game to a black screen and back again.")]
  19. [Tooltip("An image component with no sprite and painted black. The object needs to cover the hole screen.")]
  20. public Image image;
  21. public CanvasGroup canvasGroup;
  22. private float timer;
  23. private float counter;
  24. private bool fadeIn = false;
  25. private bool fadeOut = false;
  26. private float alpha;
  27. private LTDescr _tweenObject = new LTDescr();
  28. void Awake()
  29. {
  30. if(instance == null) instance = this;
  31. else Destroy(this);
  32. }
  33. void OnEnable()
  34. {
  35. TransitionEvents.OnFadeIn += FadeIn;
  36. TransitionEvents.OnFadeOut += FadeOut;
  37. }
  38. void OnDisable()
  39. {
  40. TransitionEvents.OnFadeIn -= FadeIn;
  41. TransitionEvents.OnFadeOut -= FadeOut;
  42. }
  43. void Start()
  44. {
  45. image.gameObject.SetActive(false);
  46. }
  47. void Update()
  48. {
  49. // counter += Time.deltaTime;
  50. // if(fadeIn) alpha = Mathf.Lerp(0, 255, counter / timer);
  51. // if(fadeOut) alpha = Mathf.Lerp(255, 0, counter / timer);
  52. // image.color = new Color32(0, 0, 0, (byte)Mathf.FloorToInt(alpha));
  53. // if(counter > timer)
  54. // {
  55. // counter = 0;
  56. // if(fadeOut) image.gameObject.SetActive(false);
  57. // fadeIn= false;
  58. // fadeOut = false;
  59. // TransitionEvents.TransitionFinish();
  60. // }
  61. }
  62. [Button("Fade In"), ButtonGroup, PropertyTooltip("Transition the game to a black screen. Only works during gameplay.")]
  63. public void StartFadeIn(float time = 0.7f)
  64. {
  65. fadeIn = true;
  66. fadeOut = false;
  67. timer = time;
  68. counter = 0;
  69. image.gameObject.SetActive(true);
  70. if(_tweenObject == null) _tweenObject = new LTDescr();
  71. canvasGroup.alpha = 0;
  72. _tweenObject = LeanTween.alphaCanvas(canvasGroup, 1, time).setIgnoreTimeScale( true );
  73. //Debug.Log("Fading In");
  74. }
  75. [Button("Fade Out"), ButtonGroup, PropertyTooltip("Transition from a black screen back to the game. Only works during gameplay.")]
  76. public void StartFadeOut(float time = 0.7f)
  77. {
  78. fadeOut = true;
  79. fadeIn= false;
  80. timer = time;
  81. counter = 0;
  82. image.gameObject.SetActive(true);
  83. //Debug.Log("Fading Out");
  84. if(_tweenObject == null) _tweenObject = new LTDescr();
  85. canvasGroup.alpha = 1;
  86. _tweenObject = LeanTween.alphaCanvas(canvasGroup, 0, time).setIgnoreTimeScale( true );
  87. Timer.Execute(time * 1000, () => {
  88. image.gameObject.SetActive(false);
  89. });
  90. }
  91. public static void FadeIn(float time = 0.7f)
  92. {
  93. if(instance != null) instance.StartFadeIn(time);
  94. }
  95. public static void FadeOut(float time = 0.7f)
  96. {
  97. if(instance != null) instance.StartFadeOut(time);
  98. }
  99. }
  100. }