BasePanelController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. using System;
  2. using System.Collections;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace TapSDK.UI
  6. {
  7. /// <summary>
  8. /// base panel of TapSDK UI module
  9. /// </summary>
  10. [RequireComponent(typeof(Canvas))]
  11. [RequireComponent(typeof(CanvasGroup))]
  12. public abstract class BasePanelController : MonoBehaviour
  13. {
  14. /// <summary>
  15. /// the canvas related to this panel
  16. /// </summary>
  17. [HideInInspector]
  18. public Canvas canvas;
  19. /// <summary>
  20. /// the canvas group related to this panel
  21. /// </summary>
  22. [HideInInspector]
  23. public CanvasGroup canvasGroup;
  24. /// <summary>
  25. /// fade in/out time
  26. /// </summary>
  27. private const float FADE_TIME = 0.1f;
  28. /// <summary>
  29. /// animation elapse time
  30. /// </summary>
  31. private float _animationElapse;
  32. private Vector2 _screenSize;
  33. private Vector2 _cachedAnchorPos;
  34. private RectTransform _rectTransform;
  35. private Coroutine _animationCoroutine;
  36. /// <summary>
  37. /// open parameter
  38. /// </summary>
  39. protected IOpenPanelParameter openParam;
  40. /// <summary>
  41. /// sorting order of this panel
  42. /// </summary>
  43. public int openOrder;
  44. /// <summary>
  45. /// settings about this panel
  46. /// </summary>
  47. public BasePanelConfig panelConfig;
  48. /// <summary>
  49. /// the transform parent when created it would be attached to
  50. /// </summary>
  51. /// <value></value>
  52. public virtual Transform AttachedParent => UIManager.Instance.GetUIRootTransform();
  53. #region Load
  54. protected virtual void Awake()
  55. {
  56. canvas = GetComponent<Canvas>();
  57. canvasGroup = GetComponent<CanvasGroup>();
  58. _rectTransform = transform as RectTransform;
  59. _screenSize = new Vector2(Screen.width, Screen.height);
  60. #if UNITY_EDITOR
  61. if (canvas == null)
  62. {
  63. Debug.LogErrorFormat("[TapSDK UI] BasePanel Must Be Related To Canvas Component!");
  64. }
  65. #endif
  66. }
  67. /// <summary>
  68. /// bind ugui components for every panel
  69. /// </summary>
  70. protected virtual void BindComponents() {}
  71. /// <summary>
  72. /// create the prefab instance
  73. /// </summary>
  74. /// <param name="param"></param>
  75. public void OnLoaded(IOpenPanelParameter param = null)
  76. {
  77. openParam = param;
  78. // 寻找组件
  79. BindComponents();
  80. // 添加到控制层
  81. UIManager.Instance.AddUI(this);
  82. // 更新层级信息
  83. InitCanvasSetting();
  84. // 开始动画效果
  85. OnShowEffectStart();
  86. // 调用加载成功方法
  87. OnLoadSuccess();
  88. }
  89. private void InitCanvasSetting()
  90. {
  91. if (canvas.renderMode != RenderMode.ScreenSpaceOverlay)
  92. {
  93. var camera = UIManager.Instance.GetUICamera();
  94. if (camera != null)
  95. {
  96. canvas.worldCamera = camera;
  97. }
  98. }
  99. canvas.pixelPerfect = true;
  100. canvas.overrideSorting = true;
  101. }
  102. /// <summary>
  103. /// init panel logic here
  104. /// </summary>
  105. protected virtual void OnLoadSuccess()
  106. {
  107. }
  108. #endregion
  109. #region Animation
  110. protected virtual void OnShowEffectStart()
  111. {
  112. if (panelConfig.animationType == EAnimationMode.None)
  113. {
  114. return;
  115. }
  116. OnEffectStart();
  117. if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
  118. {
  119. canvasGroup.alpha = 0;
  120. }
  121. if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
  122. {
  123. transform.localScale = Vector3.zero;
  124. }
  125. if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
  126. {
  127. _cachedAnchorPos = _rectTransform.anchoredPosition;
  128. _rectTransform.anchoredPosition += new Vector2(_screenSize.x, 0);
  129. }
  130. _animationCoroutine = StartCoroutine(FadeInCoroutine(FADE_TIME));
  131. }
  132. protected virtual void OnShowEffectEnd()
  133. {
  134. OnEffectEnd();
  135. }
  136. protected virtual void OnCloseEffectStart()
  137. {
  138. OnEffectStart();
  139. if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
  140. {
  141. canvasGroup.alpha = 1;
  142. }
  143. if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
  144. {
  145. transform.localScale = Vector3.one;
  146. }
  147. if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
  148. {
  149. _rectTransform.anchoredPosition = _cachedAnchorPos;
  150. }
  151. _animationCoroutine = StartCoroutine(FadeOutCoroutine(FADE_TIME));
  152. }
  153. protected virtual void OnCloseEffectEnd()
  154. {
  155. OnEffectEnd();
  156. GameObject.Destroy(gameObject);
  157. }
  158. private void OnEffectStart()
  159. {
  160. _animationElapse = 0;
  161. if (_animationCoroutine != null)
  162. {
  163. StopCoroutine(_animationCoroutine);
  164. _animationCoroutine = null;
  165. }
  166. canvasGroup.interactable = false;
  167. }
  168. private void OnEffectEnd()
  169. {
  170. canvasGroup.interactable = true;
  171. _animationElapse = 0;
  172. _animationCoroutine = null;
  173. }
  174. private IEnumerator FadeInCoroutine(float time)
  175. {
  176. while (_animationElapse < time)
  177. {
  178. yield return null;
  179. _animationElapse += Time.deltaTime;
  180. float value = Mathf.Clamp01(_animationElapse / time);
  181. if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
  182. {
  183. canvasGroup.alpha = value;
  184. }
  185. if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
  186. {
  187. transform.localScale = new Vector3(value, value, value);
  188. }
  189. if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
  190. {
  191. var temp = (1 - value) * _screenSize.x;
  192. _rectTransform.anchoredPosition = new Vector2(_cachedAnchorPos.x + temp, _cachedAnchorPos.y);
  193. }
  194. }
  195. if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
  196. {
  197. canvasGroup.alpha = 1;
  198. }
  199. if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
  200. {
  201. transform.localScale = Vector3.one;
  202. }
  203. if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
  204. {
  205. _rectTransform.anchoredPosition = _cachedAnchorPos;
  206. }
  207. OnShowEffectEnd();
  208. }
  209. private IEnumerator FadeOutCoroutine(float time)
  210. {
  211. while (_animationElapse < time)
  212. {
  213. yield return null;
  214. _animationElapse += Time.deltaTime;
  215. float value = 1 - Mathf.Clamp01(_animationElapse / time);
  216. if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
  217. {
  218. canvasGroup.alpha = value;
  219. }
  220. if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
  221. {
  222. transform.localScale = new Vector3(value, value, value);
  223. }
  224. if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
  225. {
  226. var temp = (1 - value) * _screenSize.x;
  227. _rectTransform.anchoredPosition = new Vector2(_cachedAnchorPos.x + temp, _cachedAnchorPos.y);
  228. }
  229. }
  230. if ((panelConfig.animationType & EAnimationMode.Alpha) == EAnimationMode.Alpha)
  231. {
  232. canvasGroup.alpha = 0;
  233. }
  234. if ((panelConfig.animationType & EAnimationMode.Scale) == EAnimationMode.Scale)
  235. {
  236. transform.localScale = Vector3.zero;
  237. }
  238. if ((panelConfig.animationType & EAnimationMode.RightSlideIn) == EAnimationMode.RightSlideIn)
  239. {
  240. _rectTransform.anchoredPosition = new Vector2(_cachedAnchorPos.x + _screenSize.x, _cachedAnchorPos.y);
  241. }
  242. OnCloseEffectEnd();
  243. }
  244. #endregion
  245. /// <summary>
  246. /// on receive resolution change event
  247. /// </summary>
  248. /// <param name="res"></param>
  249. protected virtual void UIAdapt(Vector2Int res) {}
  250. /// <summary>
  251. /// common close api
  252. /// </summary>
  253. public virtual void Close()
  254. {
  255. UIManager.Instance.RemoveUI(this);
  256. }
  257. /// <summary>
  258. /// set canvas sorting order
  259. /// </summary>
  260. /// <param name="openOrder"></param>
  261. public void SetOpenOrder(int openOrder)
  262. {
  263. this.openOrder = openOrder;
  264. if (canvas != null)
  265. {
  266. canvas.sortingOrder = openOrder;
  267. }
  268. }
  269. /// <summary>
  270. /// also would destroy panel gameObject
  271. /// </summary>
  272. public virtual void Dispose()
  273. {
  274. if (panelConfig.animationType == EAnimationMode.None)
  275. {
  276. GameObject.Destroy(gameObject);
  277. return;
  278. }
  279. OnCloseEffectStart();
  280. }
  281. }
  282. }