/// <summary> /// 获取一个 Item /// </summary> /// <param name="prefabPath"></param> /// <returns></returns> private DYDataGridItem GetDYDataGridItemByPrefabPath(string prefabPath) { if (!itemPool.ContainsKey(prefabPath)) { Queue <DYDataGridItem> newQueue = new Queue <DYDataGridItem>(); itemPool.Add(prefabPath, newQueue); } Queue <DYDataGridItem> itemQueue = itemPool[prefabPath]; DYDataGridItem item = null; if (itemQueue.Count > 0) { item = itemQueue.Dequeue(); } else { GameObject obj = Instantiate(Resources.Load(prefabPath)) as GameObject; obj.name = obj.name.Replace("(Clone)", ""); item = obj.GetComponent <DYDataGridItem>(); if (item == null) { Debug.LogError("DYDataGridItem 为 NULL"); } } if (item != null) { item.gameObject.SetActive(true); } return(item); }
public void OnDestroy() { foreach (KeyValuePair <DYData, DYDataGridItem> kv in activeDataGridItem) { if (kv.Value.gameObject != null) { GameObject.Destroy(kv.Value.gameObject); } } activeDataGridItem.Clear(); activeDataGridItem = null; foreach (KeyValuePair <string, Queue <DYDataGridItem> > kv in itemPool) { while (kv.Value.Count > 0) { DYDataGridItem item = kv.Value.Dequeue(); if (item.gameObject != null) { GameObject.Destroy(item); } } } itemPool.Clear(); itemPool = null; }
/// <summary> /// 缓存一个 Item /// </summary> /// <param name="prefabPath"></param> /// <param name="item"></param> private void CacheDYDataGridItem(string prefabPath, DYDataGridItem item) { if (item.gameObject != null) { item.gameObject.SetActive(false); } itemPool[prefabPath].Enqueue(item); }
/// <summary> /// 渲染一个 Data /// </summary> /// <param name="data"></param> private void RenderItem(DYData data) { DYDataGridItem item = GetDYDataGridItemByPrefabPath(data.prefabPath); item.rectTrans.SetParent(this.content); item.rectTrans.anchoredPosition3D = data.itemPos; item.rectTrans.localScale = Vector3.one; item.data = data; item.Render(); activeDataGridItem.Add(data, item); }
/// <summary> /// 回收一个 Data Item /// </summary> /// <param name="data"></param> private void RecoupItem(DYData data) { DYDataGridItem item = null; activeDataGridItem.TryGetValue(data, out item); if (item != null) { activeDataGridItem.Remove(data); item.Release(); CacheDYDataGridItem(data.prefabPath, item); } else { Debug.LogError("回收数据出错,无法在活动的 Data 中找到对应的 Item "); } }