Ejemplo n.º 1
0
        /// <summary>This allows you to despawn a clone via GameObject, with optional delay.</summary>
        public static void Despawn(GameObject clone, float delay = 0.0f)
        {
            if (clone != null)
            {
                var pool = default(LeanGameObjectPool);

                // Try and find the pool associated with this clone
                if (Links.TryGetValue(clone, out pool) == true)
                {
                    // Remove the association
                    Links.Remove(clone);

                    pool.Despawn(clone, delay);
                }
                else
                {
                    if (LeanGameObjectPool.TryFindPoolByClone(clone, ref pool) == true)
                    {
                        pool.Despawn(clone, delay);
                    }
                    else
                    {
                        Debug.LogWarning("You're attempting to despawn a gameObject that wasn't spawned from this pool", clone);

                        // Fall back to normal destroying
                        Object.Destroy(clone);
                    }
                }
            }
            else
            {
                Debug.LogWarning("You're attempting to despawn a null gameObject", clone);
            }
        }
Ejemplo n.º 2
0
        /// <summary>This allows you to detach a clone via GameObject, with optional delay.
        /// A detached clone will act as a normal GameObject, requiring you to manually destroy or otherwise manage it.
        /// NOTE: If this clone has been despawned then it will still be parented to the pool.</summary>
        public static void Detach(GameObject clone)
        {
            if (clone != null)
            {
                var pool = default(LeanGameObjectPool);

                // Try and find the pool associated with this clone
                if (Links.TryGetValue(clone, out pool) == true)
                {
                    // Remove the association
                    Links.Remove(clone);

                    pool.Detach(clone);
                }
                else
                {
                    if (LeanGameObjectPool.TryFindPoolByClone(clone, ref pool) == true)
                    {
                        pool.Detach(clone);
                    }
                    else
                    {
                        Debug.LogWarning("You're attempting to detach a gameObject that wasn't spawned from this pool", clone);
                    }
                }
            }
            else
            {
                Debug.LogWarning("You're attempting to detach a null gameObject", clone);
            }
        }
Ejemplo n.º 3
0
        /// <summary>Find the pool responsible for handling the specified prefab clone.
        /// NOTE: This can be an expensive operation if you have many large pools.</summary>
        public static bool TryFindPoolByClone(GameObject clone, ref LeanGameObjectPool pool)
        {
            foreach (var instance in Instances)
            {
                // Search hash set
                if (instance.spawnedClonesHashSet.Contains(clone) == true)
                {
                    pool = instance;

                    return(true);
                }

                // Search list
                for (var j = instance.spawnedClonesList.Count - 1; j >= 0; j--)
                {
                    if (instance.spawnedClonesList[j] == clone)
                    {
                        pool = instance;

                        return(true);
                    }
                }
            }

            return(false);
        }
        public static GameObject Spawn(GameObject prefab, Vector3 position, Quaternion rotation, Transform parent)
        {
            if (prefab != null)
            {
                // Find the pool that handles this prefab
                var pool = LeanGameObjectPool.FindPoolByPrefab(prefab);

                // Create a new pool for this prefab?
                if (pool == null)
                {
                    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);
        }
Ejemplo n.º 5
0
        /// <summary>Find the pool responsible for handling the specified prefab.</summary>
        public static bool TryFindPoolByPrefab(GameObject prefab, ref LeanGameObjectPool foundPool)
        {
            for (var i = Instances.Count - 1; i >= 0; i--)
            {
                var pool = Instances[i];

                if (pool.Prefab == prefab)
                {
                    foundPool = pool; return(true);
                }
            }

            return(false);
        }
Ejemplo n.º 6
0
        /// <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);
        }
Ejemplo n.º 7
0
        /// <summary>This allows you to despawn a clone via GameObject, with optional delay.</summary>
        public static void Despawn(GameObject clone, float delay = 0.0f)
        {
            if (clone != null)
            {
                var pool = default(LeanGameObjectPool);

                // Try and find the pool associated with this clone
                if (Links.TryGetValue(clone, out pool) == true)
                {
                    // Remove the association
                    Links.Remove(clone);

                    pool.Despawn(clone, delay);
                }
                else
                {
                    if (LeanGameObjectPool.TryFindPoolByClone(clone, ref pool) == true)
                    {
                        pool.Despawn(clone, delay);
                    }
                    else
                    {
                        Debug.LogWarning("You're attempting to despawn a gameObject that wasn't spawned from a pool (or the pool was destroyed).", clone);

                        // Fall back to normal destroying
#if UNITY_EDITOR
                        if (Application.isPlaying == false)
                        {
                            Object.DestroyImmediate(clone);

                            return;
                        }
#endif
                        if (delay > 0.0f)
                        {
                            Object.Destroy(clone);
                        }
                        else
                        {
                            Object.Destroy(clone, delay);
                        }
                    }
                }
            }
            else
            {
                Debug.LogWarning("You're attempting to despawn a null gameObject.", clone);
            }
        }
Ejemplo n.º 8
0
        /// <summary>Find the pool responsible for handling the specified prefab clone.
        /// NOTE: This can be an expensive operation if you have many large pools.</summary>
        public static bool TryFindPoolByClone(GameObject gameObject, ref LeanGameObjectPool foundPool)
        {
            for (var i = Instances.Count - 1; i >= 0; i--)
            {
                var pool = Instances[i];

                for (var j = pool.spawnedClones.Count - 1; j >= 0; j--)
                {
                    var clone = pool.spawnedClones[j];

                    if (clone.GameObject == gameObject)
                    {
                        foundPool = pool; return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 9
0
        /// <summary>Find the pool responsible for handling the specified prefab clone.
        /// NOTE: This can be an expensive operation if you have many large pools.</summary>
        public static bool TryFindPoolByClone(GameObject clone, ref LeanGameObjectPool pool)
        {
            for (var i = Instances.Count - 1; i >= 0; i--)
            {
                pool = Instances[i];

                // Search hash set
                if (pool.spawnedClonesHashSet.Contains(clone) == true)
                {
                    return(true);
                }

                // Search list
                for (var j = pool.spawnedClonesList.Count - 1; j >= 0; j--)
                {
                    if (pool.spawnedClonesList[j] == clone)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
Ejemplo n.º 10
0
 /// <summary>Find the pool responsible for handling the specified prefab.</summary>
 public static bool TryFindPoolByPrefab(GameObject prefab, ref LeanGameObjectPool foundPool)
 {
     return(prefabMap.TryGetValue(prefab, out foundPool));
 }