uGUICanvasFade.cs 2.5 KB

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