コード例 #1
0
    /// <summary>
    /// Acquire an instance of the pooled object based on the specified name. If an object cannot be acquired, a new one can be instantiated. Note that while using the Acquire method to load objects from resources is still available, this feature is no longer reccomended. An instantiation method has been added to support this behavior.
    /// </summary>
    /// <param name="name">Name.</param>
    public GameObject Acquire(string name)
    {
        GameObject go = m_poolLibrary[name].Acquire(this.transform.position, this.transform.rotation);

        if (go == null)
        {
            if (!m_enforcePooling)
            {
                // Attempt to instantiate a new instance of an otherwise pooled object.
                if (m_poolLibrary[name] != null)
                {
                    go = (GameObject)GameObject.Instantiate(m_poolLibrary[name].Prefab, this.transform.position, this.transform.rotation);
                }
            }

                        #if UNITY_EDITOR
            else
            {
                if (m_debug)
                {
                    Debug.Log("Attempting to overdraw from the pool, but pooling is enforced! A null object will be returned.");
                }
            }
                        #endif
        }

                #if UNITY_EDITOR
        else
        {
            if (m_debug)
            {
                Debug.Log("Acquired an object from the pool.");
            }
        }
                #endif

        // Attempt to instantiate an object from the Resources folder -- assume "name in parameters" refers to an asset path. These can be removed using the pool.
        if (go == null && m_allowInstantiation)
        {
            go = (GameObject)GameObject.Instantiate(Resources.Load(name), this.transform.position, this.transform.rotation);
        }

        // Should this object be detached from the Pool's tree?
        if (go != null && m_detach)
        {
            go.transform.parent = null;
        }

        // Throw an exception if there is an internal object acquisition error.
        if (go == null && !m_enforcePooling)
        {
            WhisperPoolException exception = new WhisperPoolException("Object could not be acquired or instantiated!");
            Debug.LogError(exception.Message);
            throw exception;
        }

        return(go);
    }
コード例 #2
0
    /// <summary>
    /// Start this instance, and initialize the internal data structures (Pool Library, object lists) within the pool.
    /// </summary>
    void Start()
    {
        if (m_persistent)
        {
            DontDestroyOnLoad(this.gameObject);
        }

        // Error handling. If the pool initializes and the Pool Library is null, notify the user.
        if (m_poolLibrary == null)
        {
            WhisperPoolException exception = new WhisperPoolException("Pool Library is null, something failed to initialize!");
            Debug.LogError(exception.Message);
            throw exception;
        }

        // Setup helper lists
        m_timedReleaseObjects = new List <DelayedReleaseObject> ();

        // Load preset if it is not null
        if (m_usePreset)
        {
            if (m_presets != null)
            {
                for (int i = 0; i < m_presets.Count; i++)
                {
                    if (m_presets[i] != null)
                    {
                        m_poolLibrary.Nodes.AddRange(m_presets[i].Library.Nodes);
                    }
                }
            }
        }

        // Sort the Pool Library
        m_poolLibrary.Pool = this.gameObject;
        m_poolLibrary.Sort();
        m_poolLibrary.Initialize();
    }
コード例 #3
0
    /// <summary>
    /// Release the specified object. If it was acquired from the pool, it is released. If it was instantiated, it will be destroyed. Otherwise, the object will either be left alone or destroyed.
    /// </summary>
    /// <param name="obj">Object to release or destroy.</param>
    public void Release(GameObject obj)
    {
        if (m_poolLibrary[obj.name] != null)
        {
            bool released = m_poolLibrary[obj.name].Release(obj, this.transform.position, this.transform.rotation);

            if (!released)
            {
                if (m_enforcePooling)
                {
                    WhisperPoolException exception = new WhisperPoolException("Failed to release object. Object not destroyed as pooling is enforced. A pool overdraw has occured. Ensure that no new objects were instantiated by the pool if \"Enforce Pooling\" was at one point disabled during runtime.");
                    Debug.LogError(exception.Message);
                    throw exception;
                }

                else
                {
                                        #if UNITY_EDITOR
                    if (m_debug)
                    {
                        Debug.Log("Object instantiated after start up by the pool, destroying it instead.");
                    }
                                        #endif

                    Destroy(obj);
                }
            }

                        #if UNITY_EDITOR
            else
            {
                if (m_debug)
                {
                    Debug.Log("Released an object back into the pool.");
                }
            }
                        #endif
        }

        else
        {
            if (m_allowInstantiation)
            {
                                #if UNITY_EDITOR
                if (m_debug)
                {
                    Debug.Log("Object not in pool passed to the release method--destroying object.");
                }
                                #endif

                Destroy(obj);
            }

                        #if UNITY_EDITOR
            else
            {
                if (m_debug)
                {
                    Debug.Log("Object not in pool passed to the release method, but external object removal has not been enabled in editor. To destroy external objects, enable \"Allow Instantiations\" in the editor.");
                }
            }
                        #endif
        }
    }