protected virtual void Update() { // Decay the life of all delayed destruction calls for (int __i = delays.Count - 1; __i >= 0; __i--) { Delay __delay = delays[__i]; __delay.life -= Time.deltaTime; // Skip to next one? if (__delay.life > 0.0f) { continue; } // Remove and pool delay delays.RemoveAt(__i); ClassPool <Delay> .Despawn(__delay); // Finally despawn it after delay if (__delay.clone != null) { Despawn(__delay.clone); } else { CGDebug.LogWarning("Attempting to update the delayed destruction of a prefab clone that no longer exists, did you accidentally delete it?", this); } } }
public void PreloadOne() { if (prefab != null) { // Create clone GameObject __clone = CreateClone(Vector3.zero, Quaternion.identity, null); // Add clone to despawned list despawnedClones.Add(__clone); // Deactivate it __clone.SetActive(false); // Move it under this GO __clone.transform.SetParent(transform, false); if (Capacity > 0 && Total > Capacity) { CGDebug.LogWarning("You've preloaded more than the pool capacity, please verify you're preloading the intended amount.", this); } } else { CGDebug.LogWarning("Attempting to preload a null prefab.", this); } }
public void Despawn(GameObject clone, float t = 0.0f) { if (clone != null) { // Delay the despawn? if (t > 0.0f) { DespawnWithDelay(clone, t); } // Despawn now? else { TryDespawn(clone); // If this clone was marked for delayed despawn, remove it for (int __i = delays.Count - 1; __i >= 0; __i--) { Delay __delay = delays[__i]; if (__delay.clone == clone) { delays.RemoveAt(__i); } } } } else { CGDebug.LogWarning("You're attempting to despawn a null gameObject", this); } }
private void TryDespawn(GameObject clone) { if (_spawnedClonesHashSet.Remove(clone) || spawnedClones.Remove(clone)) { DespawnNow(clone); } else { CGDebug.LogWarning("You're attempting to despawn a GameObject that wasn't spawned from this pool, make sure your Spawn and Despawn calls match.", clone); } }
public static GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) { if (prefab != null) { // Find the pool that handles this prefab // Create a new pool for this prefab? if (GameObjectPool.TryFindPoolByPrefab(prefab, out GameObjectPool __pool) == false) { __pool = new GameObject("CGPool(" + prefab.name + ")").AddComponent <GameObjectPool>(); __pool.prefab = prefab; } // Try and spawn a clone from this pool GameObject __clone = default(GameObject); if (__pool.TrySpawn(position, rotation, parent, ref __clone) != true) { return(null); } // Clone already registered? if (Links.Remove(__clone)) { // If this pool recycles clones, then this can be expected if (__pool.Recycle) { } // This shouldn't happen else { CGDebug.LogWarning("You're attempting to spawn a clone that hasn't been despawned. Make sure all your Spawn and Despawn calls match, you shouldn't be manually destroying them!", __clone); } } // Associate this clone with this pool Links.Add(__clone, __pool); return(__clone); } CGDebug.LogError("Attempting to spawn a null prefab"); return(null); }
public void PreloadAll() { if (Preload <= 0) { return; } if (prefab != null) { for (int __i = Total; __i < Preload; __i++) { PreloadOne(); } } else { CGDebug.LogWarning("Attempting to preload a null prefab", this); } }
private bool TryDespawnOldest(ref GameObject clone, bool registerDespawned) { MergeSpawnedClonesToList(); // Loop through all spawnedClones from the front (oldest) until one is found while (spawnedClones.Count > 0) { clone = spawnedClones[0]; spawnedClones.RemoveAt(0); if (clone != null) { DespawnNow(clone, registerDespawned); return(true); } CGDebug.LogWarning("This pool contained a null spawned clone, did you accidentally destroy it?", this); } return(false); }
public static void Despawn(GameObject clone, float delay = 0.0f) { if (clone != null) { // Try and find the pool associated with this clone if (Links.TryGetValue(clone, out GameObjectPool __pool)) { // Remove the association Links.Remove(clone); __pool.Despawn(clone, delay); } else { if (GameObjectPool.TryFindPoolByClone(clone, ref __pool)) { __pool.Despawn(clone, delay); } else { CGDebug.LogWarning("You're attempting to despawn a gameObject that wasn't spawned from this pool", clone); // Fall back to normal destroying #if UNITY_EDITOR //Object.DestroyImmediate(clone); #else //Object.Destroy(clone); #endif } } } else { CGDebug.LogWarning("You're attempting to despawn a null gameObject", clone); } }
public bool TrySpawn(Vector3 position, Quaternion rotation, Transform parent, ref GameObject clone) { if (prefab != null) { // Spawn a previously despawned/preloaded clone? for (int __i = despawnedClones.Count - 1; __i >= 0; __i--) { clone = despawnedClones[__i]; despawnedClones.RemoveAt(__i); if (clone != null) { SpawnClone(clone, position, rotation, parent); return(true); } CGDebug.LogWarning("This pool contained a null despawned clone, did you accidentally destroy it?", this); } // Make a new clone? if (Capacity <= 0 || Total < Capacity) { clone = CreateClone(position, rotation, parent); // Add clone to spawned list if (Recycle) { spawnedClones.Add(clone); } else { _spawnedClonesHashSet.Add(clone); } // Activate clone.SetActive(true); // Notifications InvokeOnSpawn(clone); return(true); } // Recycle? if (Recycle != true || TryDespawnOldest(ref clone, false) != true) { return(false); } SpawnClone(clone, position, rotation, parent); return(true); } CGDebug.LogWarning("You're attempting to spawn from a pool with a null prefab", this); return(false); }