UIModule.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XGame.Framework.Asset;
  5. using XGame.Framework.Asyncs;
  6. using XGame.Framework.Interfaces;
  7. namespace XGame.Framework.UI
  8. {
  9. public class UIModule : IUIModule, IUpdate, ILateUpdate, IUIContextSetter, IDisposable, IUIAssetModuleCreator
  10. {
  11. private UIContext _context;
  12. private IUITree _uitree;
  13. private IAssetModule _assetModule;
  14. /// <summary>
  15. /// 加载中的
  16. /// </summary>
  17. private Dictionary<string, IAsync> _loadingMap;
  18. /// <summary>
  19. /// 已打开的UI
  20. /// </summary>
  21. private Dictionary<string, IUIView> _openedMap;
  22. /// <summary>
  23. /// 已关闭的UI
  24. /// </summary>
  25. private Dictionary<string, IUIView> _closedMap;
  26. /// <summary>
  27. /// Update 和 LateUpdate用
  28. /// </summary>
  29. private List<IUIView> _updates;
  30. public UIModule(IAssetModule assetModule, IUITree uitree)
  31. {
  32. _assetModule = assetModule;
  33. _uitree = uitree;
  34. _loadingMap = new Dictionary<string, IAsync>();
  35. _openedMap = new Dictionary<string, IUIView>();
  36. _closedMap = new Dictionary<string, IUIView>();
  37. _updates = new List<IUIView>();
  38. }
  39. public UIContext Context { get => _context; set => _context = value; }
  40. #region 接口实现
  41. Camera IUIModule.Camera => _uitree.Camera;
  42. Canvas IUIModule.GetCanvas(UILayer layer)
  43. {
  44. return _uitree.GetCanvas(layer);
  45. }
  46. IAsync IUIModule.OpenAsync(UIKey uikey, object intent)
  47. {
  48. string key = uikey;
  49. if (_loadingMap.ContainsKey(key))
  50. {
  51. return _loadingMap[key];
  52. }
  53. var asyncGroup = new AsyncGroup();
  54. if (_openedMap.ContainsKey(key))
  55. {
  56. Log.Warn($"重复开启UI:{key}");
  57. }
  58. else if (_closedMap.TryGetValue(key, out var closedView))
  59. { // 已关闭的UI
  60. asyncGroup.On(_ =>
  61. {
  62. _closedMap.Remove(key);
  63. _uitree.SetAsLastSibling(closedView.Panel);
  64. closedView.Enable(intent);
  65. _openedMap.Add(key, closedView);
  66. });
  67. }
  68. else
  69. {
  70. _loadingMap.Add(key, asyncGroup);
  71. var loadAsync = _assetModule.LoadAsync<UIPanel>(key);
  72. loadAsync.Join(asyncGroup);
  73. loadAsync.On(_ =>
  74. {
  75. _loadingMap.Remove(key);
  76. var panel = loadAsync.Result;
  77. if (panel == null)
  78. {
  79. Log.Error($"UI加载结果为空. UIKey:{key}");
  80. return;
  81. }
  82. var view = Activator.CreateInstance(uikey.UIViewType) as IUIView;
  83. var context = _context.Clone();
  84. (context as IUIViewAdapter).Key = uikey;
  85. (view as UIView).Init(context, panel);
  86. _uitree.AddPanel(panel);
  87. view.Enable(intent);
  88. _openedMap.Add(key, view);
  89. });
  90. }
  91. asyncGroup.End();
  92. return asyncGroup;
  93. }
  94. void IUIModule.Close(UIKey uikey, bool isDestroy)
  95. {
  96. var key = uikey.Key;
  97. if (_loadingMap.TryGetValue(key, out var async))
  98. {
  99. _loadingMap.Remove(key);
  100. async.RemoveAll();
  101. return;
  102. }
  103. if (_openedMap.TryGetValue(key, out var uiView))
  104. {
  105. _openedMap.Remove(key);
  106. uiView.Disable();
  107. if (isDestroy)
  108. {
  109. DestroyUIView(uiView);
  110. }
  111. else
  112. {
  113. _closedMap.Add(key, uiView);
  114. }
  115. return;
  116. }
  117. if (isDestroy && _closedMap.TryGetValue(key, out uiView))
  118. {
  119. _closedMap.Remove(key);
  120. DestroyUIView(uiView);
  121. }
  122. }
  123. IAsync IUIModule.Preload(UIKey uikey)
  124. {
  125. var loadAsync = _assetModule.PreloadAsync<UIPanel>(uikey);
  126. return loadAsync;
  127. }
  128. void IUpdate.Update(int millisecond)
  129. {
  130. if (_openedMap.Count == 0)
  131. return;
  132. _updates.Clear();
  133. _updates.AddRange(_openedMap.Values);
  134. for(var i = 0; i < _updates.Count;)
  135. {
  136. var view = _updates[i];
  137. view.Update(millisecond);
  138. if (view.Active)
  139. {
  140. i++;
  141. }
  142. else
  143. {
  144. _updates.RemoveAt(i);
  145. }
  146. }
  147. }
  148. void ILateUpdate.LateUpdate(int millisecond)
  149. {
  150. if (_updates.Count == 0)
  151. return;
  152. foreach (var uiview in _updates)
  153. {
  154. uiview.LateUpdate(millisecond);
  155. }
  156. _updates.Clear();
  157. }
  158. void IDisposable.Dispose()
  159. {
  160. _updates.Clear();
  161. foreach (var async in _loadingMap.Values)
  162. {
  163. async.RemoveAll();
  164. }
  165. _loadingMap.Clear();
  166. foreach (var uiview in _closedMap.Values)
  167. {
  168. DestroyUIView(uiview);
  169. }
  170. _closedMap.Clear();
  171. foreach (var uiview in _openedMap.Values)
  172. {
  173. DestroyUIView(uiview);
  174. }
  175. _openedMap.Clear();
  176. (_uitree as IDisposable)?.Dispose();
  177. (_assetModule as IDisposable)?.Dispose();
  178. _assetModule = null;
  179. _context = null;
  180. }
  181. IUIAssetModule IUIAssetModuleCreator.Create(UIContext context)
  182. {
  183. return new UIAssetModule(context);
  184. }
  185. #endregion
  186. #region 内部函数
  187. private void DestroyUIView(IUIView view)
  188. {
  189. var uiPanel = view.Panel as UIPanel;
  190. (view as IDisposable).Dispose();
  191. _assetModule.Recycle(uiPanel.gameObject, true);
  192. }
  193. #endregion
  194. }
  195. }