UIModule.cs 6.1 KB

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