// 오브젝트풀에 아이템을 추가함 private void AddToPool(int numberInPool) { Rigidbody rb = null; PooledObj pooled = null; for (int i = 0; i < numberInPool; i++) { // 풀링할 오브젝트 프리팹 인덱스를 선택함 int randomIndex = Random.Range(0, _prefabs.Length); // 지정한 프리팹을 오브젝트로 생성함 GameObject instance = Instantiate(_prefabs[randomIndex]); pooled = instance.GetComponent <PooledObj>(); if (pooled == null) { pooled = instance.AddComponent <PooledObj>(); } pooled.RetrunPool += ReturnPool; // 생성한 오브젝트를 풀링 오브젝트의 자식으로 연결함 instance.transform.parent = transform; // 오브젝트를 비활성화 함 instance.SetActive(false); rb = instance.GetComponent <Rigidbody>(); if (rb == null) { rb = instance.AddComponent <Rigidbody>(); } if (_useSizeByWeight) { RandomSizeByWeight(instance, rb, _weightMinMax); } // 오브젝트 풀에 생성한 오브젝트를 추가함 //_pool.Add (instance); Pool = instance; } }
public static ObjectPool GetPool(PooledObj prefab) { GameObject obj; ObjectPool pool; if (Application.isEditor) { obj = GameObject.Find(prefab.name + " Pool"); if (obj) { pool = obj.GetComponent <ObjectPool>(); if (pool) { return(pool); } } } obj = new GameObject(prefab.name + " Pool"); pool = obj.AddComponent <ObjectPool>(); pool.prefab = prefab; return(pool); }
public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation) { if (!poolDictionary.ContainsKey(tag)) { Debug.LogWarning("Pool with tag" + tag + "doesn't exist"); return(null); } GameObject objectToSpawn = poolDictionary[tag].Dequeue(); objectToSpawn.SetActive(true); objectToSpawn.transform.position = position; objectToSpawn.transform.rotation = rotation; PooledObj pooledObj = objectToSpawn.GetComponent <PooledObj>(); if (pooledObj != null) { pooledObj.OnObjectSpawn(); } poolDictionary[tag].Enqueue(objectToSpawn); return(objectToSpawn); }
public void ReturnObject(PooledObj obj) { obj.transform.SetParent(transform); obj.gameObject.SetActive(false); availableObjects.Push(obj); }