public static GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent) { if (prefab != null) { // Find the pool that handles this prefab var pool = default(LeanGameObjectPool); // Create a new pool for this prefab? if (LeanGameObjectPool.TryFindPoolByPrefab(prefab, ref pool) == false) { pool = new GameObject("LeanPool (" + prefab.name + ")").AddComponent <LeanGameObjectPool>(); pool.Prefab = prefab; } // Try and spawn a clone from this pool var clone = pool.Spawn(position, rotation, parent); if (clone != null) { // If this clone was recycled, recycle the link too if (pool.Recycle == true && pool.Spawned >= pool.Capacity) { var existingPool = default(LeanGameObjectPool); if (Links.TryGetValue(clone, out existingPool) == true) { if (existingPool != pool) { Links.Remove(clone); } else { return(clone.gameObject); } } } // Associate this clone with this pool Links.Add(clone, pool); return(clone.gameObject); } } else { Debug.LogError("Attempting to spawn a null prefab"); } return(null); }
/// <summary>This allows you to spawn a prefab via GameObject.</summary> public static GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent = null, bool worldPositionStays = true) { if (prefab != null) { // Find the pool that handles this prefab var pool = default(LeanGameObjectPool); // Create a new pool for this prefab? if (LeanGameObjectPool.TryFindPoolByPrefab(prefab, ref pool) == false) { pool = new GameObject("LeanPool (" + prefab.name + ")").AddComponent <LeanGameObjectPool>(); pool.Prefab = prefab; } // Try and spawn a clone from this pool var clone = default(GameObject); if (pool.TrySpawn(position, rotation, parent, worldPositionStays, ref clone) == true) { // Clone already registered? if (Links.Remove(clone) == true) { // If this pool recycles clones, then this can be expected if (pool.Recycle == true) { } // This shouldn't happen else { Debug.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); } } else { Debug.LogError("Attempting to spawn a null prefab."); } return(null); }