Пример #1
0
        private void CloneNewInstance <C>(C prefab, PoolSet pooledSet, Vector3 position, Quaternion rotation, out GameObject instanceObject, out C instanceScript) where C : IPooledObject
        {
            // Initialized a new GameObject
            instanceObject = Instantiate(prefab.gameObject, position, rotation);

            // Update this object's parent so it won't get destroyed easily
            instanceObject.transform.parent = poolingParent;

            // Grab the script from the clone if there is any in the original
            instanceScript = null;
            if (pooledSet.ContainsPoolScript == true)
            {
                instanceScript      = instanceObject.GetComponent <C>();
                instanceScript.Pool = pooledSet;
            }

            // Add this new GameObject and script into the dictionary
            pooledSet.ActiveInstances.Add(instanceObject, instanceScript);

            // Indicate to the script the object was initialized
            if (instanceScript != null)
            {
                instanceScript.AfterInitialized(this);
            }
        }
Пример #2
0
        GameObject CloneNewInstance(GameObject prefab, PoolSet pooledSet, Vector3 position, Quaternion rotation)
        {
            // Initialized a new GameObject
            GameObject returnObject = (GameObject)Instantiate(prefab, position, rotation);

            // Update this object's parent so it won't get destroyed easily
            returnObject.transform.parent = poolingParent;

            // Grab the script from the clone if there is any in the original
            IPooledObject script = null;

            if (pooledSet.containsPoolScript == true)
            {
                script = returnObject.GetComponent <IPooledObject>();
                script.OriginalPrefab = prefab;
            }

            // Add this new GameObject and script into the dictionary
            pooledSet.allClonedInstances.Add(returnObject, script);

            // Indicate to the script the object was initialized
            if (script != null)
            {
                script.Initialized(this);
            }
            return(returnObject);
        }
Пример #3
0
        public GameObject GetInstance(GameObject prefab, Vector3 position, Quaternion rotation)
        {
            // Cover the edge case
            GameObject returnObject = null;

            if (prefab != null)
            {
                // Check if the prefab is already pooled
                if (allPooledObjects.ContainsKey(prefab) == true)
                {
                    // Grab the pooled objects
                    PoolSet pooledSet = allPooledObjects[prefab];

                    // Check if there are any deactivated objects
                    foreach (KeyValuePair <GameObject, IPooledObject> cloneInstance in pooledSet.allClonedInstances)
                    {
                        if (cloneInstance.Key.activeSelf == false)
                        {
                            // If so, position this object properly
                            returnObject = cloneInstance.Key;
                            Transform cloneTransform = returnObject.transform;
                            cloneTransform.position = position;
                            cloneTransform.rotation = rotation;

                            // Activate this object and return it
                            returnObject.SetActive(true);

                            // If this set has a script, indicate this GameObject was activated
                            if (cloneInstance.Value != null)
                            {
                                cloneInstance.Value.Activated(this);
                            }
                            break;
                        }
                    }

                    // If not, initialize a new object
                    if (returnObject == null)
                    {
                        // Initialized a new GameObject
                        returnObject = CloneNewInstance(prefab, pooledSet, position, rotation);
                    }
                }
                else
                {
                    // Create a new entry in the dictionary
                    PoolSet pooledSet = new PoolSet(prefab);
                    allPooledObjects.Add(prefab, pooledSet);

                    // Initialized a new GameObject
                    returnObject = CloneNewInstance(prefab, pooledSet, position, rotation);
                }
            }
            return(returnObject);
        }
Пример #4
0
        public C GetInstance <C>(C prefab, Vector3 position, Quaternion rotation) where C : IPooledObject
        {
            // Cover the edge case
            C returnScript = null;

            if (prefab != null)
            {
                // Check if the prefab is already pooled
                PoolSet pooledSet;
                if (allPooledObjects.TryGetValue(prefab, out pooledSet) == true)
                {
                    // Check if there are any deactivated objects
                    if (pooledSet.InactiveInstances.Count > 0)
                    {
                        foreach (KeyValuePair <GameObject, IPooledObject> cloneInstance in pooledSet.InactiveInstances)
                        {
                            // If so, position this object properly
                            GameObject clone = cloneInstance.Key;
                            clone.transform.position = position;
                            clone.transform.rotation = rotation;

                            // Activate this object and return it
                            clone.SetActive(true);

                            // If this set has a script, indicate this GameObject was activated
                            returnScript = (C)cloneInstance.Value;
                            returnScript.AfterActivated(this);
                            break;
                        }
                    }
                    else
                    {
                        // If not, initialized a new GameObject
                        GameObject clone;
                        CloneNewInstance(prefab, pooledSet, position, rotation, out clone, out returnScript);
                    }
                }
                else
                {
                    // Create a new entry in the dictionary
                    pooledSet = new PoolSet(prefab);
                    allPooledObjects.Add(prefab, pooledSet);

                    // Initialized a new GameObject
                    GameObject clone;
                    CloneNewInstance(prefab, pooledSet, position, rotation, out clone, out returnScript);
                }
            }
            return(returnScript);
        }