BaseFader.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. public abstract class BaseFader : IFade
  5. {
  6. public static bool StartCrossFade(IFade fadeIn, IFade fadeOut, float time, bool skippable = true, Func<float, bool> onUpdateFadeIn = null, Action onCompletedFadeIn = null, Func<float, bool> onUpdateFadeOut = null, Action onCompletedFadeOut = null)
  7. {
  8. if (fadeIn == null || fadeOut == null || fadeIn.isShow || fadeOut.isHide || fadeIn == fadeOut)
  9. {
  10. return false;
  11. }
  12. bool flag = fadeIn.StartFade(BaseFader.FadeType.In, time, skippable, onUpdateFadeIn, onCompletedFadeIn);
  13. return flag | fadeOut.StartFade(BaseFader.FadeType.Out, time, skippable, onUpdateFadeOut, onCompletedFadeOut);
  14. }
  15. public static bool StartCrossFade(IFade fadeIn, IFade fadeOut, bool skippable = true, Func<float, bool> onUpdateFadeIn = null, Action onCompletedFadeIn = null, Func<float, bool> onUpdateFadeOut = null, Action onCompletedFadeOut = null)
  16. {
  17. if (fadeIn == null || fadeOut == null || fadeIn.isShow || fadeOut.isHide || fadeIn == fadeOut)
  18. {
  19. return false;
  20. }
  21. bool flag = fadeIn.StartFade(BaseFader.FadeType.In, skippable, onUpdateFadeIn, onCompletedFadeIn);
  22. return flag | fadeOut.StartFade(BaseFader.FadeType.Out, skippable, onUpdateFadeOut, onCompletedFadeOut);
  23. }
  24. public static float DefaultFadeTime
  25. {
  26. get
  27. {
  28. return 0.5f;
  29. }
  30. }
  31. public virtual float customDefaultFadeTime
  32. {
  33. get
  34. {
  35. return this.customDefaultFadeTime_;
  36. }
  37. set
  38. {
  39. this.customDefaultFadeTime_ = value;
  40. }
  41. }
  42. public BaseFader.FadeState state { get; protected set; }
  43. public virtual bool isShow
  44. {
  45. get
  46. {
  47. return this.state == BaseFader.FadeState.Show || this.state == BaseFader.FadeState.FadeInNow;
  48. }
  49. }
  50. public virtual bool isHide
  51. {
  52. get
  53. {
  54. return this.state == BaseFader.FadeState.Hide || this.state == BaseFader.FadeState.FadeOutNow;
  55. }
  56. }
  57. public virtual bool isFadeNow
  58. {
  59. get
  60. {
  61. return this.state == BaseFader.FadeState.FadeInNow || this.state == BaseFader.FadeState.FadeOutNow;
  62. }
  63. }
  64. BaseFader.FadeState IFade.state
  65. {
  66. get
  67. {
  68. return this.state;
  69. }
  70. }
  71. float IFade.defaultFadeTime
  72. {
  73. get
  74. {
  75. return this.customDefaultFadeTime;
  76. }
  77. }
  78. protected MonoBehaviour coroutineInstant
  79. {
  80. get
  81. {
  82. return GameMain.Instance;
  83. }
  84. }
  85. public bool StartFade(BaseFader.FadeType type, bool skippable = true, Func<float, bool> onUpdate = null, Action onCompleted = null)
  86. {
  87. return this.StartFade(type, this.customDefaultFadeTime, skippable, onUpdate, onCompleted);
  88. }
  89. public virtual bool StartFade(BaseFader.FadeType type, float time, bool skippable = true, Func<float, bool> onUpdate = null, Action onCompleted = null)
  90. {
  91. if (type == BaseFader.FadeType.In)
  92. {
  93. if (this.isShow)
  94. {
  95. return false;
  96. }
  97. this.StartFade(true, time, skippable, onUpdate, onCompleted);
  98. }
  99. else
  100. {
  101. if (this.isHide)
  102. {
  103. return false;
  104. }
  105. this.StartFade(false, time, skippable, onUpdate, onCompleted);
  106. }
  107. return true;
  108. }
  109. public virtual void FadeComplete()
  110. {
  111. if (this.state == BaseFader.FadeState.FadeOutNow)
  112. {
  113. if (this.fadeCoroutine != null)
  114. {
  115. this.coroutineInstant.StopCoroutine(this.fadeCoroutine);
  116. }
  117. this.fadeCoroutine = null;
  118. this.ApplyFadeValue(0f);
  119. this.state = BaseFader.FadeState.Hide;
  120. if (this.onCompleted != null)
  121. {
  122. this.onCompleted();
  123. }
  124. this.onCompleted = null;
  125. }
  126. else if (this.state == BaseFader.FadeState.FadeInNow)
  127. {
  128. if (this.fadeCoroutine != null)
  129. {
  130. this.coroutineInstant.StopCoroutine(this.fadeCoroutine);
  131. }
  132. this.fadeCoroutine = null;
  133. this.ApplyFadeValue(1f);
  134. this.state = BaseFader.FadeState.Show;
  135. if (this.onCompleted != null)
  136. {
  137. this.onCompleted();
  138. }
  139. this.onCompleted = null;
  140. }
  141. }
  142. protected virtual void StartFade(bool typeIn, float time, bool skippable, Func<float, bool> onUpdate, Action onCompleted)
  143. {
  144. if (typeIn && this.state == BaseFader.FadeState.FadeOutNow)
  145. {
  146. if (this.fadeCoroutine != null)
  147. {
  148. this.coroutineInstant.StopCoroutine(this.fadeCoroutine);
  149. }
  150. this.fadeCoroutine = null;
  151. this.OnAbortFadeOut();
  152. }
  153. else if (!typeIn && this.state == BaseFader.FadeState.FadeInNow)
  154. {
  155. if (this.fadeCoroutine != null)
  156. {
  157. this.coroutineInstant.StopCoroutine(this.fadeCoroutine);
  158. }
  159. this.fadeCoroutine = null;
  160. this.OnAbortFadeIn();
  161. }
  162. this.state = ((!typeIn) ? BaseFader.FadeState.FadeOutNow : BaseFader.FadeState.FadeInNow);
  163. this.onCompleted = onCompleted;
  164. if (typeIn)
  165. {
  166. this.OnBeforeFadeIn();
  167. }
  168. else
  169. {
  170. this.OnBeforeFadeOut();
  171. }
  172. this.fadeCoroutine = this.coroutineInstant.StartCoroutine(this.Fade(this.GetFadeValue(), (!typeIn) ? 0f : 1f, time, skippable, onUpdate, delegate
  173. {
  174. this.state = ((!typeIn) ? BaseFader.FadeState.Hide : BaseFader.FadeState.Show);
  175. if (this.onCompleted != null)
  176. {
  177. this.onCompleted();
  178. }
  179. this.onCompleted = null;
  180. if (typeIn)
  181. {
  182. this.OnAfterFadeIn();
  183. }
  184. else
  185. {
  186. this.OnAfterFadeOut();
  187. }
  188. }));
  189. }
  190. protected virtual IEnumerator Fade(float from, float to, float fadeTime, bool skippable, Func<float, bool> onUpdate, Action onComplete)
  191. {
  192. if (fadeTime > 0f)
  193. {
  194. float totalTime = 0f;
  195. while (fadeTime >= totalTime)
  196. {
  197. totalTime += Time.unscaledDeltaTime;
  198. float value = Mathf.Lerp(from, to, totalTime / fadeTime);
  199. if ((skippable && this.CheckSkipState()) || (onUpdate != null && !onUpdate(value)))
  200. {
  201. break;
  202. }
  203. this.ApplyFadeValue(value);
  204. yield return null;
  205. }
  206. }
  207. else if (onUpdate != null)
  208. {
  209. onUpdate(to);
  210. }
  211. this.ApplyFadeValue(to);
  212. if (onComplete != null)
  213. {
  214. onComplete();
  215. }
  216. this.fadeCoroutine = null;
  217. yield break;
  218. }
  219. protected abstract void ApplyFadeValue(float value);
  220. protected abstract float GetFadeValue();
  221. protected abstract bool CheckSkipState();
  222. protected virtual void OnBeforeFadeIn()
  223. {
  224. }
  225. protected virtual void OnAfterFadeIn()
  226. {
  227. }
  228. protected virtual void OnBeforeFadeOut()
  229. {
  230. }
  231. protected virtual void OnAfterFadeOut()
  232. {
  233. }
  234. protected virtual void OnAbortFadeIn()
  235. {
  236. }
  237. protected virtual void OnAbortFadeOut()
  238. {
  239. }
  240. protected Action onCompleted;
  241. protected Coroutine fadeCoroutine;
  242. protected float customDefaultFadeTime_ = BaseFader.DefaultFadeTime;
  243. public enum FadeType
  244. {
  245. In,
  246. Out
  247. }
  248. public enum FadeState
  249. {
  250. None,
  251. Show,
  252. Hide,
  253. FadeInNow,
  254. FadeOutNow
  255. }
  256. }