-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathObjectPool.cs
More file actions
138 lines (124 loc) · 4.05 KB
/
ObjectPool.cs
File metadata and controls
138 lines (124 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObjectPool : MonoBehaviour
{
[SerializeField] private ObjectPool_Pool[] _ObjectPools = null;
private List<Transform> _Parents = new List<Transform>();
public static ObjectPool POOL;
private void Awake()
{
POOL = this;
for (int i = 0; i < _ObjectPools.Length; i++)
{
//Create parent
GameObject poolparent = new GameObject();
Destroy(poolparent.GetComponent<MeshRenderer>());
Destroy(poolparent.GetComponent<BoxCollider>());
//Set parent
poolparent.transform.parent = transform;
poolparent.transform.name = "Pool_" + _ObjectPools[i]._Name;
_Parents.Add(poolparent.transform);
//Create objects
for (int o = 0; o < _ObjectPools[i]._Amount; o++)
{
GameObject obj = (GameObject)Instantiate(_ObjectPools[i]._Prefab);
obj.transform.parent = poolparent.transform;
obj.transform.position = new Vector2(9999, 9999);
obj.SetActive(false);
_ObjectPools[i]._Objects.Add(obj);
}
}
}
//GetObject
public GameObject GetObject(string objname)
{
int id = FindObjectPoolID(objname, false);
return GetObject(id, true);
}
public GameObject GetObject(GameObject obj)
{
int id = FindObjectPoolID(obj);
return GetObject(id, true);
}
public GameObject GetObjectPrefabName(string prefabname)
{
int id = FindObjectPoolID(prefabname, true);
return GetObject(id, true);
}
//GetObject/setactive
public GameObject GetObject(string objname, bool setactive)
{
int id = FindObjectPoolID(objname, false);
return GetObject(id, setactive);
}
public GameObject GetObject(GameObject obj, bool setactive)
{
int id = FindObjectPoolID(obj);
return GetObject(id, setactive);
}
public GameObject GetObjectPrefabName(string prefabname, bool setactive)
{
int id = FindObjectPoolID(prefabname, true);
return GetObject(id, setactive);
}
public GameObject GetObject(int id, bool setactive)
{
GameObject freeObject = null;
for (int i = 0; i < _ObjectPools[id]._Objects.Count; i++)
{
if (!_ObjectPools[id]._Objects[i].activeInHierarchy)
{
_ObjectPools[id]._Objects[i].transform.position = new Vector3(999, 999, 999);
_ObjectPools[id]._Objects[i].SetActive(setactive);
freeObject = _ObjectPools[id]._Objects[i];
return freeObject;
}
}
freeObject = (GameObject)Instantiate(_ObjectPools[id]._Prefab, new Vector3(999, 999, 999), Quaternion.identity);
freeObject.transform.parent = _Parents[id];
freeObject.SetActive(setactive);
_ObjectPools[id]._Objects.Add(freeObject);
return freeObject;
}
public List<GameObject> GetAllObjects(GameObject objtype)
{
int id = FindObjectPoolID(objtype);
return _ObjectPools[id]._Objects;
}
private int FindObjectPoolID(GameObject obj)
{
int id = 0;
for (int i = 0; i < _ObjectPools.Length; i++)
{
if (obj == _ObjectPools[i]._Prefab)
{
id = i;
}
}
return id;
}
private int FindObjectPoolID(string objname, bool isprefab)
{
for (int i = 0; i < _ObjectPools.Length; i++)
{
if (isprefab)
{
if (objname == _ObjectPools[i]._Prefab.name)
return i;
}
else if (objname == _ObjectPools[i]._Name)
return i;
}
Debug.Log(objname + " Not Found");
return 0;
}
}
[System.Serializable]
public class ObjectPool_Pool
{
public string _Name;
public GameObject _Prefab;
public int _Amount;
[HideInInspector] public List<GameObject> _Objects;
}