Пример #1
0
    public GameObject GetPooledObject(ObjectPoolTypeVariable type = null)
    {
        if (type == null)
        {
            type = pooledObjects.Keys.First();
        }

        foreach (GameObject pooledObject in pooledObjects[type])
        {
            if (!pooledObject.activeInHierarchy)
            {
                return(pooledObject);
            }
        }

        // In case of expand
        ObjectPoolItem objectPoolItemToUse = null;

        foreach (ObjectPoolItem objectPoolItem in objectPoolItems)
        {
            if (type == null || objectPoolItem.Type.ToString() == type.ToString())
            {
                objectPoolItemToUse = objectPoolItem;
                break;
            }
        }
        foreach (Transform child in transform)
        {
            if (type == null || child.name == type.ToString())
            {
                return(InstantiatePooledObject(objectPoolItemToUse, child));
            }
        }
        return(null);
    }
Пример #2
0
    public List <GameObject> GetPooledObjects(int n, ObjectPoolTypeVariable type = null)
    {
        if (type == null)
        {
            type = pooledObjects.Keys.First();
        }

        List <GameObject> result = new List <GameObject>();
        int length = 0;

        foreach (GameObject pooledObject in pooledObjects[type])
        {
            if (!pooledObject.activeInHierarchy)
            {
                result.Add(pooledObject);
                length++;

                if (length == n)
                {
                    return(result);
                }
            }
        }

        // In case of expand
        ObjectPoolItem objectPoolItemToUse = null;

        foreach (ObjectPoolItem objectPoolItem in objectPoolItems)
        {
            if (type == null || objectPoolItem.Type.ToString() == type.ToString())
            {
                objectPoolItemToUse = objectPoolItem;
                break;
            }
        }
        foreach (Transform child in transform)
        {
            if (type == null || child.name == type.ToString())
            {
                while (length < n)
                {
                    result.Add(InstantiatePooledObject(objectPoolItemToUse, child));
                    length++;
                }
            }
        }
        return(null);
    }