BaseUIView.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using UnityEngine;
  5. using UnityEngine.EventSystems;
  6. using UnityEngine.UI;
  7. namespace ProjectBase.UI
  8. {
  9. public class BaseUIView : MonoBehaviour
  10. {
  11. public SOUIViewConfig uiViewConfig;
  12. [HideInInspector]
  13. public UIViewLayerController viewLayerController;
  14. private UIViewState m_ViewState;
  15. protected Canvas canvas;
  16. private bool refreshed = false;
  17. public UIViewState ViewState
  18. {
  19. get
  20. {
  21. return m_ViewState;
  22. }
  23. set
  24. {
  25. m_ViewState = value;
  26. #if UNITY_EDITOR
  27. name = string.Format("{0}{1}", uiViewConfig.viewName, System.Enum.GetName(typeof(UIViewState), ViewState));
  28. #endif
  29. }
  30. }
  31. public int ViewSortOrder
  32. {
  33. get
  34. {
  35. return canvas.sortingOrder;
  36. }
  37. set
  38. {
  39. canvas.overrideSorting = true;
  40. canvas.sortingOrder = value;
  41. }
  42. }
  43. #region Unity
  44. #endregion
  45. public void Init()
  46. {
  47. InitCanvas();
  48. InitUIObjects();
  49. }
  50. private void InitCanvas()
  51. {
  52. canvas = GetComponent<Canvas>();
  53. if (canvas == null)
  54. {
  55. canvas = gameObject.AddComponent<Canvas>();
  56. }
  57. GraphicRaycaster caster = GetComponent<GraphicRaycaster>();
  58. if (caster == null)
  59. {
  60. gameObject.AddComponent<GraphicRaycaster>();
  61. }
  62. }
  63. protected virtual void InitUIObjects() { }
  64. public virtual void OnPush()
  65. {
  66. ViewState = UIViewState.Nonvisible;
  67. UpdateSortLayer();
  68. }
  69. public void OnShow()
  70. {
  71. if (ViewState != UIViewState.Visible)
  72. {
  73. if (!gameObject.activeSelf)
  74. {
  75. gameObject.SetActive(true);
  76. }
  77. Vector3 pos = transform.localPosition;
  78. pos.z = 0;
  79. transform.localPosition = pos;
  80. OnShowCallBack();
  81. ViewState = UIViewState.Visible;
  82. if (refreshed)
  83. {
  84. UpdateView();
  85. }
  86. }
  87. }
  88. protected virtual void OnShowCallBack() { }
  89. public void OnHide()
  90. {
  91. if (ViewState != UIViewState.Nonvisible)
  92. {
  93. Vector3 pos = transform.localPosition;
  94. pos.z = int.MaxValue;
  95. transform.localPosition = pos;
  96. OnHideCallBack();
  97. ViewState = UIViewState.Nonvisible;
  98. }
  99. }
  100. protected virtual void OnHideCallBack() { }
  101. public virtual void OnPopup()
  102. {
  103. if (ViewState == UIViewState.Cache)
  104. {
  105. return;
  106. }
  107. if (ViewState == UIViewState.Visible)
  108. {
  109. OnHide();
  110. }
  111. ViewState = UIViewState.Cache;
  112. }
  113. public virtual void OnExit()
  114. {
  115. if (ViewState != UIViewState.Cache)
  116. {
  117. OnPopup();
  118. }
  119. }
  120. public void SetArguments(params object[] args)
  121. {
  122. refreshed = true;
  123. UpdateArguments(args);
  124. if (uiViewConfig.alwaysUpdate || ViewState == UIViewState.Visible)
  125. {
  126. UpdateView();
  127. }
  128. }
  129. protected virtual void UpdateArguments(params object[] args) { }
  130. public void UpdateView()
  131. {
  132. refreshed = false;
  133. UpdateViewCallBack();
  134. }
  135. protected virtual void UpdateViewCallBack() { }
  136. private void UpdateSortLayer()
  137. {
  138. canvas.overrideSorting = true;
  139. switch (uiViewConfig.viewLayer)
  140. {
  141. case UIViewLayer.BackGround:
  142. canvas.sortingLayerID = SortingLayer.NameToID("BackGround");
  143. break;
  144. case UIViewLayer.Base:
  145. canvas.sortingLayerID = SortingLayer.NameToID("Base");
  146. break;
  147. case UIViewLayer.Popup:
  148. canvas.sortingLayerID = SortingLayer.NameToID("Popup");
  149. break;
  150. case UIViewLayer.Top:
  151. canvas.sortingLayerID = SortingLayer.NameToID("Top");
  152. break;
  153. case UIViewLayer.Debug:
  154. canvas.sortingLayerID = SortingLayer.NameToID("Debug");
  155. break;
  156. default:
  157. break;
  158. }
  159. }
  160. public void ResetUIView()
  161. {
  162. ViewSortOrder = 0;
  163. viewLayerController = null;
  164. }
  165. }
  166. }