YIUILoopScrollChildSystem.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using YIUIFramework;
  6. namespace ET.Client
  7. {
  8. /// <summary>
  9. /// 无限循环列表 (异步)
  10. /// 文档: https://lib9kmxvq7k.feishu.cn/wiki/HPbwwkhsKi9aDik5VEXcqPhDnIh
  11. /// </summary>
  12. [FriendOf(typeof(YIUILoopScrollChild))]
  13. [EntitySystemOf(typeof(YIUILoopScrollChild))]
  14. public static partial class YIUILoopScrollChildSystem
  15. {
  16. #region ObjectSystem
  17. [EntitySystem]
  18. private static void Awake(this YIUILoopScrollChild self)
  19. {
  20. self.m_OwnerEntity = self.GetParent<Entity>();
  21. self.AwakePreLoad();
  22. }
  23. [EntitySystem]
  24. private static void Awake(this YIUILoopScrollChild self, LoopScrollRect owner, Type itemType)
  25. {
  26. self.m_OwnerEntity = self.GetParent<Entity>();
  27. self.Initialize(owner, itemType);
  28. self.AwakePreLoad();
  29. }
  30. [EntitySystem]
  31. private static void Awake(this YIUILoopScrollChild self, LoopScrollRect owner, Type itemType, string itemClickEventName)
  32. {
  33. self.m_OwnerEntity = self.GetParent<Entity>();
  34. self.Initialize(owner, itemType);
  35. self.SetOnClick(itemClickEventName);
  36. self.AwakePreLoad();
  37. }
  38. [EntitySystem]
  39. private static void Destroy(this YIUILoopScrollChild self)
  40. {
  41. self.m_ItemPool?.Clear((obj) => { ((Entity)obj)?.Parent?.Dispose(); });
  42. foreach (var code in self.m_BanLayerOptionForeverHashSet)
  43. {
  44. self.YIUIMgr()?.RecoverLayerOptionForever(code);
  45. }
  46. }
  47. #endregion
  48. #region Private
  49. public static void Initialize(this YIUILoopScrollChild self, LoopScrollRect owner, Type itemType)
  50. {
  51. var data = self.YIUIBind().GetBindVoByType(itemType);
  52. if (data == null) return;
  53. self.m_Owner = owner;
  54. self.m_ItemType = itemType;
  55. self.m_ItemTransformDic.Clear();
  56. self.m_ItemTransformIndexDic.Clear();
  57. self.m_BindVo = data.Value;
  58. self.m_ItemPool = new(self, self.OnCreateItemRenderer);
  59. self.m_Owner.prefabSource = self;
  60. self.m_Owner.dataSource = self;
  61. self.InitClearContent();
  62. self.InitCacheParent();
  63. self.m_InvokeLoadInstantiate = new YIUIInvokeEntity_LoadInstantiateByVo
  64. {
  65. BindVo = self.m_BindVo,
  66. ParentEntity = self,
  67. ParentTransform = self.CacheRect,
  68. };
  69. }
  70. private static void InitCacheParent(this YIUILoopScrollChild self)
  71. {
  72. if (self.m_Owner.u_CacheRect != null)
  73. {
  74. self.m_Owner.u_CacheRect.gameObject.SetActive(false);
  75. }
  76. else
  77. {
  78. var cacheObj = new GameObject("Cache");
  79. var cacheRect = cacheObj.GetOrAddComponent<RectTransform>();
  80. self.m_Owner.u_CacheRect = cacheRect;
  81. cacheRect.SetParent(self.m_Owner.transform, false);
  82. cacheObj.SetActive(false);
  83. }
  84. }
  85. //不应该初始化时有内容 所有不管是什么全部摧毁
  86. private static void InitClearContent(this YIUILoopScrollChild self)
  87. {
  88. var count = self.Content.childCount;
  89. for (var i = 0; i < count; i++)
  90. {
  91. var child = self.Content.GetChild(0);
  92. child.gameObject.SafeDestroySelf();
  93. }
  94. }
  95. private static Entity GetItemRendererByDic(this YIUILoopScrollChild self, Transform tsf)
  96. {
  97. if (self.m_ItemTransformDic.TryGetValue(tsf, out EntityRef<Entity> value))
  98. {
  99. return value;
  100. }
  101. Debug.LogError($"{tsf.name} 没找到这个关联对象 请检查错误");
  102. return null;
  103. }
  104. private static void AddItemRendererByDic(this YIUILoopScrollChild self, Transform tsf, Entity item)
  105. {
  106. self.m_ItemTransformDic.TryAdd(tsf, item);
  107. }
  108. private static int GetItemIndex(this YIUILoopScrollChild self, Transform tsf)
  109. {
  110. return self.m_ItemTransformIndexDic.GetValueOrDefault(tsf, -1);
  111. }
  112. private static void ResetItemIndex(this YIUILoopScrollChild self, Transform tsf, int index)
  113. {
  114. self.m_ItemTransformIndexDic[tsf] = index;
  115. }
  116. #endregion
  117. #region LoopScrollRect Interface
  118. private static async ETTask<EntityRef<Entity>> OnCreateItemRenderer(this YIUILoopScrollChild self)
  119. {
  120. EntityRef<YIUILoopScrollChild> selfRef = self;
  121. var item = await EventSystem.Instance?.YIUIInvokeEntityAsync<YIUIInvokeEntity_LoadInstantiateByVo, ETTask<Entity>>(self, self.m_InvokeLoadInstantiate);
  122. self = selfRef;
  123. if (item == null)
  124. {
  125. Log.Error($"YIUILoopScroll 实例化失败 请检查 {self.m_BindVo.PkgName} {self.m_BindVo.ResName}");
  126. return null;
  127. }
  128. var ownerRectTransform = item.GetParent<YIUIChild>().OwnerRectTransform;
  129. #if UNITY_EDITOR
  130. if (!self.m_FirstCheckLayoutElement)
  131. {
  132. var layoutElement = ownerRectTransform.GetComponent<ILayoutElement>();
  133. if (layoutElement == null)
  134. {
  135. var layoutGroup = ownerRectTransform.GetComponent<ILayoutGroup>();
  136. if (layoutGroup == null)
  137. {
  138. Debug.LogError($"{ownerRectTransform.name} 没有LayoutElement组件 请检查错误");
  139. }
  140. }
  141. self.m_FirstCheckLayoutElement = true;
  142. }
  143. #endif
  144. self.AddItemRendererByDic(ownerRectTransform, item);
  145. self.AddOnClickEvent(item);
  146. return item;
  147. }
  148. private static async ETTask<GameObject> GetObject(this YIUILoopScrollChild self, int index)
  149. {
  150. var item = await self.m_ItemPool.Get();
  151. return ((Entity)item)?.GetParent<YIUIChild>()?.OwnerGameObject;
  152. }
  153. private static void ReturnObject(this YIUILoopScrollChild self, Transform transform)
  154. {
  155. var item = self.GetItemRendererByDic(transform);
  156. if (item == null) return;
  157. self.m_ItemPool.Put(item);
  158. self.ResetItemIndex(transform, -1);
  159. transform.SetParent(self.m_Owner.u_CacheRect, false);
  160. }
  161. //初始化后预加载指定数量的实例
  162. //适用于部分情况下需要提前加载实例 减少卡顿
  163. //但是你本来就是打开后马上就刷新列表 那就没必要预加载了
  164. private static void AwakePreLoad(this YIUILoopScrollChild self)
  165. {
  166. if (self.PreLoadCount <= 0) return;
  167. self.PreLoadAsync(self.PreLoadCount).NoContext();
  168. }
  169. public static async ETTask PreLoadAsync(this YIUILoopScrollChild self, int count)
  170. {
  171. EntityRef<YIUILoopScrollChild> selfRef = self;
  172. using var coroutineLock = await self.Root().GetComponent<CoroutineLockComponent>().Wait(CoroutineLockType.YIUIFramework, self.GetHashCode());
  173. self = selfRef;
  174. var loadCount = count - self.m_ItemPool.Count;
  175. if (loadCount <= 0) return;
  176. using var listTemp = ListComponent<EntityRef<Entity>>.Create();
  177. for (var i = 0; i < loadCount; i++)
  178. {
  179. self = selfRef;
  180. var item = await self.m_ItemPool.Get();
  181. var transform = ((Entity)item)?.GetParent<YIUIChild>()?.OwnerRectTransform;
  182. if (transform != null)
  183. {
  184. self = selfRef;
  185. transform.SetParent(self.m_Owner.u_CacheRect, false);
  186. listTemp.Add(item);
  187. }
  188. }
  189. foreach (var item in listTemp)
  190. {
  191. self.m_ItemPool.Put(item);
  192. }
  193. }
  194. private static void ProvideData(this YIUILoopScrollChild self, Transform transform, int index)
  195. {
  196. var item = self.GetItemRendererByDic(transform);
  197. if (item == null) return;
  198. self.ResetItemIndex(transform, index);
  199. var select = self.m_OnClickItemHashSet.Contains(index);
  200. if (self.Data == null)
  201. {
  202. Debug.LogError($"{self.Parent.GetType().Name} {self.m_Owner.name}当前没有设定数据 m_Data == null");
  203. return;
  204. }
  205. YIUILoopHelper.Renderer(self.m_LoopRendererSystemType, self.OwnerEntity, item, self.Data[index], index, select);
  206. }
  207. //原地刷新 重新触发一次可见的Item 时候数据变化 但是长度不变
  208. //又不想全刷新也不想改变当前滑动位置,选中状态等等, 纯只刷新状态用
  209. private static void UpdateRenderer(this YIUILoopScrollChild self, int index)
  210. {
  211. var item = self.GetItemByIndex(index);
  212. if (item == null)
  213. {
  214. Debug.LogError($"没有找到 {index} 对应的item");
  215. return;
  216. }
  217. var select = self.m_OnClickItemHashSet.Contains(index);
  218. if (self.Data == null)
  219. {
  220. Debug.LogError($"{self.Parent.GetType().Name} {self.m_Owner.name}当前没有设定数据 m_Data == null");
  221. return;
  222. }
  223. YIUILoopHelper.Renderer(self.m_LoopRendererSystemType, self.OwnerEntity, item, self.Data[index], index, select);
  224. }
  225. #endregion
  226. #region EntitySystem
  227. [EntitySystem]
  228. public class YIUILoopScrollPrefabAsyncSource : YIUILoopScrollPrefabAsyncSourceSystem<YIUILoopScrollChild>
  229. {
  230. protected override async ETTask<GameObject> GetObject(YIUILoopScrollChild self, int index)
  231. {
  232. return await self.GetObject(index);
  233. }
  234. protected override void ReturnObject(YIUILoopScrollChild self, Transform trans)
  235. {
  236. self.ReturnObject(trans);
  237. }
  238. }
  239. [EntitySystem]
  240. public class YIUILoopScrollDataSource : YIUILoopScrollDataSourceSystem<YIUILoopScrollChild>
  241. {
  242. protected override void ProvideData(YIUILoopScrollChild self, Transform transform, int idx)
  243. {
  244. self.ProvideData(transform, idx);
  245. }
  246. }
  247. #endregion
  248. }
  249. }