LoopGridItemPool.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SuperScrollView
  5. {
  6. public class GridItemPool
  7. {
  8. GameObject mPrefabObj;
  9. string mPrefabName;
  10. int mInitCreateCount = 1;
  11. List<LoopGridViewItem> mTmpPooledItemList = new List<LoopGridViewItem>();
  12. List<LoopGridViewItem> mPooledItemList = new List<LoopGridViewItem>();
  13. static int mCurItemIdCount = 0;
  14. RectTransform mItemParent = null;
  15. public GridItemPool()
  16. {
  17. }
  18. public void Init(GameObject prefabObj, int createCount, RectTransform parent)
  19. {
  20. mPrefabObj = prefabObj;
  21. mPrefabName = mPrefabObj.name;
  22. mInitCreateCount = createCount;
  23. mItemParent = parent;
  24. mPrefabObj.SetActive(false);
  25. for (int i = 0; i < mInitCreateCount; ++i)
  26. {
  27. LoopGridViewItem tViewItem = CreateItem();
  28. RecycleItemReal(tViewItem);
  29. }
  30. }
  31. public LoopGridViewItem GetItem()
  32. {
  33. mCurItemIdCount++;
  34. LoopGridViewItem tItem = null;
  35. if (mTmpPooledItemList.Count > 0)
  36. {
  37. int count = mTmpPooledItemList.Count;
  38. tItem = mTmpPooledItemList[count - 1];
  39. mTmpPooledItemList.RemoveAt(count - 1);
  40. tItem.gameObject.SetActive(true);
  41. }
  42. else
  43. {
  44. int count = mPooledItemList.Count;
  45. if (count == 0)
  46. {
  47. tItem = CreateItem();
  48. }
  49. else
  50. {
  51. tItem = mPooledItemList[count - 1];
  52. mPooledItemList.RemoveAt(count - 1);
  53. tItem.gameObject.SetActive(true);
  54. }
  55. }
  56. tItem.ItemId = mCurItemIdCount;
  57. return tItem;
  58. }
  59. public void DestroyAllItem()
  60. {
  61. ClearTmpRecycledItem();
  62. int count = mPooledItemList.Count;
  63. for (int i = 0; i < count; ++i)
  64. {
  65. GameObject.DestroyImmediate(mPooledItemList[i].gameObject);
  66. }
  67. mPooledItemList.Clear();
  68. }
  69. public LoopGridViewItem CreateItem()
  70. {
  71. GameObject go = GameObject.Instantiate<GameObject>(mPrefabObj, Vector3.zero, Quaternion.identity, mItemParent);
  72. go.SetActive(true);
  73. RectTransform rf = go.GetComponent<RectTransform>();
  74. rf.localScale = Vector3.one;
  75. rf.anchoredPosition3D = Vector3.zero;
  76. rf.localEulerAngles = Vector3.zero;
  77. LoopGridViewItem tViewItem = go.GetComponent<LoopGridViewItem>();
  78. tViewItem.ItemPrefabName = mPrefabName;
  79. return tViewItem;
  80. }
  81. void RecycleItemReal(LoopGridViewItem item)
  82. {
  83. item.gameObject.SetActive(false);
  84. mPooledItemList.Add(item);
  85. }
  86. public void RecycleItem(LoopGridViewItem item)
  87. {
  88. item.PrevItem = null;
  89. item.NextItem = null;
  90. mTmpPooledItemList.Add(item);
  91. }
  92. public void ClearTmpRecycledItem()
  93. {
  94. int count = mTmpPooledItemList.Count;
  95. if (count == 0)
  96. {
  97. return;
  98. }
  99. for (int i = 0; i < count; ++i)
  100. {
  101. RecycleItemReal(mTmpPooledItemList[i]);
  102. }
  103. mTmpPooledItemList.Clear();
  104. }
  105. }
  106. }