-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoolSystem.cs
85 lines (68 loc) · 2.27 KB
/
PoolSystem.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PoolSystem : MonoBehaviour
{
public static PoolSystem instance;
[System.Serializable]
public class Pool
{
public string tag;
public GameObject prefab;
public int size;
}
public List<Pool> pools;
public Dictionary<string, Queue<GameObject>> poolDictionary;
private Dictionary<GameObject, string> activeObjects = new Dictionary<GameObject, string>();
#region Singleton/Pool Init
private void Awake()
{
if(instance == null)
{
instance = this;
}
poolDictionary = new Dictionary<string, Queue<GameObject>>();
foreach (Pool pool in pools)
{
Queue<GameObject> objectPool = new Queue<GameObject>();
for (int i = 0; i < pool.size; i++)
{
GameObject obj = Instantiate(pool.prefab, transform);
obj.SetActive(false);
objectPool.Enqueue(obj);
}
poolDictionary.Add(pool.tag, objectPool);
}
}
#endregion
public GameObject SpawnFromPool(string tag, Vector3 position, Quaternion rotation)
{
if(!poolDictionary.ContainsKey(tag))
return null;
// Check if there are any inactive objects in the pool
if (poolDictionary[tag].Count == 0)
return null;
GameObject objectToSpawn = poolDictionary[tag].Dequeue();
// Add the object to the active objects dictionary
activeObjects.Add(objectToSpawn, tag);
// Enable the object and call the OnSpawn method
objectToSpawn.SetActive(true);
objectToSpawn.transform.position = position;
objectToSpawn.transform.rotation = rotation;
return objectToSpawn;
}
public void DeactivateToPool(string tag, GameObject objectToDeactivate)
{
if (!poolDictionary.ContainsKey(tag))
return;
// Check if the object is active and in the active objects dictionary
if (objectToDeactivate.activeSelf && activeObjects.ContainsKey(objectToDeactivate))
{
// Deactivate the object, remove it from the active objects dictionary, and return it to the pool
objectToDeactivate.SetActive(false);
objectToDeactivate.transform.parent = transform;
activeObjects.Remove(objectToDeactivate);
poolDictionary[tag].Enqueue(objectToDeactivate);
}
}
}