-
Notifications
You must be signed in to change notification settings - Fork 0
/
PoolController.cs
93 lines (79 loc) · 2.49 KB
/
PoolController.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
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class PoolController : MonoBehaviour
{
public bool InitializeOnAwake;
public bool FindAllPools;
Dictionary<string,PoolComponent> poolDictionary;
public static PoolController Instance;
private void Awake()
{
Instance = this;
if (InitializeOnAwake)
{
Initialize();
}
}
public void Initialize()
{
poolDictionary = new Dictionary<string, PoolComponent>();
if (FindAllPools)
{
PoolComponent[] tempComponents = FindObjectsOfType<PoolComponent>();
foreach (PoolComponent component in tempComponents)
{
poolDictionary.Add(component.Key, component);
}
}
}
public void InitializeAllPools()
{
List<PoolComponent> tempComponnents = new List<PoolComponent>();
tempComponnents = poolDictionary.Values.ToList();
tempComponnents.Union(FindObjectsOfType<PoolComponent>());
foreach (PoolComponent component in tempComponnents)
{
component.InitializePool();
}
}
public void InitializePools()
{
List<PoolComponent> tempComponnents = new List<PoolComponent>();
tempComponnents = poolDictionary.Values.ToList();
foreach (PoolComponent component in tempComponnents)
{
component.InitializePool();
}
}
public GameObject InstantiateFromPool(string key, Vector3 position, Quaternion rotation)
{
return poolDictionary[key].InstantiateFromPool(position, rotation);
}
public void ReturnToPool(string key, GameObject returnedObject)
{
poolDictionary[key].ReturnToPool(returnedObject);
}
public PoolComponent GetPool(string key)
{
if (poolDictionary.ContainsKey(key))
{
return poolDictionary[key];
}
else
{
Debug.LogError("Key " + key + " does not exist.");
return null;
}
}
public PoolComponent CreatePool(string key, GameObject objectTemplate, int objectsAmount)
{
if (!poolDictionary.ContainsKey(key))
{
PoolComponent tempComponent = new PoolComponent(objectTemplate, objectsAmount);
poolDictionary.Add(key, tempComponent);
}
return GetPool(key);
}
}