Beispiel #1
0
        /// <summary>
        /// 初始化有戏物体对象池
        /// </summary>
        /// <param name="arr"></param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public IEnumerator Init(GameObjectPoolEntity[] arr, Transform parent)
        {
            int len = arr.Length;

            for (int i = 0; i < len; i++)
            {
                GameObjectPoolEntity entity = arr[i];
                if (entity.Pool != null)
                {
                    UnityEngine.Object.Destroy(entity.Pool.gameObject);
                    yield return(null);

                    entity.Pool = null;
                }
                //创建对象池
                PathologicalGames.SpawnPool pool = PathologicalGames.PoolManager.Pools.Create(entity.PoolName);
                pool.group.parent        = parent;
                pool.group.localPosition = Vector3.zero;
                entity.Pool = pool;

                m_SpawnPoolDic[entity.PoolId] = entity;
            }
        }
Beispiel #2
0
        /// <summary>
        /// 从对象池中获取对象 可以废弃
        /// </summary>
        /// <param name="poolId"></param>
        /// <param name="prefab"></param>
        /// <param name="onComplete"></param>
        public void Spawn(byte poolId, Transform prefab, System.Action <Transform> onComplete)
        {
            GameObjectPoolEntity entity = m_SpawnPoolDic[poolId];

            //拿到预设池
            PathologicalGames.PrefabPool prefabPool = entity.Pool.GetPrefabPool(prefab);


            if (prefabPool == null)
            {
                prefabPool = new PathologicalGames.PrefabPool(prefab);
                prefabPool.cullDespawned = entity.CullDespawned;
                prefabPool.cullAbove     = entity.CullAbove;
                prefabPool.cullDelay     = entity.CullDelay;

                prefabPool.cullMaxPerPass = entity.CullMaxPerPass;
                entity.Pool.CreatePrefabPool(prefabPool);
            }
            if (onComplete != null)
            {
                onComplete(entity.Pool.Spawn(prefab));
            }
        }
Beispiel #3
0
        /// <summary>
        /// 回池
        /// </summary>
        /// <param name="poolId"></param>
        /// <param name="instance"></param>
        public void Despawn(byte poolId, Transform instance)
        {
            GameObjectPoolEntity entity = m_SpawnPoolDic[poolId];

            entity.Pool.Despawn(instance);
        }
Beispiel #4
0
        /// <summary>
        /// 从对象池中获取对象
        /// </summary>
        /// <param name="prefabId"></param>
        /// <param name="onComplete"></param>
        public void Spawn(int prefabId, BaseAction <Transform> onComplete)
        {
            //拿到预设表数据
            Sys_PrefabEntity entity = GameEntry.DataTable.DataTableManager.Sys_PrefabDBModel.Get(prefabId);

            if (entity == null)
            {
                Debug.LogError("预设不存在");
                return;
            }
            GameEntry.Resource.ResourceLoaderManager.LoadMainAsset((AssetCategory)entity.AssetCategory, entity.AssetPath, (ResourceEntity resourseEntity) =>
            {
                //拿到对象池
                GameObjectPoolEntity gameObjectPoolEntity = m_SpawnPoolDic[entity.PoolId];

                Transform prefab = ((GameObject)resourseEntity.Target).transform;

                //使用预设编号 当前池id
                PrefabPool prefabPool = gameObjectPoolEntity.Pool.GetPrefabPool(entity.Id);

                if (prefabPool == null)
                {
                    //先去队列里找 空闲的池
                    if (m_PrefabPoolQueue.Count > 0)
                    {
                        Debug.LogError("从队列里取");
                        prefabPool = m_PrefabPoolQueue.Dequeue();

                        prefabPool.PrefabPoolId = entity.Id;  //预设池编号设置
                        gameObjectPoolEntity.Pool.AddPrefabPool(prefabPool);

                        prefabPool.prefab   = prefab;
                        prefabPool.prefabGO = prefab.gameObject;
                        prefabPool.AddPrefabToDic(prefab.name, prefab);
                    }
                    else
                    {
                        prefabPool = new PrefabPool(prefab, entity.Id);
                        gameObjectPoolEntity.Pool.CreatePrefabPool(prefabPool, resourseEntity);
                    }
                    prefabPool.OnPrefabPoolClear = (PrefabPool pool) =>
                    {
                        //预设池加入队列
                        pool.PrefabPoolId = 0;
                        gameObjectPoolEntity.Pool.RemovePrefabPool(pool);
                        m_PrefabPoolQueue.Enqueue(pool);
                    };
                    //这些属性要从表格中读取
                    prefabPool.cullDespawned  = gameObjectPoolEntity.CullDespawned;
                    prefabPool.cullAbove      = gameObjectPoolEntity.CullAbove;
                    prefabPool.cullDelay      = gameObjectPoolEntity.CullDelay;
                    prefabPool.cullMaxPerPass = gameObjectPoolEntity.CullMaxPerPass;
                }
                if (onComplete != null)
                {
                    //拿到一个实例
                    Transform retTrans = gameObjectPoolEntity.Pool.Spawn(prefab, resourseEntity);
                    int instanceID     = retTrans.gameObject.GetInstanceID();
                    m_InstanceIdPoolIdDic[instanceID] = entity.PoolId;
                    onComplete(retTrans);
                }
            });
        }