Ejemplo n.º 1
0
    public List <GameObject> GetSeveral(string tag, int number)
    {
        List <GameObject> objects = new List <GameObject>();

        for (int i = 0; i < pooledObjects.Count; i++)
        {
            if (!pooledObjects[i].activeInHierarchy && pooledObjects[i].CompareTag(tag))
            {
                objects.Add(pooledObjects[i]);
                if (objects.Count == number)
                {
                    return(objects);
                }
            }
        }
        foreach (ObjectPoolItem item in itemsToPool)
        {
            if (item.ObjectToPool.CompareTag(tag))
            {
                var missingObjects = number - objects.Count;
                for (int i = 0; i < missingObjects; i++)
                {
                    GameObject obj = (GameObject)Instantiate(item.ObjectToPool);
                    obj.transform.SetParent(parents[item.ObjectToPool.tag].transform);
                    obj.SetActive(false);
                    pooledObjects.Add(obj);
                    objects.Add(obj);
                    NewObjectCreated?.Invoke(obj);
                }
            }
        }
        return(objects);
    }
Ejemplo n.º 2
0
 public void Init()
 {
     pooledObjects = new List <GameObject>();
     foreach (ObjectPoolItem item in itemsToPool)
     {
         var parent = new GameObject(item.ObjectToPool.tag);
         parents.Add(item.ObjectToPool.tag, parent);
         for (int i = 0; i < item.AmountToPool; i++)
         {
             GameObject obj = (GameObject)Instantiate(item.ObjectToPool);
             obj.transform.SetParent(parent.transform);
             obj.SetActive(false);
             pooledObjects.Add(obj);
             NewObjectCreated?.Invoke(obj);
         }
     }
 }
Ejemplo n.º 3
0
    public GameObject GetPooledObject(string tag)
    {
        for (int i = 0; i < pooledObjects.Count; i++)
        {
            if (!pooledObjects[i].activeInHierarchy && pooledObjects[i].CompareTag(tag))
            {
                return(pooledObjects[i]);
            }
        }

        foreach (ObjectPoolItem item in itemsToPool)
        {
            if (item.ObjectToPool.CompareTag(tag))
            {
                GameObject obj = (GameObject)Instantiate(item.ObjectToPool);
                obj.transform.SetParent(parents[item.ObjectToPool.tag].transform);
                obj.SetActive(false);
                pooledObjects.Add(obj);
                NewObjectCreated?.Invoke(obj);
                return(obj);
            }
        }
        return(null);
    }