Exemplo n.º 1
0
 /// <summary>
 /// 缓存池中将默认以资源的路径为唯一标识,通过资源唯一标识可以获致到对应的缓存池
 /// </summary>
 /// <param name="name"></param>
 /// <returns></returns>
 public PoolGroup GetGroup(string name)
 {
     if (itemGroupDic.TryGetValue(name, out PoolGroup itemGroup))
     {
         return(itemGroup);
     }
     PoolLogger.Warning(PoolUtill.LOG_TAG, "The pool is not found.name = " + name);
     return(null);
 }
Exemplo n.º 2
0
        /// <summary>
        /// 删除指定的缓存池
        /// </summary>
        /// <param name="name">资源唯一标签,一般使用资源路径</param>
        public void DeleteGroup(string name)
        {
            PoolGroup itemGroup = GetGroup(name);

            if (itemGroup != null)
            {
                itemGroupDic.Remove(name);
                itemGroup.DoDestroy();
            }
            else
            {
                PoolLogger.Warning(PoolUtill.LOG_TAG, "The pool is not found.name = " + name);
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// 使用给定的GameObject创建缓存池
        /// </summary>
        /// <param name="itemName">资源唯一标签,一般使用资源路径</param>
        /// <param name="itemTemplate">模板GameObject</param>
        /// <returns></returns>
        public PoolGroup CreateGroup(string itemName, TemplateType itemType, GameObject itemTemplate)
        {
            if (itemTemplate == null)
            {
                PoolLogger.Error(PoolUtill.LOG_TAG, "Template is Null");
                return(null);
            }

            if (!itemGroupDic.TryGetValue(itemName, out PoolGroup itemGroup))
            {
                itemGroup = new PoolGroup(Name, categoryTransform, itemName, itemType, itemTemplate);
                itemGroupDic.Add(itemName, itemGroup);
                return(itemGroup);
            }

            PoolLogger.Warning(PoolUtill.LOG_TAG, "The pool has been created.uniqueName = " + itemName);
            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 回收GameObject
        /// </summary>
        /// <param name="item"></param>
        public void ReleaseItem(GameObject item)
        {
            if (item == null)
            {
                PoolLogger.Error(PoolUtill.LOG_TAG, "GameObjectPool::ReleaseItem->Item is Null");
                return;
            }

            if (unusedItemQueue.Count > limitMaxAmount)
            {
                UnityObject.Destroy(item);
                return;
            }

            item.transform.SetParent(groupTransform, false);
            item.SetActive(false);
            unusedItemQueue.Enqueue(item);

            if (PoolUtill.IsInDebug)
            {
                //从使用列表中删除要回收的对象
                for (int i = usedItemList.Count - 1; i >= 0; i--)
                {
                    if (usedItemList[i].TryGetTarget(out GameObject target) && !target.IsNull())
                    {
                        if (target != item)
                        {
                            continue;
                        }
                        else
                        {
                            usedItemList.RemoveAt(i);
                            break;
                        }
                    }
                    else
                    {
                        usedItemList.RemoveAt(i);
                    }
                }