BasePanelMgr.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using System;
  2. using com.workman.cm3d2.scene.dailyEtc;
  3. using UnityEngine;
  4. public abstract class BasePanelMgr : MonoBehaviour
  5. {
  6. public abstract void Init();
  7. public void SetDurationToFadeIn(float duration)
  8. {
  9. this.durationToFadeIn_ = duration;
  10. }
  11. public void SetDurationToFadeOut(float duration)
  12. {
  13. this.durationToFadeOut_ = duration;
  14. }
  15. protected virtual void SetFadeTargetPanel()
  16. {
  17. }
  18. protected virtual void OpenPanel()
  19. {
  20. }
  21. protected virtual void OpenPanel(string openType)
  22. {
  23. }
  24. protected virtual void ClosePanel()
  25. {
  26. }
  27. protected virtual void BeforeFadeOut()
  28. {
  29. }
  30. protected virtual void BeforeFadeIn()
  31. {
  32. }
  33. private float durationToFadeIn
  34. {
  35. get
  36. {
  37. return this.durationToFadeIn_;
  38. }
  39. }
  40. private float durationToFadeOut
  41. {
  42. get
  43. {
  44. return this.durationToFadeOut_;
  45. }
  46. }
  47. protected virtual void Start()
  48. {
  49. GameObject f_goParent = GameObject.Find("/UI Root");
  50. GameObject childObject = UTY.GetChildObject(f_goParent, "Manager", false);
  51. if (childObject != null)
  52. {
  53. this.sceneMgr = childObject.GetComponent<SceneMgr>();
  54. }
  55. this.SetDurationToFadeIn(0.25f);
  56. this.SetDurationToFadeOut(0.25f);
  57. this.ChangeFadeState(BasePanelMgr.FadeState.None);
  58. }
  59. private void ChangeFadeState(BasePanelMgr.FadeState fadeState)
  60. {
  61. this.currentFadeState = fadeState;
  62. }
  63. protected T GetManager<T>() where T : BasePanelMgr
  64. {
  65. return this.sceneMgr.GetManager<T>();
  66. }
  67. public void BaseInit()
  68. {
  69. this.Init();
  70. this.SetFadeTargetPanel();
  71. }
  72. public void BaseOpenPanel()
  73. {
  74. this.BeforeOpen();
  75. this.OpenPanel();
  76. this.FadeInPanel();
  77. }
  78. public void BaseOpenPanel(string openType)
  79. {
  80. this.BeforeOpen();
  81. this.OpenPanel(openType);
  82. this.FadeInPanel();
  83. }
  84. public void BaseClosePanel()
  85. {
  86. this.BeforeClose();
  87. this.ClosePanel();
  88. this.FadeOutPanel();
  89. }
  90. public void BaseClosePanel(Action OnFinished)
  91. {
  92. this.BeforeClose();
  93. this.ClosePanel();
  94. this.FadeOutPanel(OnFinished);
  95. }
  96. public void BaseClosePanel<T>(Action<T> OnFinished, T param)
  97. {
  98. this.BeforeClose();
  99. this.ClosePanel();
  100. this.FadeOutPanel<T>(OnFinished, param);
  101. }
  102. protected void FadeInPanel()
  103. {
  104. this.fadeTargetPanel.SetActive(true);
  105. this.BeforeFadeIn();
  106. this.ChangeFadeState(BasePanelMgr.FadeState.StartFadeIn);
  107. TweenAlpha tweenAlpha = this.fadeTargetPanel.GetComponent<TweenAlpha>();
  108. if (tweenAlpha)
  109. {
  110. UnityEngine.Object.Destroy(tweenAlpha);
  111. }
  112. if (this.durationToFadeIn <= 0f)
  113. {
  114. UIRect uirect = this.fadeTargetPanel.GetComponent<UIRect>();
  115. SpriteRenderer component = this.fadeTargetPanel.GetComponent<SpriteRenderer>();
  116. Material material = null;
  117. if (uirect == null && component == null)
  118. {
  119. Renderer component2 = this.fadeTargetPanel.GetComponent<Renderer>();
  120. if (component2 != null)
  121. {
  122. material = component2.material;
  123. }
  124. if (material == null)
  125. {
  126. uirect = this.fadeTargetPanel.GetComponentInChildren<UIRect>();
  127. }
  128. }
  129. float num = 1f;
  130. if (uirect != null)
  131. {
  132. uirect.alpha = num;
  133. }
  134. else if (component != null)
  135. {
  136. Color color = component.color;
  137. color.a = num;
  138. component.color = color;
  139. }
  140. else if (material != null)
  141. {
  142. Color color2 = material.color;
  143. color2.a = num;
  144. material.color = color2;
  145. }
  146. this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeIn);
  147. this.AfterFadeIn();
  148. return;
  149. }
  150. tweenAlpha = this.fadeTargetPanel.AddComponent<TweenAlpha>();
  151. tweenAlpha.from = 0f;
  152. tweenAlpha.to = 1f;
  153. tweenAlpha.duration = this.durationToFadeIn;
  154. EventDelegate.Set(tweenAlpha.onFinished, delegate
  155. {
  156. this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeIn);
  157. this.AfterFadeIn();
  158. });
  159. tweenAlpha.PlayForward();
  160. }
  161. protected void FadeOutPanel()
  162. {
  163. this.BeforeFadeOut();
  164. this.ChangeFadeState(BasePanelMgr.FadeState.StartFadeOut);
  165. if (this.durationToFadeOut <= 0f)
  166. {
  167. UIRect uirect = this.fadeTargetPanel.GetComponent<UIRect>();
  168. SpriteRenderer component = this.fadeTargetPanel.GetComponent<SpriteRenderer>();
  169. Material material = null;
  170. if (uirect == null && component == null)
  171. {
  172. Renderer component2 = this.fadeTargetPanel.GetComponent<Renderer>();
  173. if (component2 != null)
  174. {
  175. material = component2.material;
  176. }
  177. if (material == null)
  178. {
  179. uirect = this.fadeTargetPanel.GetComponentInChildren<UIRect>();
  180. }
  181. }
  182. float num = 0f;
  183. if (uirect != null)
  184. {
  185. uirect.alpha = num;
  186. }
  187. else if (component != null)
  188. {
  189. Color color = component.color;
  190. color.a = num;
  191. component.color = color;
  192. }
  193. else if (material != null)
  194. {
  195. Color color2 = material.color;
  196. color2.a = num;
  197. material.color = color2;
  198. }
  199. this.fadeTargetPanel.SetActive(false);
  200. this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeOut);
  201. this.AfterFadeOut();
  202. return;
  203. }
  204. TweenAlpha tweenAlpha = TweenAlpha.Begin(this.fadeTargetPanel, this.durationToFadeOut, 0f);
  205. EventDelegate.Set(tweenAlpha.onFinished, delegate
  206. {
  207. this.fadeTargetPanel.SetActive(false);
  208. this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeOut);
  209. this.AfterFadeOut();
  210. });
  211. }
  212. protected void FadeOutPanel(Action OnFinished)
  213. {
  214. this.BeforeFadeOut();
  215. this.ChangeFadeState(BasePanelMgr.FadeState.StartFadeOut);
  216. TweenAlpha tweenAlpha = TweenAlpha.Begin(this.fadeTargetPanel, this.durationToFadeOut, 0f);
  217. EventDelegate.Set(tweenAlpha.onFinished, delegate
  218. {
  219. this.fadeTargetPanel.SetActive(false);
  220. this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeOut);
  221. this.AfterFadeOut();
  222. OnFinished();
  223. });
  224. }
  225. protected void FadeOutPanel<T>(Action<T> OnFinished, T param)
  226. {
  227. this.BeforeFadeOut();
  228. this.ChangeFadeState(BasePanelMgr.FadeState.StartFadeOut);
  229. TweenAlpha tweenAlpha = TweenAlpha.Begin(this.fadeTargetPanel, this.durationToFadeOut, 0f);
  230. EventDelegate.Set(tweenAlpha.onFinished, delegate
  231. {
  232. this.fadeTargetPanel.SetActive(false);
  233. this.ChangeFadeState(BasePanelMgr.FadeState.EndFadeOut);
  234. this.AfterFadeOut();
  235. OnFinished(param);
  236. });
  237. }
  238. protected virtual void AfterFadeIn()
  239. {
  240. this.AfterOpen();
  241. }
  242. protected virtual void AfterFadeOut()
  243. {
  244. this.AfterClose();
  245. }
  246. protected virtual void BeforeOpen()
  247. {
  248. }
  249. protected virtual void AfterOpen()
  250. {
  251. }
  252. protected virtual void BeforeClose()
  253. {
  254. }
  255. protected virtual void AfterClose()
  256. {
  257. }
  258. protected GameObject fadeTargetPanel;
  259. protected SceneMgr sceneMgr;
  260. protected BasePanelMgr.FadeState currentFadeState;
  261. private float durationToFadeIn_;
  262. private float durationToFadeOut_;
  263. private const string uiRootPath = "/UI Root";
  264. private const float DURATION_TO_FADE = 0.25f;
  265. public enum FadeState
  266. {
  267. None,
  268. StartFadeIn,
  269. EndFadeIn,
  270. StartFadeOut,
  271. EndFadeOut
  272. }
  273. }