-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathObjectPool.cs
121 lines (109 loc) · 4.04 KB
/
ObjectPool.cs
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
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ObjectPool {
protected static ObjectPool m_instance;
protected Dictionary<string, Dictionary<string, Stack>> reservoir;
public static ObjectPool instance {
set {
m_instance = value;
} get {
if (m_instance == null) {
m_instance = new ObjectPool();
m_instance.reservoir = new Dictionary<string, Dictionary<string, Stack>>();
}
return m_instance;
}
}
public static void ClearReservoir() {
m_instance.reservoir.Clear();
}
public GameObject GetGameObject(string type, string name, string source = "N/A"){
if(reservoir.ContainsKey(type)){
if(reservoir[type].ContainsKey(name)){
while(reservoir[type][name].Count > 0){
GameObject gameObject = (GameObject) reservoir[type][name].Pop();
if(!gameObject.activeInHierarchy){
//Debug.Log("Found Object in Pool: " + name + " for " + source);
//gameObject.SetActive(true);
//Debug.Log("Poped: " + name);
return gameObject;
}
//Debug.Log("Poped for Free; found Active: " + name);
}
}
}
//if(type == "scene") Debug.Log("Object not found: " + name);
return null;
}
public void PutGameObject(string type, string name, GameObject gameObject){
if(!reservoir.ContainsKey(type)){
reservoir.Add(type,new Dictionary<string, Stack>());
}
if(!reservoir[type].ContainsKey(name)){
Stack poolStack = new Stack();
reservoir[type].Add(name,poolStack);
}
//Debug.Log("Pushing to Pool: " + name);
if (gameObject.transform.parent != null) gameObject.transform.parent = null;
gameObject.SetActive(false);
reservoir[type][name].Push(gameObject);
}
public T GetObjectByPop<T>(string type, string name, string source = "N/A") {
if (reservoir.ContainsKey(type)) {
if (reservoir[type].ContainsKey(name)) {
if (reservoir[type][name].Count > 0) {
T obj = (T)reservoir[type][name].Pop();
//Debug.Log("Poped for: " + obj);
return obj;
}
}
}
//Debug.Log("Object not found: " + name);
return default(T);
}
public T GetObjectByPeek<T>(string type, string name, string source = "N/A") {
if (reservoir.ContainsKey(type)) {
if (reservoir[type].ContainsKey(name)) {
if (reservoir[type][name].Count > 0) {
T obj = (T)reservoir[type][name].Peek();
//Debug.Log("Peeked for: " + obj);
return obj;
}
}
}
//Debug.Log("Object not found: " + name);
return default(T);
}
public void PutObject<T>(string type, string name, T obj) {
if (!reservoir.ContainsKey(type)) {
reservoir.Add(type, new Dictionary<string, Stack>());
}
if (!reservoir[type].ContainsKey(name)) {
Stack poolStack = new Stack();
reservoir[type].Add(name, poolStack);
}
//Debug.Log("Pushing to Pool: " + name);
reservoir[type][name].Push(obj);
}
public void RemoveAllObject(string type, string name) {
if (!reservoir.ContainsKey(type) || !reservoir[type].ContainsKey(name)) {
return;
}
//Debug.Log("Removed from Pool: " + name);
reservoir[type][name].Clear();
}
public void OverrideLastObject<T>(string type, string name, T obj) {
if (!reservoir.ContainsKey(type)) {
reservoir.Add(type, new Dictionary<string, Stack>());
}
if (!reservoir[type].ContainsKey(name)) {
Stack poolStack = new Stack();
reservoir[type].Add(name, poolStack);
} else {
reservoir[type][name].Pop();
}
//Debug.Log("Pushing to Pool: " + name);
reservoir[type][name].Push(obj);
}
}