Пример #1
0
    /// <summary>
    /// If there are no deactivated objects, creates a new one, otherwise it grabs the last object added to the list.
    /// </summary>
    /// <param name="startingParent">If the 'created' object's parent.</param>
    /// <param name="destroyTime">The given lifetime that the object should stay alive. Set to (-1) if there should be no destroy time.</param>
    /// <returns>Returns the object that was created.</returns>
    public PooledObject CreateObject(Transform startingParent = null, float destroyTime = -1)
    {
        PooledObject retObj = null;

        if (deactivatedObjects.Count == 0)
        {
            retObj = ObjectPoolingManager.instance.InstantiateObject(objectPrefab);
        }
        else
        {
            retObj = deactivatedObjects[deactivatedObjects.Count - 1];
            deactivatedObjects.RemoveAt(deactivatedObjects.Count - 1);
            retObj.gameObject.SetActive(true);
        }
        retObj.transform.parent = startingParent;
        retObj.pool             = this;
        retObj.OnCreatedByPool();
        if (destroyTime != -1)
        {
            retObj.DestroyAfterTime(destroyTime);
        }
        return(retObj);
    }