Пример #1
0
    public GameObject RequestInstantiate(GameObject go, Vector3 pos, Quaternion rot, bool callStartImmediately = true)
    {
        for (int i = 0; i < poolPrefabs.Length; i++)
        {
            if (poolPrefabs[i].name == go.name)
            {
                if (pooledObjects[i].Count > 0 && pooledObjects[i].Count <= 100)
                {
                    GameObject firstIndex = pooledObjects[i][0];
                    firstIndex.transform.parent   = null;
                    firstIndex.transform.position = pos;
                    firstIndex.transform.rotation = rot;
                    firstIndex.SetActive(true);

                    PoolItem objPI = firstIndex.GetComponent <PoolItem>();
                    objPI.prefabIndex = i;

                    if (callStartImmediately)
                    {
                        objPI.InstantiateStart();
                    }

                    pooledObjects[i].RemoveAt(0);
                    return(firstIndex);
                }
                else
                {
                    GameObject newInstance = (GameObject)Instantiate(go, pos, rot);
                    newInstance.name = go.name;

                    PoolItem objPI = newInstance.GetComponent <PoolItem>();
                    objPI.prefabIndex = i;

                    if (callStartImmediately)
                    {
                        objPI.InstantiateStart();
                    }

                    return(newInstance);
                }
            }
        }
        return(null);
    }