Пример #1
0
        /// <summary>
        /// 可以通过此函数完成资源回收到池中
        /// </summary>
        public void ReleaseItem()
        {
            if (string.IsNullOrEmpty(AssetPath) || string.IsNullOrEmpty(SpawnName))
            {
                Destroy(CachedGameObject);
                return;
            }
            if (!PoolManager.GetInstance().HasSpawnPool(SpawnName))
            {
                SafeDestroy = true;

                Destroy(CachedGameObject);
                return;
            }
            SpawnPool      spawnPool = PoolManager.GetInstance().GetSpawnPool(SpawnName);
            GameObjectPool gObjPool  = spawnPool.GetGameObjectPool(AssetPath);

            if (gObjPool == null)
            {
                SafeDestroy = true;
                Destroy(CachedGameObject);
                return;
            }
            gObjPool.ReleasePoolItem(CachedGameObject);
        }
Пример #2
0
        /// <summary>
        /// 加载完成的回调
        /// </summary>
        /// <param name="assetPath"></param>
        /// <param name="uObj"></param>
        /// <param name="userData"></param>
        private void OnLoadComplete(string assetPath, UnityObject uObj, System.Object userData)
        {
            PoolData poolData = userData as PoolData;

            if (!m_PoolDatas.Contains(poolData))
            {
                return;
            }

            m_PoolDatas.Remove(poolData);

            if (uObj is GameObject templateGO)
            {
                SpawnPool spawnPool = GetSpawnPool(poolData.SpawnName);
                if (spawnPool != null)
                {
                    GameObjectPool objPool = spawnPool.CreateGameObjectPool(poolData.AssetPath, templateGO);
                    objPool.IsAutoClean        = poolData.IsAutoClean;
                    objPool.PreloadTotalAmount = poolData.PreloadTotalAmount;
                    objPool.PreloadOnceAmount  = poolData.PreloadOnceAmount;
                    objPool.completeCallback   = poolData.CompleteCallback;
                    objPool.IsCull             = poolData.IsCull;
                    objPool.CullOnceAmount     = poolData.CullOnceAmount;
                    objPool.CullDelayTime      = poolData.CullDelayTime;
                    objPool.LimitMaxAmount     = poolData.LimitMaxAmount;
                    objPool.LimitMinAmount     = poolData.LimitMinAmount;
                }
            }
        }
Пример #3
0
        /// <summary>
        /// 使用PoolData进行资源加载,资源加载完成后创建对应的缓存池
        /// </summary>
        /// <param name="poolData"></param>
        public void LoadAssetToCreateGameObjectPool(PoolData poolData)
        {
            SpawnPool spawnPool = GetSpawnPool(poolData.SpawnName);

            if (spawnPool == null)
            {
                CreateSpawnPool(poolData.SpawnName);
            }
            else
            {
                if (spawnPool.HasGameObjectPool(poolData.AssetPath))
                {
                    Debug.LogWarning("PoolManager::LoadAssetToCreateGameObjectPool->GameObjectPool has been created!");
                    return;
                }
            }

            for (int i = 0; i < m_PoolDatas.Count; i++)
            {
                PoolData pData = m_PoolDatas[i];
                if (pData.SpawnName == poolData.SpawnName && pData.AssetPath == poolData.AssetPath)
                {
                    Debug.LogError("PoolManager::CreateGameObjectPool->pool data has been added");
                    return;
                }
            }

            AssetLoaderHandle assetHandle = Loader.AssetManager.GetInstance().LoadAssetAsync(poolData.AssetPath, OnLoadComplete, AssetLoaderPriority.Default, null, poolData);

            poolData.LoaderHandle = assetHandle;
            m_PoolDatas.Add(poolData);
        }
Пример #4
0
        /// <summary>
        /// 销毁缓存池
        /// </summary>
        internal void DestroyPool()
        {
            completeCallback = null;
            if (m_PreloadTimerTask != null)
            {
                TimerManager.GetInstance().RemoveTimer(m_PreloadTimerTask);
                m_PreloadTimerTask = null;
            }

            m_UsedItemList.Clear();

            for (int i = m_UnusedItemQueue.Count - 1; i >= 0; i--)
            {
                UnityObject.Destroy(m_UnusedItemQueue.Dequeue());
            }
            m_UnusedItemQueue.Clear();

            if (m_TemplateType == PoolTemplateType.PrefabInstance || m_TemplateType == PoolTemplateType.RuntimeInstance)
            {
                UnityObject.Destroy(m_InstanceOrPrefabTemplate);
            }

            m_AssetPath = null;
            m_SpawnPool = null;
            m_InstanceOrPrefabTemplate = null;
            IsAutoClean = false;
        }
Пример #5
0
        /// <summary>
        /// 创建指定名称的分组
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public SpawnPool CreateSpawnPool(string name)
        {
            if (!m_SpawnDic.TryGetValue(name, out SpawnPool pool))
            {
                pool = new SpawnPool();
                pool.InitSpawn(name, m_CachedTransform);

                m_SpawnDic.Add(name, pool);
            }
            return(pool);
        }
Пример #6
0
        /// <summary>
        /// 初始化缓存池
        /// </summary>
        /// <param name="pool">所有的分组</param>
        /// <param name="aPath">唯一标识,一般以资源路径为值</param>
        /// <param name="templateGObj">模板</param>
        /// <param name="templateType">模板类型</param>
        internal void InitPool(SpawnPool pool, string aPath, GameObject templateGObj, PoolTemplateType templateType)
        {
            m_SpawnPool = pool;
            m_AssetPath = aPath;

            m_InstanceOrPrefabTemplate = templateGObj;
            this.m_TemplateType        = templateType;

            if (templateType != PoolTemplateType.Prefab)
            {
                m_InstanceOrPrefabTemplate.SetActive(false);
                m_InstanceOrPrefabTemplate.transform.SetParent(pool.CachedTransform, false);
            }

            m_PreloadTimerTask = TimerManager.GetInstance().AddIntervalTimer(0.05f, OnPreloadTimerUpdate);
        }