You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.3 KiB
122 lines
3.3 KiB
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<string, List<GameObject>> listObject = new Dictionary<string, List<GameObject>>(); |
|
List<GameObject> listObjectCreate = new List<GameObject>(); |
|
/// <summary> |
|
/// 생성한 오브젝트를 저장합니다. |
|
/// </summary> |
|
/// <param name="name"></param> |
|
/// <param name="obj"></param> |
|
void ObjectAdd(string name, GameObject obj) |
|
{ |
|
listObjectCreate.Add(obj); |
|
|
|
if (!listObject.ContainsKey(name)) |
|
listObject.Add(name, new List<GameObject>()); |
|
listObject[name].Add(obj); |
|
|
|
obj.name = name + " " + listObject[name].Count; |
|
} |
|
/// <summary> |
|
/// 이전에 생성한 오브젝트 중 비활성화 되어있는게 있다면 |
|
/// 비활성화 되어있는 오브젝트를 넘기고 |
|
/// 없다면 신규생성합니다. |
|
/// </summary> |
|
/// <param name="name"></param> |
|
/// <returns></returns> |
|
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(); |
|
} |
|
} |
|
|
|
|