Beispiel #1
0
        public GameObject InstantiateFromPool(GameObject prefab, Vector3 position, Quaternion rotation,
                                              PoolType poolType)
        {
            Init();

            int foundIndex = poolObjects.FindIndex(o => o.prefab == prefab && !o.isUsed);

            if (foundIndex == -1 && disabledRealInstantiate)
            {
                foundIndex = FindFreePoolObjectIndex(poolType);
            }

            if (foundIndex == -1)
            {
                if (disabledRealInstantiate)
                {
                    return(null);
                }

                WaitPoolObject newPoolObject = InstantiateNew(prefab, poolType);
                if (newPoolObject != null)
                {
                    poolObjects.Add(newPoolObject);
                    newPoolObject.lifeObject.SetActive(true);
                    newPoolObject.isUsed = true;
                    newPoolObject.lifeObject.transform.position = position;
                    newPoolObject.lifeObject.transform.rotation = rotation;
                    newPoolObject.poolObject.Activate(newPoolObject.prefab);
                    return(newPoolObject.lifeObject);
                }
                else
                {
                    Debug.LogWarning("Instantiate without pull! Object " + prefab.name +
                                     " didn't contain IPoolObject interface!");
                    GameObject newObject = Instantiate(prefab);
                    newObject.transform.position = position;
                    newObject.transform.rotation = rotation;
                    return(newObject);
                }
            }
            else
            {
                WaitPoolObject newPoolObject = poolObjects[foundIndex];
                newPoolObject.lifeObject.SetActive(true);
                newPoolObject.isUsed = true;
                newPoolObject.lifeObject.transform.position = position;
                newPoolObject.lifeObject.transform.rotation = rotation;
                newPoolObject.poolObject.Activate(newPoolObject.prefab);
                Debug.Log("Exist 'instantiate' pool object. Name=" + newPoolObject.lifeObject.name);
                return(newPoolObject.lifeObject);
            }
        }
Beispiel #2
0
        WaitPoolObject InstantiateNew(GameObject prefab, PoolType poolType)
        {
            if (prefab.GetComponent <IPoolObject>() != null)
            {
                GameObject newGameObject = Instantiate(prefab);
                newGameObject.transform.SetParent(this.transform, true);
                IPoolObject poolObject = newGameObject.GetComponent <IPoolObject>();
                poolObject.IsPooledObject = true;

                WaitPoolObject newObj = new WaitPoolObject()
                {
                    isUsed     = true,
                    lifeObject = newGameObject,
                    poolObject = poolObject,
                    prefab     = prefab,
                    poolType   = poolType
                };
                return(newObj);
            }

            return(null);
        }