Beispiel #1
0
    public GameObject SpawnFromPool(PooledObjectType tag, Vector3 pos, Quaternion rot)
    {
        if (!PoolDictionary.ContainsKey(tag))
        {
            Debug.LogWarning("PoolObjects with Tag " + tag + " doesn't exist ..");
            return(null);
        }

        GameObject objToSpawn;

        if (PoolDictionary[tag].Count != 0)
        {
            objToSpawn = PoolDictionary[tag].Peek();
            objToSpawn.SetActive(true);
            objToSpawn.transform.position = pos;
            objToSpawn.transform.rotation = rot;

            IPooledObject iPooledObj = objToSpawn.GetComponent <IPooledObject>();
            iPooledObj.Init();
            iPooledObj.OnObjectSpawn();

            PoolDictionary[tag].Dequeue();
        }
        else
        {
            objToSpawn = ExpandPool(tag, pos, rot);
        }

        return(objToSpawn);
    }
Beispiel #2
0
    private GameObject ExpandPool(PooledObjectType tag, Vector3 pos, Quaternion rot)
    {
        int        index = _poolIndexes[tag];
        GameObject temp  = Instantiate(Pool[index].Prefab);

        temp.SetActive(true);
        temp.transform.SetParent(_poolMasters[tag]);

        temp.transform.position = pos;
        temp.transform.rotation = rot;

        if (temp.GetComponent <IPooledObject>() == null)
        {
            PooledObject tempPool = temp.AddComponent <PooledObject>();
            tempPool.Type = tag;
        }

        IPooledObject iPooledObj = temp.GetComponent <IPooledObject>();

        iPooledObj.Init();
        iPooledObj.OnObjectSpawn();


        PoolDictionary[tag].Enqueue(temp);

        Pool[index].Size++;

        return(temp);
    }
Beispiel #3
0
 void CreateNewPooledObject(int numberOfObjects)
 {
     numberOfObjects = Mathf.Clamp(numberOfObjects, 0, maxPoolCapacity);
     for (int i = 0; i < numberOfObjects; i++)
     {
         IPooledObject temp = Instantiate(prefab).GetComponent <IPooledObject>();
         temp.Init(this);
         temp.Destroy();
         freed.Enqueue(temp);
     }
 }
        GameObject AllocateNew(bool active, bool toReinit)
        {
            GameObject result = Object.Instantiate(Prefab.ThisObject);

            IPooledObject pooled = result.GetComponent <IPooledObject>();

            pooled.Init();

            result.name = Prefab.ThisObject.name;
            result.SetActive(active);
            result.transform.parent = pool;

            if (toReinit)
            {
                pooled.Reinit();
            }

            allocated.Add(pooled);

            return(result);
        }