Beispiel #1
0
        /// <summary>
        /// <para>这是第一行</para>
        /// <para>在对象池中创建一个新的预制池并创建请求数量的实例对象(通过PrefabPool.preloadAmount设置),如果请求数量为0,将不会创建实例</para>
        /// <para>这个函数仅当你需要设置一个新的预制池时会被执行,比如在使用前的剔除或预加载</para>
        /// <para>另外,如果是第一次使用的预制池,用Active()将会自动创建默认值</para>
        ///
        /// <para>像下面这样创建预制池并设置参数      </para>
        /// <para>    PrefabPool prefabPool = new PrefabPool()</para>
        /// <para>    prefabPool.prefab = myPrefabReference;</para>
        /// <para>    prefabPool.preloadAmount = 0;</para>
        /// <para>    prefabPool.cullDespawned = True;</para>
        /// <para>    prefabPool.cullAbove = 50;</para>
        /// <para>    prefabPool.cullDelay = 30;</para>
        /// <para>//初始化内存池</para>
        /// <para>    spawnPool._perPrefabPoolOptions.Add(prefabPool);</para>
        /// <para>    spawnPool.CreatePrefabPool(spawnPool._perPrefabPoolOptions[spawnPool._perPrefabPoolOptions.Count - 1]);</para>
        /// </summary>
        /// <param name="prefabPool">A PrefabPool object</param>
        /// <returns>A List of instances spawned or an empty List</returns>
        public void CreatePrefabPool(PrefabPool prefabPool)
        {
            //判断内存池是否已经存在
            bool isAlreadyPool = this.GetPrefabPool(prefabPool.prefab) != null;

            if (!isAlreadyPool)
            {
                //给内存池对象赋值
                prefabPool.ObjectPool = this;

                //把预制池添加进列表和字典中
                this._prefabPools_List.Add(prefabPool);
                this.prefabs_Dict._Add(prefabPool.prefab.name, prefabPool.prefab);
            }

            // 预加载预制体的实例
            if (prefabPool.Preloaded != true)
            {
                if (this.logMessages)
                {
                    Debug.Log(string.Format("SpawnPool {0}: Preloading {1} {2}",
                                            this.poolName,
                                            prefabPool.preloadAmount,
                                            prefabPool.prefab.name));
                }

                prefabPool.PreloadInstances();
            }
        }
 public void CreatePrefabPool(PrefabPool prefabPool)
 {
     if (this.GetPrefab(prefabPool.prefab) == null)
     {
         prefabPool.spawnPool = this;
         this._prefabPools.Add(prefabPool);
         if (!this.prefabs.ContainsKey(prefabPool.prefab.name))
         {
             this.prefabs._Add(prefabPool.prefab.name, prefabPool.prefab);
         }
     }
     if (this.logMessages)
     {
         UnityEngine.Debug.Log(string.Format("SpawnPool {0}: Preloading {1} {2}", this.poolName, prefabPool.preloadAmount, prefabPool.prefab.name));
     }
     prefabPool.PreloadInstances();
 }
Beispiel #3
0
        /// <summary>
        /// Creates a new PrefabPool in this Pool and instances the requested
        /// number of instances (set by PrefabPool.preloadAmount). If preload
        /// amount is 0, nothing will be spawned and the return list will be empty.
        ///
        /// It is rare this function is needed during regular usage.
        /// This function should only be used if you need to set the preferences
        /// of a new PrefabPool, such as culling or pre-loading, before use. Otherwise,
        /// just use Spawn() and if the prefab is used for the first time a PrefabPool
        /// will automatically be created with defaults anyway.
        ///
        /// Note: Instances with ParticleEmitters can be preloaded too because
        ///       it won't trigger the emmiter or the coroutine which waits for
        ///       particles to die, which Spawn() does.
        ///
        /// Usage Example:
        ///     // Creates a prefab pool and sets culling options but doesn't
        ///     //   need to spawn any instances (this is fine)
        ///     PrefabPool prefabPool = new PrefabPool()
        ///     prefabPool.prefab = myPrefabReference;
        ///     prefabPool.preloadAmount = 0;
        ///     prefabPool.cullDespawned = True;
        ///     prefabPool.cullAbove = 50;
        ///     prefabPool.cullDelay = 30;
        ///
        ///     // Enemies is just an example. Any pool is fine.
        ///     PoolManager.Pools["Enemies"].CreatePrefabPool(prefabPool);
        ///
        ///     // Then, just use as normal...
        ///     PoolManager.Pools["Enemies"].Spawn(myPrefabReference);
        /// </summary>
        /// <param name="prefabPool">A PrefabPool object</param>
        /// <returns>A List of instances spawned or an empty List</returns>
        public void CreatePrefabPool(PrefabPool prefabPool)
        {
            // Only add a PrefabPool once. Uses a GameObject comparison on the prefabs
            //   This will rarely be needed and will almost Always run at game start,
            //   even if user-executed. This really only fails If a user tries to create
            //   a PrefabPool using a prefab which already has a PrefabPool in the same
            //   SpawnPool. Either user created twice or PoolManager went first or even
            //   second in cases where a user-script beats out PoolManager's init during
            //   Awake();
            bool isAlreadyPool = this.GetPrefabPool(prefabPool.prefab) == null ? false : true;

            // Used internally to reference back to this spawnPool for things
            //   like anchoring co-routines.
            prefabPool.spawnPool = this;
            if (!isAlreadyPool)
            {
                this._prefabPools.Add(prefabPool);

                // Add to the prefabs dict for convenience
                try
                {
                    this.prefabs._Add(prefabPool.prefab.name, prefabPool.prefab);
                }
                catch (System.Exception)
                {
                    throw new System.Exception(string.Format("对象池中已存在同名文件: {0} 。请检查配置表和对象池是否存在同名对象", prefabPool.prefab.name));
                }
            }

            // Preloading (uses a singleton bool to be sure this is only done once)
            if (prefabPool.preloaded != true)
            {
                if (this.logMessages)
                {
                    Debug.Log(string.Format("SpawnPool {0}: Preloading {1} {2}",
                                            this.poolName,
                                            prefabPool.preloadAmount,
                                            prefabPool.prefab.name));
                }

                prefabPool.PreloadInstances();
            }
        }
Beispiel #4
0
        public void CreatePrefabPool(PrefabPool prefabPool)
        {
            bool isAlreadyPool = this.GetPrefabPool(prefabPool.prefab.gameObject) == null ? false : true;

            if (isAlreadyPool)
            {
                throw new System.Exception(string.Format
                                           (
                                               "Prefab '{0}' is already in  SpawnPool '{1}'. Prefabs can be in more than 1 SpawnPool but " +
                                               "cannot be in the same SpawnPool twice.",
                                               prefabPool.prefab,
                                               this.poolName
                                           ));
            }

            prefabPool.spawnPool = this;
            this._prefabPools.Add(prefabPool);
            this.prefabs._Add(prefabPool.prefab.name, prefabPool.prefab);

            if (prefabPool.preloaded != true)
            {
                prefabPool.PreloadInstances();
            }
        }