UICanvasFade.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. [RequireComponent(typeof(CanvasGroup))]
  6. public class UICanvasFade : MonoBehaviour
  7. {
  8. private GameObject objCanvas
  9. {
  10. get
  11. {
  12. if (!this.m_ObjCanvas)
  13. {
  14. this.m_ObjCanvas = base.gameObject;
  15. }
  16. return this.m_ObjCanvas;
  17. }
  18. }
  19. private CanvasGroup canvasGroup
  20. {
  21. get
  22. {
  23. if (!this.m_CanvasGroup)
  24. {
  25. this.m_CanvasGroup = base.GetComponent<CanvasGroup>();
  26. }
  27. return this.m_CanvasGroup;
  28. }
  29. }
  30. public float fadeTime
  31. {
  32. get
  33. {
  34. return this.m_FadeTime;
  35. }
  36. set
  37. {
  38. this.m_FadeTime = Mathf.Clamp(value, 0f, 1f);
  39. }
  40. }
  41. public bool isTimeScaling
  42. {
  43. get
  44. {
  45. return this.m_IsTimeScaling;
  46. }
  47. set
  48. {
  49. this.m_IsTimeScaling = value;
  50. }
  51. }
  52. public void FadeIn(float fadeTime, bool isTimeScaling, UnityAction callback = null)
  53. {
  54. base.StopAllCoroutines();
  55. this.objCanvas.SetActive(true);
  56. this.canvasGroup.interactable = false;
  57. base.StartCoroutine(this.Coroutine_Fade(true, fadeTime, isTimeScaling, callback));
  58. }
  59. public void FadeIn()
  60. {
  61. this.FadeIn(this.m_FadeTime, this.m_IsTimeScaling, null);
  62. }
  63. public void FadeOut(float fadeTime, bool isTimeScaling, UnityAction callback = null)
  64. {
  65. if (!this.objCanvas.activeInHierarchy)
  66. {
  67. if (callback != null)
  68. {
  69. callback();
  70. }
  71. return;
  72. }
  73. base.StopAllCoroutines();
  74. this.canvasGroup.interactable = false;
  75. base.StartCoroutine(this.Coroutine_Fade(false, fadeTime, isTimeScaling, callback));
  76. }
  77. public void FadeOut()
  78. {
  79. this.FadeOut(this.m_FadeTime, this.m_IsTimeScaling, null);
  80. }
  81. private IEnumerator Coroutine_Fade(bool IsFadeIn, float fadeTime, bool isTimeScaling, UnityAction callback)
  82. {
  83. if (fadeTime > Mathf.Epsilon)
  84. {
  85. float speed = 1f / fadeTime;
  86. if (isTimeScaling)
  87. {
  88. speed *= Time.deltaTime;
  89. }
  90. else
  91. {
  92. speed *= Time.unscaledDeltaTime;
  93. }
  94. if (!IsFadeIn)
  95. {
  96. speed *= -1f;
  97. }
  98. do
  99. {
  100. this.canvasGroup.alpha += speed;
  101. yield return null;
  102. }
  103. while (this.canvasGroup.alpha > 0f && this.canvasGroup.alpha < 1f);
  104. }
  105. if (IsFadeIn)
  106. {
  107. this.objCanvas.SetActive(true);
  108. this.canvasGroup.interactable = true;
  109. this.canvasGroup.alpha = 1f;
  110. }
  111. else
  112. {
  113. this.objCanvas.SetActive(false);
  114. this.canvasGroup.interactable = false;
  115. this.canvasGroup.alpha = 0f;
  116. }
  117. if (callback != null)
  118. {
  119. callback();
  120. }
  121. yield break;
  122. }
  123. [SerializeField]
  124. private GameObject m_ObjCanvas;
  125. [SerializeField]
  126. private CanvasGroup m_CanvasGroup;
  127. [SerializeField]
  128. [Range(0f, 1f)]
  129. private float m_FadeTime = 0.5f;
  130. [SerializeField]
  131. private bool m_IsTimeScaling;
  132. }