diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/ObjectPoolManager.cs b/Gameton-06/Assets/Gameton/Scripts/Common/ObjectPoolManager.cs new file mode 100644 index 00000000..7aca0e8b --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Common/ObjectPoolManager.cs @@ -0,0 +1,129 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Pool; + +namespace TON +{ + [System.Serializable] + public class PoolData + { + public string objectId; + public GameObject objectPrefab; + // 몇개를 미리 생성 해놓을건지 + public int count; + } + public class ObjectPoolManager : MonoBehaviour + { + public static ObjectPoolManager Instance { get; private set; } + public List poolContainer = new List(); + + // 오브젝트풀 매니저 준비 완료표시 + public bool IsReady { get; private set; } + + // 생성할 오브젝트의 key값지정을 위한 변수 + private string objectName; + + + // 오브젝트풀들을 관리할 딕셔너리 + public SerializableDictionary> + ojbectPoolDic = new SerializableDictionary>(); + + // 오브젝트풀에서 오브젝트를 새로 생성할때 사용할 딕셔너리 + private SerializableDictionary poolGoDic = new SerializableDictionary(); + + + private void Awake() + { + if (Instance == null) + { + Instance = this; + } + else + { + Destroy(gameObject); + } + + Init(); + } + + private void OnDestroy() + { + if (Instance == this) + { + Instance = null; + } + } + + private void Init() + { + IsReady = false; + + for (int idx = 0; idx < poolContainer.Count; idx++) + { + IObjectPool pool = new ObjectPool(CreatePooledItem, OnTakeFromPool, OnReturnedToPool, + OnDestroyPoolObject, true, poolContainer[idx].count, poolContainer[idx].count); + + if (poolGoDic.ContainsKey(poolContainer[idx].objectId)) + { + Debug.LogFormat("{0} 이미 등록된 오브젝트입니다.", poolContainer[idx].objectId); + return; + } + + poolGoDic.Add(poolContainer[idx].objectId, poolContainer[idx].objectPrefab); + ojbectPoolDic.Add(poolContainer[idx].objectId, pool); + + // 미리 오브젝트 생성 해놓기 + for (int i = 0; i < poolContainer[idx].count; i++) + { + objectName = poolContainer[idx].objectId; + PoolAble poolAbleGo = CreatePooledItem().GetComponent(); + poolAbleGo.Pool.Release(poolAbleGo.gameObject); + } + } + + Debug.Log("오브젝트풀링 준비 완료"); + IsReady = true; + } + + private GameObject CreatePooledItem() + { + GameObject poolGo = Instantiate(poolGoDic[objectName]); + poolGo.GetComponent().Pool = ojbectPoolDic[objectName]; + return poolGo; + } + // 사용 + private void OnTakeFromPool(GameObject poolGo) + { + poolGo.SetActive(true); + } + + + // 반환 + private void OnReturnedToPool(GameObject poolGo) + { + poolGo.SetActive(false); + } + + // 삭제 + private void OnDestroyPoolObject(GameObject poolGo) + { + Destroy(poolGo); + } + + public GameObject GetEffect(string goName) + { + objectName = goName; + + if (poolGoDic.ContainsKey(goName) == false) + { + Debug.LogFormat("{0} 오브젝트풀에 등록되지 않은 오브젝트입니다.", goName); + return null; + } + + return ojbectPoolDic[goName].Get(); + } + } + + +} diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/ObjectPoolManager.cs.meta b/Gameton-06/Assets/Gameton/Scripts/Common/ObjectPoolManager.cs.meta new file mode 100644 index 00000000..68c5d5a8 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Common/ObjectPoolManager.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 0148e76254acd2346986bdf177405b67 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/PoolAble.cs b/Gameton-06/Assets/Gameton/Scripts/Common/PoolAble.cs new file mode 100644 index 00000000..2549ad7c --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Common/PoolAble.cs @@ -0,0 +1,17 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.Pool; + +namespace TON +{ + public class PoolAble : MonoBehaviour + { + public IObjectPool Pool { get; set; } + + public void ReleaseObject() + { + Pool.Release(gameObject); + } + } +} diff --git a/Gameton-06/Assets/Gameton/Scripts/Common/PoolAble.cs.meta b/Gameton-06/Assets/Gameton/Scripts/Common/PoolAble.cs.meta new file mode 100644 index 00000000..be870343 --- /dev/null +++ b/Gameton-06/Assets/Gameton/Scripts/Common/PoolAble.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: eafcfb854af802148b7288381765d116 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: