forked from NeilBostian/ObjectPooling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ObjectPoolManager.cs
73 lines (64 loc) · 2.17 KB
/
ObjectPoolManager.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
using UnityEngine;
using System;
using System.Collections.Generic;
namespace ObjectPooling
{
// This class is the only interface between
// ObjectPooling and Pool - Everything
// above this class in the call stack either
// initializes InstantiationData or handles
// network objects
public static class ObjectPoolManager
{
public static void Initialize()
{
}
public static void Uninitialize()
{
}
public static GameObject Instantiate(InstantiationData i)
{
if (ObjectPool.objectPool.LogFull)
Debug.Log("Instantiating " + i.PrefabPath);
Pool myPool = ObjectPool.GetPool(i.PrefabPath, i.PoolType);
var g = myPool.GetObject(i);
return g;
}
public static void Delete(GameObject g)
{
if (ObjectPool.objectPool.LogFull)
Debug.Log("Deleting " + g.name);
PoolToken token = ObjectPool.GetPoolToken(g);
Pool myPool = ObjectPool.GetPool(token.poolPrefabString, token.poolType);
myPool.StoreObject(g);
}
public static GameObject InstantiateAsChild(InstantiationData i, int parentTokenId)
{
var go = Instantiate(i);
var parent = ManagedObjects.Find(parentTokenId);
if (parent != null)
{
go.transform.parent = parent.transform;
}
else
{
throw new Exception("Parent with Id " + parentTokenId + " not found.");
}
return go;
}
public static GameObject InstantiateAsSibling(InstantiationData i, int siblingTokenId)
{
var go = Instantiate(i);
var sibling = ManagedObjects.Find(siblingTokenId);
if (sibling != null)
{
go.transform.parent = sibling.transform.parent;
}
else
{
throw new Exception("Sibling with Id " + siblingTokenId + " not found.");
}
return go;
}
}
}