using System.Collections; using System.Collections.Generic; using UnityEngine; public abstract class ResourcePool : MonoBehaviour { static public GameObject ObjectCreateStatic(GameObject prefab, Transform target = null) { Transform t = Instantiate(prefab).transform; //t.transform.parent = target; t.SetParent(target); t.transform.localScale = Vector3.one; t.transform.localPosition = Vector3.zero; t.transform.localEulerAngles = Vector3.zero; return t.gameObject; } Dictionary> listObject = new Dictionary>(); List listObjectCreate = new List(); /// /// 생성한 오브젝트를 저장합니다. /// /// /// void ObjectAdd(string name, GameObject obj) { listObjectCreate.Add(obj); if (!listObject.ContainsKey(name)) listObject.Add(name, new List()); listObject[name].Add(obj); obj.name = name + " " + listObject[name].Count; } /// /// 이전에 생성한 오브젝트 중 비활성화 되어있는게 있다면 /// 비활성화 되어있는 오브젝트를 넘기고 /// 없다면 신규생성합니다. /// /// /// GameObject ObjectGet(string name) { if (listObject.ContainsKey(name)) { return listObject[name].Find(a => !a.activeSelf); //foreach (GameObject t in listObject[name]) //{ // if (!t.activeInHierarchy) // return t; //} } return null; } public GameObject ObjectCreate(GameObject prefab, Transform parents, Vector3 scale, Vector3 pos, bool ON = true) { GameObject obj = ObjectGet(prefab.name); if (obj == null) { obj = Instantiate(prefab); ObjectAdd(prefab.name, obj); } obj.transform.SetParent(parents); obj.transform.localScale = scale; obj.transform.position = pos; obj.transform.localEulerAngles = Vector3.zero; obj.gameObject.SetActive(ON); return obj; } public GameObject ObjectCreateLocalPos(GameObject prefab, Transform parents, Vector3 scale, Vector3 pos, bool ON = true) { GameObject obj = ObjectGet(prefab.name); if (obj == null) { obj = Instantiate(prefab); ObjectAdd(prefab.name, obj); } obj.transform.SetParent(parents); obj.transform.localScale = scale; obj.transform.transform.localPosition = pos; obj.transform.localEulerAngles = Vector3.zero; obj.gameObject.SetActive(ON); return obj; } public bool DISABLE_HIDE = false; public void ObjectAllHide() { for (int i = 0; i < listObjectCreate.Count; i++) { listObjectCreate[i].transform.SetParent(transform); listObjectCreate[i].SetActive(false); } } private void OnDisable() { if (DISABLE_HIDE) { ObjectAllHide(); } } public void ObjectDestroy() { listObject.Clear(); listObjectCreate.Clear(); } }