-
Notifications
You must be signed in to change notification settings - Fork 0
/
ManifestationsFactory.cs
59 lines (45 loc) · 1.54 KB
/
ManifestationsFactory.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
using System.Collections.Generic;
using UnityEngine;
public class ManifestationsFactory : Singleton<ManifestationsFactory>
{
public int poolSize;
public GameObject[] prefabs;
protected Dictionary<string, Helper.ObjectPool> pools;
private void Start()
{
InitPools();
}
private void InitPools()
{
pools = new Dictionary<string, Helper.ObjectPool>();
for (int i = 0; i < prefabs.Length; i++)
pools.Add(prefabs[i].name, new Helper.ObjectPool(prefabs[i], poolSize));
}
public GameObject CreateObject(string name, Vector3 position)
{
return CreateObject(name, position, Quaternion.identity);
}
public GameObject CreateObject(string objectName, Vector3 position, Quaternion rotation, bool setActive = true)
{
if (!pools.ContainsKey(objectName))
{
Debug.LogWarning(string.Format("Could not find a pooled object with name '{0}'", name));
return null;
}
GameObject instance = pools[objectName].GetObject();
instance.transform.position = position;
instance.transform.rotation = rotation;
instance.SetActive(setActive);
return instance;
}
public bool ReturnObject(GameObject instance)
{
if (!pools.ContainsKey(instance.name))
{
Debug.LogWarning(string.Format("Could not find a pooled object with name '{0}'", name));
return false;
}
pools[instance.name].ReturnObject(instance);
return true;
}
}