ReusingScrollRect.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. using System.Collections.Generic;
  5. using System;
  6. public class ReusingScrollRect : ScrollRectInput
  7. {
  8. public string m_ItemName = "";
  9. //默认是从上到下,从左到右,勾上此选项则反向
  10. public bool m_isInversion = false;
  11. //是否接受操作
  12. public bool m_isReceiveControl = true;
  13. public List<Dictionary<string, object>> m_datas = new List<Dictionary<string, object>>();
  14. public List<ReusingScrollItemBase> m_items = new List<ReusingScrollItemBase>();
  15. public List<ReusingScrollItemBase> m_itemCaches = new List<ReusingScrollItemBase>();
  16. //RectTransform m_rectTransform;
  17. private Bounds m_viewBounds;
  18. public GameObject m_itemPrefab;
  19. public Vector3 m_itemSize;
  20. #region 公共方法
  21. public void SetItem(string itemName)
  22. {
  23. m_ItemName = itemName;
  24. //m_rectTransform = GetComponent<RectTransform>();
  25. Rebuild(CanvasUpdate.Layout);
  26. UpdateBound();
  27. SetLayout();
  28. m_itemPrefab = GameObjectManager.CreateGameObjectByPool(m_ItemName);
  29. m_itemSize = m_itemPrefab.GetComponent<RectTransform>().sizeDelta;
  30. }
  31. public override void Dispose()
  32. {
  33. base.Dispose();
  34. for (int i = 0; i < m_items.Count; i++)
  35. {
  36. GameObjectManager.DestroyGameObjectByPool(m_items[i].gameObject);
  37. }
  38. m_items.Clear();
  39. for (int i = 0; i < m_itemCaches.Count; i++)
  40. {
  41. GameObjectManager.DestroyGameObjectByPool(m_itemCaches[i].gameObject);
  42. }
  43. m_itemCaches.Clear();
  44. }
  45. public void Refresh()
  46. {
  47. for (int i = 0; i < m_items.Count; i++)
  48. {
  49. m_items[i].SetContent(m_items[i].m_index, m_datas[m_items[i].m_index]);
  50. }
  51. SetItemDisplay();
  52. }
  53. public void SetData(List<Dictionary<string, object>> data)
  54. {
  55. m_datas = data;
  56. UpdateIndexList(data.Count);
  57. UpdateConetntSize(data.Count);
  58. SetItemDisplay();
  59. }
  60. public ReusingScrollItemBase GetItem(int index)
  61. {
  62. for (int i = 0; i < m_items.Count; i++)
  63. {
  64. if(m_items[i].m_index == index)
  65. {
  66. return m_items[i];
  67. }
  68. }
  69. return null;
  70. }
  71. public Vector3 GetItemAnchorPos(int index)
  72. {
  73. if (content == null)
  74. {
  75. throw new Exception("SetItemDisplay Exception: content is null !");
  76. }
  77. return GetItemPos(index) + GetRealItemOffset() + content.localPosition;
  78. }
  79. public void SetPos(Vector3 pos)
  80. {
  81. if (content == null)
  82. {
  83. throw new Exception("SetItemDisplay Exception: content is null !");
  84. }
  85. content.anchoredPosition3D = pos;
  86. SetItemDisplay();
  87. }
  88. #endregion
  89. #region 继承方法
  90. bool m_rebuild = false;
  91. public void Update()
  92. {
  93. if(m_rebuild)
  94. {
  95. m_rebuild = false;
  96. SetItemDisplay();
  97. }
  98. }
  99. protected override void OnSetContentAnchoredPosition(InputUIOnScrollEvent e)
  100. {
  101. base.OnSetContentAnchoredPosition(e);
  102. SetItemDisplay();
  103. }
  104. protected override void Start()
  105. {
  106. base.Start();
  107. SetItemDisplay();
  108. }
  109. public override void Rebuild(CanvasUpdate executing)
  110. {
  111. base.Rebuild(executing);
  112. UpdateBound();
  113. m_rebuild = true;
  114. }
  115. #endregion
  116. #region 私有方法
  117. void SetLayout()
  118. {
  119. content.anchorMin = GetMinAchors();
  120. content.anchorMax = GetMaxAchors();
  121. content.pivot = GetPivot();
  122. content.anchoredPosition3D = Vector3.zero;
  123. }
  124. void UpdateBound()
  125. {
  126. m_viewBounds = new Bounds(viewRect.rect.center, viewRect.rect.size);
  127. }
  128. void UpdateConetntSize(int count)
  129. {
  130. Vector3 size = m_itemSize;
  131. if (horizontal)
  132. {
  133. size.x *= count;
  134. size.y = 0;
  135. }
  136. if(vertical)
  137. {
  138. size.y *= count;
  139. size.x = 0;
  140. }
  141. content.sizeDelta = size;
  142. //Debug.Log(m_itemSize + "" + size + " m_rectTransform.sizeDelta " + m_rectTransform.sizeDelta, m_rectTransform );
  143. }
  144. void UpdateIndexList(int count)
  145. {
  146. m_indexList = new List<ReusingData>();
  147. for (int i = 0; i < count; i++)
  148. {
  149. ReusingData reusingTmp = null;
  150. if (m_indexList.Count > i)
  151. {
  152. reusingTmp = m_indexList[i];
  153. }
  154. else
  155. {
  156. reusingTmp = new ReusingData();
  157. m_indexList.Add(reusingTmp);
  158. }
  159. reusingTmp.index = i;
  160. reusingTmp.status = ReusingStatus.Hide;
  161. }
  162. }
  163. List<ReusingData> m_indexList = new List<ReusingData>();
  164. void SetItemDisplay(bool isRebuild = false)
  165. {
  166. if(content == null)
  167. {
  168. throw new Exception("SetItemDisplay Exception: content is null !");
  169. }
  170. //计算已显示的哪些需要隐藏
  171. for (int i = 0; i < m_items.Count; i++)
  172. {
  173. m_items[i].OnDrag();
  174. //已经完全离开了显示区域
  175. if (!m_viewBounds.Intersects(GetItemBounds(m_items[i].m_index)))
  176. {
  177. ReusingScrollItemBase itemTmp = m_items[i];
  178. m_items.Remove(itemTmp);
  179. itemTmp.OnHide();
  180. if (!isRebuild)
  181. {
  182. //隐藏并移到缓存
  183. itemTmp.gameObject.SetActive(false);
  184. }
  185. m_itemCaches.Add(itemTmp);
  186. m_indexList[itemTmp.m_index].status = ReusingStatus.Hide;
  187. }
  188. }
  189. //计算出哪些需要显示
  190. //如果有未显示的则显示出来,从对象池取出对象
  191. bool m_clip = false;
  192. for (int i = 0; i < m_indexList.Count; i++)
  193. {
  194. if (m_indexList[i].status == ReusingStatus.Hide)
  195. {
  196. if (m_viewBounds.Intersects(GetItemBounds(m_indexList[i].index)))
  197. {
  198. ShowItem(i, isRebuild, m_datas[i]);
  199. m_indexList[i].status = ReusingStatus.Show;
  200. m_clip = true;
  201. }
  202. else
  203. {
  204. if (m_clip)
  205. {
  206. break;
  207. }
  208. }
  209. }
  210. else
  211. {
  212. m_clip = true;
  213. }
  214. }
  215. }
  216. void ShowItem(int index,bool isRebuild,Dictionary<string, object> data)
  217. {
  218. ReusingScrollItemBase itemTmp = GetItem();
  219. itemTmp.transform.SetParent(content);
  220. itemTmp.transform.localScale = Vector3.one;
  221. if (!isRebuild)
  222. {
  223. itemTmp.SetContent(index, data);
  224. }
  225. itemTmp.RectTransform.pivot = GetPivot();
  226. itemTmp.RectTransform.anchorMin = GetMinAchors();
  227. itemTmp.RectTransform.anchorMax = GetMaxAchors();
  228. itemTmp.RectTransform.sizeDelta = GetItemSize();
  229. itemTmp.RectTransform.anchoredPosition3D = GetItemPos(index);
  230. itemTmp.m_index = index;
  231. }
  232. ReusingScrollItemBase GetItem()
  233. {
  234. ReusingScrollItemBase result = null;
  235. if (m_itemCaches.Count>0)
  236. {
  237. result = m_itemCaches[0];
  238. result.gameObject.SetActive(true);
  239. result.OnShow();
  240. m_itemCaches.RemoveAt(0);
  241. m_items.Add(result);
  242. return result;
  243. }
  244. result = GameObjectManager.CreateGameObjectByPool(m_itemPrefab).GetComponent<ReusingScrollItemBase>();
  245. result.Init(m_UIEventKey, m_items.Count);
  246. m_items.Add(result);
  247. return result;
  248. }
  249. Bounds GetItemBounds(int index)
  250. {
  251. return new Bounds(GetItemPos(index) + GetRealItemOffset() + content.localPosition, m_itemSize);
  252. }
  253. Vector3 GetItemPos(int index)
  254. {
  255. Vector3 offset = Vector3.zero;
  256. if (vertical)
  257. {
  258. offset = new Vector3(0, -m_itemSize.y, 0);
  259. }
  260. else
  261. {
  262. offset = new Vector3(m_itemSize.x, 0, 0);
  263. }
  264. if (m_isInversion)
  265. {
  266. offset *= -1;
  267. }
  268. offset *= index;
  269. return offset;
  270. }
  271. Vector3 GetRealItemOffset()
  272. {
  273. Vector3 offset;
  274. if (vertical)
  275. {
  276. offset = new Vector3(0, -m_itemSize.y / 2, 0);
  277. }
  278. else
  279. {
  280. offset = new Vector3(m_itemSize.x/2, 0, 0);
  281. }
  282. if(m_isInversion)
  283. {
  284. offset *= -1;
  285. }
  286. return offset;
  287. }
  288. Vector2 GetPivot()
  289. {
  290. Vector2 pivot = new Vector2(0.5f, 0.5f);
  291. if (horizontal)
  292. {
  293. if (!m_isInversion)
  294. {
  295. pivot.x = 0;
  296. }
  297. else
  298. {
  299. pivot.x = 1;
  300. }
  301. }
  302. if (vertical)
  303. {
  304. if (!m_isInversion)
  305. {
  306. pivot.y = 1;
  307. }
  308. else
  309. {
  310. pivot.y = 0;
  311. }
  312. }
  313. return pivot;
  314. }
  315. Vector2 GetMinAchors()
  316. {
  317. Vector2 minAchors = new Vector2(0.5f, 0.5f);
  318. if (horizontal)
  319. {
  320. if (!m_isInversion)
  321. {
  322. minAchors.x = 0;
  323. }
  324. else
  325. {
  326. minAchors.x = 1;
  327. }
  328. minAchors.y = 0;
  329. }
  330. if (vertical)
  331. {
  332. if (!m_isInversion)
  333. {
  334. minAchors.y = 1;
  335. }
  336. else
  337. {
  338. minAchors.y = 0;
  339. }
  340. minAchors.x = 0;
  341. }
  342. return minAchors;
  343. }
  344. Vector2 GetMaxAchors()
  345. {
  346. Vector2 maxAchors = new Vector2(0.5f, 0.5f);
  347. if (horizontal)
  348. {
  349. if (!m_isInversion)
  350. {
  351. maxAchors.x = 0;
  352. }
  353. else
  354. {
  355. maxAchors.x = 1;
  356. }
  357. maxAchors.y = 1;
  358. }
  359. if (vertical)
  360. {
  361. if (!m_isInversion)
  362. {
  363. maxAchors.y = 1;
  364. }
  365. else
  366. {
  367. maxAchors.y = 0;
  368. }
  369. maxAchors.x = 1;
  370. }
  371. return maxAchors;
  372. }
  373. Vector2 GetItemSize()
  374. {
  375. Vector3 size = m_itemSize;
  376. if (horizontal)
  377. {
  378. size.y = 0;
  379. }
  380. if (vertical)
  381. {
  382. size.x = 0;
  383. }
  384. return size;
  385. }
  386. //void OnDrawGizmos()
  387. //{
  388. // return;
  389. // Gizmos.color = Color.red;
  390. // Gizmos.DrawCube(m_viewBounds.center, m_viewBounds.size);
  391. // Gizmos.color = Color.green;
  392. // Gizmos.DrawCube(GetItemBounds(0).center, GetItemBounds(0).size);
  393. // Gizmos.color = new Color(1, 1, 0, 0.5f);
  394. // for (int i = 0; i < 100; i++)
  395. // {
  396. // Gizmos.color -= new Color(0.01f, 0, 0, 0);
  397. // Gizmos.DrawCube(GetItemBounds(i).center, GetItemBounds(i).size);
  398. // }
  399. //}
  400. #endregion
  401. #region 动画
  402. public void StartEnterAnim()
  403. {
  404. m_isReceiveControl = false;
  405. StartCoroutine(EnterAnim());
  406. }
  407. public void StartExitAnim()
  408. {
  409. m_isReceiveControl = false;
  410. StartCoroutine(ExitAnim());
  411. }
  412. void EndEnterAnim()
  413. {
  414. m_isReceiveControl = true;
  415. }
  416. void EndExitAnim()
  417. {
  418. m_isReceiveControl = true;
  419. }
  420. public virtual IEnumerator EnterAnim()
  421. {
  422. return null;
  423. }
  424. public virtual IEnumerator ExitAnim()
  425. {
  426. return null;
  427. }
  428. #endregion
  429. #region 私有类和枚举
  430. class ReusingData
  431. {
  432. public int index;
  433. public ReusingStatus status;
  434. }
  435. enum ReusingStatus
  436. {
  437. Show,
  438. Hide
  439. }
  440. #endregion
  441. }