internal bool InactiveInstance(Transform xform, bool sendEventMessage) { if (this.LogMessages) { Debug.Log(string.Format("SpawnPool {0} ({1}): Despawning '{2}'", this.ObjectPool.poolName, this.prefab.name, xform.name)); } //把实例从显示数组移到隐藏数组中 this._active_List.Remove(xform); this._inactive_List.Add(xform); //通知自定义代码中添加的实例的事件OnDespawned(像除自身外所有子类发消息) if (sendEventMessage) { xform.gameObject.BroadcastMessage( "OnDespawned", this.ObjectPool, SendMessageOptions.DontRequireReceiver ); } PoolManagerUtils.SetActive(xform.gameObject, false); if (!this._destroy_Bool && // 销毁开关 this.AutoDestroy && // 自动销毁的开关 this.totalCount > this.DestroyExceptTheNumber) // 对象总数大于保留的对象数目 { this._destroy_Bool = true; this.ObjectPool.StartCoroutine(Destroy()); } return(true); }
internal bool DespawnInstance(Transform xform, bool sendEventMessage) { if (this.logMessages) { UnityEngine.Debug.Log(string.Format("SpawnPool {0} ({1}): Despawning '{2}'", this.spawnPool.poolName, this.prefab.name, xform.name)); } this._spawned.Remove(xform); this.spawnPool.NextDisableTr = xform; if (sendEventMessage) { xform.gameObject.BroadcastMessage("OnDespawned", this.spawnPool, SendMessageOptions.DontRequireReceiver); } if (this.spawnPool.DelayNextDisable) { this.spawnPool.DelayNextDisable = false; this.spawnPool.StartCoroutine(this.DelayedlDespawned(xform)); } else { this._despawned.Add(xform); PoolManagerUtils.SetActive(xform.gameObject, false); } if (!this.cullingActive && this.cullDespawned && this.totalCount > this.cullAbove) { this.cullingActive = true; this.spawnPool.StartCoroutine(this.CullDespawned()); } return(true); }
/// <summary> /// 用对象池添加一个现存的实例到预制池 /// Used by a SpawnPool to add an existing instance to this PrefabPool. /// This is used during game start to pool objects which are not /// instantiated at runtime /// </summary> /// <param name="inst">The instance to add</param> /// <param name="inactive">True to despawn on add</param> internal void AddInstanceNotInPool(Transform inst, bool inactive) { //给实例的名字加上数字 this.nameInstance(inst); if (inactive) { PoolManagerUtils.SetActive(inst.gameObject, false); this._inactive_List.Add(inst); } else { this._active_List.Add(inst); } }
// ParticleSystem (Shuriken) Version... private IEnumerator ListenForEmitDespawn(ParticleSystem emitter) { // Wait for the delay time to complete // Waiting the extra frame seems to be more stable and means at least one // frame will always pass yield return(new WaitForSeconds(emitter.startDelay + 0.25f)); // Do nothing until all particles die or the safecount hits a max value float safetimer = 0; // Just in case! See Spawn() for more info while (emitter.IsAlive(true)) { if (!PoolManagerUtils.activeInHierarchy(emitter.gameObject)) { emitter.Clear(true); yield break; // Do nothing, already despawned. Quit. } safetimer += Time.deltaTime; if (safetimer > this.maxParticleDespawnTime) { Debug.LogWarning ( string.Format ( "SpawnPool {0}: " + "Timed out while listening for all particles to die. " + "Waited for {1}sec.", this.poolName, this.maxParticleDespawnTime ) ); } yield return(null); } // Turn off emit before despawning //emitter.Clear(true); this.Despawn(emitter.transform); }
/// <summary> ///<para>若开启了先进先出,则在没有隐藏对象的情况下,把第一个对象隐藏,再显示出来</para> /// </summary> /// <returns> /// The new instance's Transform. /// /// If the Limit option was used for the PrefabPool associated with the /// passed prefab, then this method will return null if the limit is /// reached. /// </returns> internal Transform ActiveInstance(Vector3 pos, Quaternion rot) { //开启了限制实例数量和先进先出,而且显示实例数量大于等于限制数量 if (this.limitInstances && this.limitFIFO && this._active_List.Count >= this.limitAmount) { Transform firstIn = this._active_List[0]; if (this.LogMessages) { Debug.Log(string.Format ( "SpawnPool {0} ({1}): " + "限制完成,隐藏 {2}...", this.ObjectPool.poolName, this.prefab.name, firstIn )); } //隐藏实例 this.InactiveInstance(firstIn); //从显示数组中移除 this.ObjectPool._active_List.Remove(firstIn); } Transform inst; // 如果没有隐藏的实例,创建一个新的 if (this._inactive_List.Count == 0) { inst = this.ActiveNew(pos, rot); } else { //从隐藏数组移到显示数组 inst = this._inactive_List[0]; this._inactive_List.RemoveAt(0); this._active_List.Add(inst); if (inst == null) { var msg = "确保没有手动删除一个隐藏对象"; throw new MissingReferenceException(msg); } if (this.LogMessages) { Debug.Log(string.Format("SpawnPool {0} ({1}): 显示 '{2}'.", this.ObjectPool.poolName, this.prefab.name, inst.name)); } inst.position = pos; inst.rotation = rot; PoolManagerUtils.SetActive(inst.gameObject, true); } return(inst); }