public bool DespawnInstance(Transform xform, bool sendEventMessage) { if (this.logMessages) { Debug.Log(string.Format("SpawnPool {0} ({1}): Despawning '{2}'", this.spawnPool.poolName, this.prefab.name, xform.name)); } this._spawned.Remove(xform); this._despawned.Add(xform); if (sendEventMessage) { xform.gameObject.BroadcastMessage( "OnDespawned", this.spawnPool, SendMessageOptions.DontRequireReceiver ); } PoolManagerUtils.SetActive(xform.gameObject, false); if (!this.cullingActive && this.cullDespawned && this.totalCount > this.cullAbove) { this.cullingActive = true; this.spawnPool.StartCoroutine(CullDespawned()); } return(true); }
internal Transform SpawnInstance(Vector3 pos, Quaternion rot) { if (this.limitInstances && this.limitFIFO && this._spawned.Count >= this.limitAmount) { Transform firstIn = this._spawned[0]; if (this.logMessages) { Debug.LogErrorFormat("SpawnPool {0} ({1}): LIMIT REACHED! FIFO=True. Calling despawning for {2}...", this.spawnPool.poolName, this.prefab.name, firstIn ); } this.DespawnInstance(firstIn); this.spawnPool._spawned.Remove(firstIn); } Transform inst; if (this._despawned.Count == 0) { inst = this.SpawnNew(pos, rot); } else { inst = this._despawned[0]; this._despawned.RemoveAt(0); this._spawned.Add(inst); if (inst == null) { var msg = "Make sure you didn't delete a despawned instance directly."; throw new MissingReferenceException(msg); } if (this.logMessages) { Debug.Log(string.Format("SpawnPool {0} ({1}): respawning '{2}'.", this.spawnPool.poolName, this.prefab.name, inst.name)); } inst.position = pos; inst.rotation = rot; PoolManagerUtils.SetActive(inst.gameObject, true); } return(inst); }
internal void AddUnpooled(Transform inst, bool despawn) { this.nameInstance(inst); if (despawn) { PoolManagerUtils.SetActive(inst.gameObject, false); this._despawned.Add(inst); } else { this._spawned.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); }