-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathObjectPool.cs
More file actions
59 lines (45 loc) · 1.53 KB
/
ObjectPool.cs
File metadata and controls
59 lines (45 loc) · 1.53 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
using System.Collections.Generic;
using NaughtyAttributes;
using UnityEngine;
public class ObjectPool : MonoBehaviour {
private static ObjectPool control;
private Dictionary<GameObject, List<GameObject>> objects = new Dictionary<GameObject, List<GameObject>>();
private void Awake() {
control = this;
}
public static int NumObjects => control.transform.childCount;
public static int NumActiveObjects {
get {
Transform trans = control.transform;
int num = 0;
for (int i = 0; i < trans.childCount; i++)
if (trans.GetChild(i).gameObject.activeSelf)
num++;
return num;
}
}
public static GameObject Spawn(GameObject prefab, Vector3 position) => control.SpawnObject(prefab, position);
public GameObject SpawnObject(GameObject prefab, Vector3 position) => SpawnObject(prefab, position, transform);
public GameObject SpawnObject(GameObject prefab, Vector3 position, Transform parent) {
if (objects.ContainsKey(prefab)) {
List<GameObject> instances = objects[prefab];
for (int i = 0; i < instances.Count; i++) {
if (instances[i] == null) {
instances.RemoveAt(i);
i--;
} else if (!instances[i].activeSelf) {
instances[i].SetActive(true);
instances[i].transform.localPosition = position;
return instances[i];
}
}
GameObject spawn = Spawn();
instances.Add(spawn);
return spawn;
} else {
objects.Add(prefab, new List<GameObject>() { Spawn() });
return objects[prefab][0];
}
GameObject Spawn() => Instantiate(prefab, position, Quaternion.identity, parent);
}
}