Пример #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 GameObject GenerateAt(ObjectPoolTypeVariable type, Vector3 position)
    {
        var pooledObject = GetPooledObject(type);

        pooledObject.transform.position = position;
        pooledObject.SetActive(true);
        return(pooledObject);
    }
Пример #3
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);
    }
Пример #4
0
    void Instantiate(GameObject surfaces, ObjectPoolTypeVariable typeVariable = null, float zOffset = 0)
    {
        ObjectPoolTypeVariable currentTypeVariable = typeVariable == null?GetRandomObjectPoolTypeVariable() : typeVariable;

        GameObject pooledObject = objectPoolController.GetPooledObject(currentTypeVariable);

        if (randomPositionInsideSurface)
        {
            pooledObject.transform.position = GetRandomPosition(surfaces);
        }
        else
        {
            pooledObject.transform.position = transform.position;
        }
        pooledObject.transform.position += new Vector3(0, 0, zOffset);
        InvokeOnSpawn(pooledObject);
        pooledObject.SetActive(true);
    }
Пример #5
0
 public void InstantiateOne(ObjectPoolTypeVariable typeVariable)
 {
     Instantiate(surfacesContainer, typeVariable);
 }