UGuiForm.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 Jiang Yin. All rights reserved.
  4. // Homepage: https://gameframework.cn/
  5. // Feedback: mailto:ellan@gameframework.cn
  6. //------------------------------------------------------------
  7. using System.Collections;
  8. using System.Collections.Generic;
  9. using UnityEngine;
  10. using UnityEngine.UI;
  11. using UnityGameFramework.Runtime;
  12. namespace MetaClient
  13. {
  14. public abstract class UGuiForm : UIFormLogic
  15. {
  16. public const int DepthFactor = 100;
  17. private const float FadeTime = 0.3f;
  18. private static Font s_MainFont = null;
  19. private Canvas m_CachedCanvas = null;
  20. private CanvasGroup m_CanvasGroup = null;
  21. private List<Canvas> m_CachedCanvasContainer = new List<Canvas>();
  22. public int OriginalDepth
  23. {
  24. get;
  25. private set;
  26. }
  27. public int Depth
  28. {
  29. get
  30. {
  31. return m_CachedCanvas.sortingOrder;
  32. }
  33. }
  34. public void Close()
  35. {
  36. Close(false);
  37. }
  38. public void Close(bool ignoreFade)
  39. {
  40. StopAllCoroutines();
  41. if (ignoreFade)
  42. {
  43. GameEntry.UI.CloseUIForm(this);
  44. }
  45. else
  46. {
  47. StartCoroutine(CloseCo(FadeTime));
  48. }
  49. }
  50. public void PlayUISound(int uiSoundId)
  51. {
  52. GameEntry.Sound.PlayUISound(uiSoundId);
  53. }
  54. public static void SetMainFont(Font mainFont)
  55. {
  56. if (mainFont == null)
  57. {
  58. Log.Error("Main font is invalid.");
  59. return;
  60. }
  61. s_MainFont = mainFont;
  62. }
  63. #if UNITY_2017_3_OR_NEWER
  64. protected override void OnInit(object userData)
  65. #else
  66. protected internal override void OnInit(object userData)
  67. #endif
  68. {
  69. base.OnInit(userData);
  70. m_CachedCanvas = gameObject.GetOrAddComponent<Canvas>();
  71. m_CachedCanvas.overrideSorting = true;
  72. OriginalDepth = m_CachedCanvas.sortingOrder;
  73. m_CanvasGroup = gameObject.GetOrAddComponent<CanvasGroup>();
  74. RectTransform transform = GetComponent<RectTransform>();
  75. transform.anchorMin = Vector2.zero;
  76. transform.anchorMax = Vector2.one;
  77. transform.anchoredPosition = Vector2.zero;
  78. transform.sizeDelta = Vector2.zero;
  79. gameObject.GetOrAddComponent<GraphicRaycaster>();
  80. Text[] texts = GetComponentsInChildren<Text>(true);
  81. for (int i = 0; i < texts.Length; i++)
  82. {
  83. texts[i].font = s_MainFont;
  84. if (!string.IsNullOrEmpty(texts[i].text))
  85. {
  86. texts[i].text = GameEntry.Localization.GetString(texts[i].text);
  87. }
  88. }
  89. }
  90. #if UNITY_2017_3_OR_NEWER
  91. protected override void OnRecycle()
  92. #else
  93. protected internal override void OnRecycle()
  94. #endif
  95. {
  96. base.OnRecycle();
  97. }
  98. #if UNITY_2017_3_OR_NEWER
  99. protected override void OnOpen(object userData)
  100. #else
  101. protected internal override void OnOpen(object userData)
  102. #endif
  103. {
  104. base.OnOpen(userData);
  105. m_CanvasGroup.alpha = 0f;
  106. StopAllCoroutines();
  107. StartCoroutine(m_CanvasGroup.FadeToAlpha(1f, FadeTime));
  108. }
  109. #if UNITY_2017_3_OR_NEWER
  110. protected override void OnClose(bool isShutdown, object userData)
  111. #else
  112. protected internal override void OnClose(bool isShutdown, object userData)
  113. #endif
  114. {
  115. base.OnClose(isShutdown, userData);
  116. }
  117. #if UNITY_2017_3_OR_NEWER
  118. protected override void OnPause()
  119. #else
  120. protected internal override void OnPause()
  121. #endif
  122. {
  123. base.OnPause();
  124. }
  125. #if UNITY_2017_3_OR_NEWER
  126. protected override void OnResume()
  127. #else
  128. protected internal override void OnResume()
  129. #endif
  130. {
  131. base.OnResume();
  132. m_CanvasGroup.alpha = 0f;
  133. StopAllCoroutines();
  134. StartCoroutine(m_CanvasGroup.FadeToAlpha(1f, FadeTime));
  135. }
  136. #if UNITY_2017_3_OR_NEWER
  137. protected override void OnCover()
  138. #else
  139. protected internal override void OnCover()
  140. #endif
  141. {
  142. base.OnCover();
  143. }
  144. #if UNITY_2017_3_OR_NEWER
  145. protected override void OnReveal()
  146. #else
  147. protected internal override void OnReveal()
  148. #endif
  149. {
  150. base.OnReveal();
  151. }
  152. #if UNITY_2017_3_OR_NEWER
  153. protected override void OnRefocus(object userData)
  154. #else
  155. protected internal override void OnRefocus(object userData)
  156. #endif
  157. {
  158. base.OnRefocus(userData);
  159. }
  160. #if UNITY_2017_3_OR_NEWER
  161. protected override void OnUpdate(float elapseSeconds, float realElapseSeconds)
  162. #else
  163. protected internal override void OnUpdate(float elapseSeconds, float realElapseSeconds)
  164. #endif
  165. {
  166. base.OnUpdate(elapseSeconds, realElapseSeconds);
  167. }
  168. #if UNITY_2017_3_OR_NEWER
  169. protected override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
  170. #else
  171. protected internal override void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
  172. #endif
  173. {
  174. int oldDepth = Depth;
  175. base.OnDepthChanged(uiGroupDepth, depthInUIGroup);
  176. int deltaDepth = UGuiGroupHelper.DepthFactor * uiGroupDepth + DepthFactor * depthInUIGroup - oldDepth + OriginalDepth;
  177. GetComponentsInChildren(true, m_CachedCanvasContainer);
  178. for (int i = 0; i < m_CachedCanvasContainer.Count; i++)
  179. {
  180. m_CachedCanvasContainer[i].sortingOrder += deltaDepth;
  181. }
  182. m_CachedCanvasContainer.Clear();
  183. }
  184. private IEnumerator CloseCo(float duration)
  185. {
  186. yield return m_CanvasGroup.FadeToAlpha(0f, duration);
  187. GameEntry.UI.CloseUIForm(this);
  188. }
  189. }
  190. }