/// Move an instance from despawned to spawned, set the position and /// rotation, activate it and all children and return the GameObject. public GameObject SpawnInstance() { GameObject inst = null; // If nothing is available, create a new instance if (despawned.size == 0) { // This will also handle limiting the number of NEW instances if (spawnPool.limitCount > 0 && totalCount >= spawnPool.limitCount) { inst = spawned[0]; } else { inst = this.SpawnNew(); } } else { // Switch the instance we are using to the spawned list // Use the first item in the list for ease inst = despawned[0]; spawnPool.RemoveFromCache(inst); despawned.RemoveAt(0); spawned.Add(inst); // This came up for a user so this was added to throw a user-friendly error 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.loadType.ToString(), this.prefab.name,inst.name)); } // Get an instance and set position, rotation and then Reactivate the instance and all children inst.transform.position = Vector3.zero; inst.transform.rotation = Quaternion.identity; PoolManagerUtils.SetActive(inst, true, this.spawnPool.loadType); } return(inst); }
/// <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="despawn">True to despawn on add</param> internal void AddUnpooled(Transform inst, bool despawn) { this.nameInstance(inst); // Adds the number to the end if (despawn) { // Deactivate the instance and all children PoolManagerUtils.SetActive(inst.gameObject, false); // Start Tracking as despawned this._despawned.Add(inst); } else { this._spawned.Add(inst); } }
public bool PreDespawn(GameObject Instance) { bool despawned = false; if (this.despawned.Contains(Instance)) { despawned = false; } else if (this.spawned.Contains(Instance)) { this.spawned.Remove(Instance); this.despawned.Add(Instance); // Deactivate the instance and all children PoolManagerUtils.SetActive(Instance, false, this.spawnPool.loadType); despawned = true; } return(despawned); }
internal 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)); } // Switch to the despawned list this._spawned.Remove(xform); this._despawned.Add(xform); // Notify instance of event OnDespawned for custom code additions. // This is done before handling the deactivate and enqueue incase // there the user introduces an unforseen issue. if (sendEventMessage) { xform.gameObject.BroadcastMessage( "OnDespawned", this.spawnPool, SendMessageOptions.DontRequireReceiver ); } // Deactivate the instance and all children PoolManagerUtils.SetActive(xform.gameObject, false); // Trigger culling if the feature is ON and the size of the // overall pool is over the Cull Above threashold. // This is triggered here because Despawn has to occur before // it is worth culling anyway, and it is run fairly often. if (!this.cullingActive && // Cheap & Singleton. Only trigger once! this.cullDespawned && // Is the feature even on? Cheap too. this.totalCount > this.cullAbove) // Criteria met? { this.cullingActive = true; this.spawnPool.StartCoroutine(CullDespawned()); } return(true); }
private void OnDespawnObject(Transform xform) { this.spawned.Remove(xform); this.despawned.Add(xform); long ticks = DateTime.Now.Ticks; if (this.mDespawnedTime.ContainsKey(xform)) { this.mDespawnedTime[xform] = ticks; } else { this.mDespawnedTime.Add(xform, ticks); } xform.gameObject.BroadcastMessage("OnDespawned", SendMessageOptions.DontRequireReceiver); PoolManagerUtils.SetActive(xform.gameObject, false); if (!this.cullingActive && this.cullDespawned && this.totalCount > this.cullAbove) { this.cullingActive = true; this.spawnPool.StartCoroutine(this.CullDespawned()); } }
/// <summary> /// Move an instance from despawned to spawned, set the position and /// rotation, activate it and all children and return the transform. /// /// If there isn't an instance available, a new one is made. /// </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 SpawnInstance(Vector3 pos, Quaternion rot) { // Handle FIFO limiting if the limit was used and reached. // If first-in-first-out, despawn item zero and continue on to respawn it if (this.limitInstances && this.limitFIFO && this._spawned.Count >= this.limitAmount) { Transform firstIn = this._spawned[0]; if (this.logMessages) { Debug.Log(string.Format ( "SpawnPool {0} ({1}): " + "LIMIT REACHED! FIFO=True. Calling despawning for {2}...", this.spawnPool.poolName, this.prefab.name, firstIn )); } this.DespawnInstance(firstIn); // Because this is an internal despawn, we need to re-sync the SpawnPool's // internal list to reflect this this.spawnPool._spawned.Remove(firstIn); } Transform inst; // If nothing is available, create a new instance if (this._despawned.Count == 0) { // This will also handle limiting the number of NEW instances inst = this.SpawnNew(pos, rot); } else { // Switch the instance we are using to the spawned list // Use the first item in the list for ease inst = this._despawned[0]; this._despawned.RemoveAt(0); this._spawned.Add(inst); // This came up for a user so this was added to throw a user-friendly error 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)); } // Get an instance and set position, rotation and then // Reactivate the instance and all children inst.position = pos; inst.rotation = rot; PoolManagerUtils.SetActive(inst.gameObject, true); } // // NOTE: OnSpawned message broadcast was moved to main Spawn() to ensure it runs last // return(inst); }