| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace SuperScrollView
- {
- public class GridItemPool
- {
- GameObject mPrefabObj;
- string mPrefabName;
- int mInitCreateCount = 1;
- List<LoopGridViewItem> mTmpPooledItemList = new List<LoopGridViewItem>();
- List<LoopGridViewItem> mPooledItemList = new List<LoopGridViewItem>();
- static int mCurItemIdCount = 0;
- RectTransform mItemParent = null;
- public GridItemPool()
- {
- }
- public void Init(GameObject prefabObj, int createCount, RectTransform parent)
- {
- mPrefabObj = prefabObj;
- mPrefabName = mPrefabObj.name;
- mInitCreateCount = createCount;
- mItemParent = parent;
- mPrefabObj.SetActive(false);
- for (int i = 0; i < mInitCreateCount; ++i)
- {
- LoopGridViewItem tViewItem = CreateItem();
- RecycleItemReal(tViewItem);
- }
- }
- public LoopGridViewItem GetItem()
- {
- mCurItemIdCount++;
- LoopGridViewItem tItem = null;
- if (mTmpPooledItemList.Count > 0)
- {
- int count = mTmpPooledItemList.Count;
- tItem = mTmpPooledItemList[count - 1];
- mTmpPooledItemList.RemoveAt(count - 1);
- tItem.gameObject.SetActive(true);
- }
- else
- {
- int count = mPooledItemList.Count;
- if (count == 0)
- {
- tItem = CreateItem();
- }
- else
- {
- tItem = mPooledItemList[count - 1];
- mPooledItemList.RemoveAt(count - 1);
- tItem.gameObject.SetActive(true);
- }
- }
- tItem.ItemId = mCurItemIdCount;
- return tItem;
- }
- public void DestroyAllItem()
- {
- ClearTmpRecycledItem();
- int count = mPooledItemList.Count;
- for (int i = 0; i < count; ++i)
- {
- GameObject.DestroyImmediate(mPooledItemList[i].gameObject);
- }
- mPooledItemList.Clear();
- }
- public LoopGridViewItem CreateItem()
- {
- GameObject go = GameObject.Instantiate<GameObject>(mPrefabObj, Vector3.zero, Quaternion.identity, mItemParent);
- go.SetActive(true);
- RectTransform rf = go.GetComponent<RectTransform>();
- rf.localScale = Vector3.one;
- rf.anchoredPosition3D = Vector3.zero;
- rf.localEulerAngles = Vector3.zero;
- LoopGridViewItem tViewItem = go.GetComponent<LoopGridViewItem>();
- tViewItem.ItemPrefabName = mPrefabName;
- return tViewItem;
- }
- void RecycleItemReal(LoopGridViewItem item)
- {
- item.gameObject.SetActive(false);
- mPooledItemList.Add(item);
- }
- public void RecycleItem(LoopGridViewItem item)
- {
- item.PrevItem = null;
- item.NextItem = null;
- mTmpPooledItemList.Add(item);
- }
- public void ClearTmpRecycledItem()
- {
- int count = mTmpPooledItemList.Count;
- if (count == 0)
- {
- return;
- }
- for (int i = 0; i < count; ++i)
- {
- RecycleItemReal(mTmpPooledItemList[i]);
- }
- mTmpPooledItemList.Clear();
- }
- }
- }
|