-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathGameObjectPool.cs
More file actions
149 lines (124 loc) · 5.11 KB
/
GameObjectPool.cs
File metadata and controls
149 lines (124 loc) · 5.11 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
139
140
141
142
143
144
145
146
147
148
149
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Engine.State {
public class GameObjectPoolItemMeta {
public GameObject go;
public int amountToBuffer = 3;
public string name = "";
}
public class GameObjectPool : GameObjectBehavior {
public static GameObjectPool instance;
/// <summary>
/// The object prefabs which the pool can handle.
/// </summary>
public Dictionary<string, GameObjectPoolItemMeta> objectPrefabs;
/// <summary>
/// The pooled objects currently available.
/// </summary>
public Dictionary<string, List<GameObject>> pooledObjects;
/// <summary>
/// The amount of objects of each type to buffer.
/// </summary>
public int[] amountToBuffer;
public int defaultBufferAmount = 3;
/// <summary>
/// The container object that we will keep unused pooled objects so we dont clog up the editor with objects.
/// </summary>
protected GameObject containerObject;
private void Awake() {
instance = this;
}
// Use this for initialization
private void Start() {
containerObject = new GameObject("GameObjectPool");
}
public void CachePrefab(string prefabName, int bufferAmount) {
//Loop through the object prefabs and make a new list for each one.
//We do this because the pool can only support prefabs set to it in the editor,
//so we can assume the lists of pooled objects are in the same order as object prefabs in the array
pooledObjects = new Dictionary<string, List<GameObject>>();
int i = 0;
foreach (GameObjectPoolItemMeta item in objectPrefabs.Values) {
// pooledObjects[item.name][i] = new Dictionary<string, List<GameObject>>();
for (int n = 0; n < bufferAmount; n++) {
GameObject newObj = Instantiate(Resources.Load(prefabName)) as GameObject;
newObj.name = prefabName;
PoolObject(newObj);
LogUtil.Log("item:" + item.name);
}
i++;
}
}
/*
public void CachePrefab(UnityEngine.Ob objectPrefab, int bufferAmount) {
//Loop through the object prefabs and make a new list for each one.
//We do this because the pool can only support prefabs set to it in the editor,
//so we can assume the lists of pooled objects are in the same order as object prefabs in the array
pooledObjects = new List<GameObject>[objectPrefabs.Length];
int i = 0;
foreach ( GameObject objectPrefab in objectPrefabs ) {
pooledObjects[i] = new List<GameObject>();
int bufferAmountItem;
if(i < amountToBuffer.Length) bufferAmountItem = amountToBuffer[i];
else
bufferAmountItem = defaultBufferAmount;
for ( int n=0; n<bufferAmountItem; n++) {
GameObject newObj = Instantiate(objectPrefab) as GameObject;
newObj.name = objectPrefab.name;
PoolObject(newObj);
}
i++;
}
}
*/
/// <summary>
/// Gets a new object for the name type provided. If no object type exists or if onlypooled is true and there is no objects of that type in the pool
/// then null will be returned.
/// </summary>
/// <returns>
/// The object for type.
/// </returns>
/// <param name='objectType'>
/// Object type.
/// </param>
/// <param name='onlyPooled'>
/// If true, it will only return an object if there is one currently pooled.
/// </param>
public GameObject GetObjectForType(string objectType, bool onlyPooled) {
if (pooledObjects.ContainsKey(objectType)) {
foreach (GameObject go in pooledObjects[objectType]) {
GameObject pooledObject = go;
//pooledObjects[i].RemoveAt(0);
pooledObject.transform.parent = null;
pooledObject.SetActive(true);
return pooledObject;
}
}
else if (!onlyPooled) {
return Instantiate(Resources.Load(objectPrefabs[objectType].name)) as GameObject;
}
return null;
}
/// <summary>
/// Pools the object specified. Will not be pooled if there is no prefab of that type.
/// </summary>
/// <param name='obj'>
/// Object to be pooled.
/// </param>
public void PoolObject(GameObject obj) {
/*
for ( int i=0; i<objectPrefabs.Length; i++)
{
if(objectPrefabs[i].name == obj.name)
{
obj.SetActive(false);
obj.transform.parent = containerObject.transform;
pooledObjects[i].Add(obj);
return;
}
}
*/
}
}
}