Esempio n. 1
0
        /// <summary>
        /// 从缓存池中得到指定类型的组件
        /// </summary>
        /// <typeparam name="T">继承于MonoBehaviour的组件</typeparam>
        /// <param name="isAutoActive">是否激获取到的GameObject,默认为true</param>
        /// <returns></returns>
        public T GetComponentItem <T>(bool isAutoActive = true, bool isAddIfNotFind = false) where T : MonoBehaviour
        {
            if (m_InstanceOrPrefabTemplate.GetComponent <T> () == null && !isAddIfNotFind)
            {
                return(null);
            }

            GameObject gObj      = GetPoolItem(isAutoActive);
            T          component = null;

            if (gObj != null)
            {
                component = gObj.GetComponent <T>();
                if (component == null)
                {
                    component = gObj.AddComponent <T>();
                    GameObjectPoolItem poolItem = component as GameObjectPoolItem;
                    if (poolItem != null)
                    {
                        poolItem.SpawnName = m_SpawnPool.PoolName;
                        poolItem.AssetPath = m_AssetPath;
                        poolItem.DoSpawned();
                    }
                }
            }

            return(component);
        }
Esempio n. 2
0
        /// <summary>
        /// 回收GameObject,如果此GameObject不带有GameObjectPoolItem组件,则无法回收到池中,将会直接删除
        /// </summary>
        /// <param name="item"></param>
        public void ReleasePoolItem(GameObject item)
        {
            if (item == null)
            {
                DebugUtility.LogError(LOG_TAG, "GameObjectPool::ReleaseItem->Item is Null");
                return;
            }

            GameObjectPoolItem pItem = item.GetComponent <GameObjectPoolItem>();

            if (pItem != null)
            {
                pItem.DoDespawned();
            }

            item.transform.SetParent(m_SpawnPool.CachedTransform, false);
            item.SetActive(false);
            m_UnusedItemQueue.Enqueue(item);

            for (int i = m_UsedItemList.Count - 1; i >= 0; i--)
            {
                if (m_UsedItemList[i].TryGetTarget(out GameObject target))
                {
                    if (!UnityObjectExtension.IsNull(target))
                    {
                        if (target != item)
                        {
                            continue;
                        }
                        else
                        {
                            m_UsedItemList.RemoveAt(i);
                            break;
                        }
                    }
                }

                m_UsedItemList.RemoveAt(i);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// 根据模板创建一个新对象
        /// </summary>
        /// <returns></returns>
        private GameObject CreateNewItem()
        {
            GameObject item = null;

            if (m_TemplateType == PoolTemplateType.RuntimeInstance)
            {
                item = GameObject.Instantiate(m_InstanceOrPrefabTemplate);
            }
            else
            {
                item = (GameObject)Loader.AssetManager.GetInstance().InstantiateAsset(m_AssetPath, m_InstanceOrPrefabTemplate);
            }

            if (item != null)
            {
                GameObjectPoolItem poolItem = item.GetComponent <GameObjectPoolItem>();
                if (poolItem != null)
                {
                    poolItem.AssetPath = m_AssetPath;
                    poolItem.SpawnName = m_SpawnPool.PoolName;
                }
            }
            return(item);
        }
Esempio n. 4
0
        /// <summary>
        /// 从缓存池中得到一个GameObject对象
        /// </summary>
        /// <param name="isAutoSetActive">是否激获取到的GameObject,默认为true</param>
        /// <returns></returns>
        public GameObject GetPoolItem(bool isAutoSetActive = true)
        {
            if (LimitMaxAmount != 0 && m_UsedItemList.Count > LimitMaxAmount)
            {
                DebugUtility.LogWarning(LOG_TAG, "GameObjectPool::GetItem->Large than Max Amount");
                return(null);
            }

            GameObject item = null;

            if (m_UnusedItemQueue.Count > 0)
            {
                item = m_UnusedItemQueue.Dequeue();
            }
            else
            {
                item = CreateNewItem();
            }

            if (item != null)
            {
                GameObjectPoolItem poolItem = item.GetComponent <GameObjectPoolItem>();
                if (poolItem != null)
                {
                    poolItem.DoSpawned();
                }
            }

            if (isAutoSetActive)
            {
                item.SetActive(true);
            }

            m_UsedItemList.Add(new WeakReference <GameObject>(item));
            return(item);
        }