-
Notifications
You must be signed in to change notification settings - Fork 0
/
VisualPartRuntimePool.cs
105 lines (92 loc) · 2.52 KB
/
VisualPartRuntimePool.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
#region copyright
// BuildR 2.0
// Available on the Unity Asset Store https://www.assetstore.unity3d.com/#!/publisher/412
// Copyright (c) 2017 Jasper Stocker http://support.jasperstocker.com
// Support contact email@jasperstocker.com
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
#endregion
using System.Collections.Generic;
using UnityEngine;
namespace BuildR2
{
public class VisualPartRuntimePool : MonoBSingleton<VisualPartRuntimePool>
{
private List<VisualPartRuntime> _pool;
private bool _initialised;
[SerializeField]
private int prewarmSize = 25;
private int _internalCounter = 0;
public void ClearPool()
{
Transform[] children = transform.GetComponentsInChildren<Transform>();
int childCount = children.Length;
for(int c = 0; c < childCount; c++)
{
if(children[c] == transform) continue;
#if UNITY_EDITOR
DestroyImmediate(children[c].gameObject);
#else
Destroy(children[c].gameObject);
#endif
}
_internalCounter = 0;
}
public VisualPartRuntime Pull()
{
if (!_initialised) Init();
VisualPartRuntime output = GetPoolItem();
output.Activate();
return output;
}
public void Push(VisualPartRuntime item)
{
if (!_initialised) Init();
item.transform.parent = transform;
_pool.Add(item);
}
public void Push(VisualPartRuntime[] items)
{
foreach (VisualPartRuntime item in items)
Push(item);
}
public void Push(List<VisualPartRuntime> items)
{
foreach (VisualPartRuntime item in items)
Push(item);
}
private void Init()
{
_initialised = true;
_pool = new List<VisualPartRuntime>();
for(int p = 0; p < prewarmSize; p++)
Push(Instantiate());
}
private VisualPartRuntime GetPoolItem()
{
if (_pool.Count > 0)
{
VisualPartRuntime output = _pool[0];
_pool.RemoveAt(0);
return output;
}
else
{
return Instantiate();//there was nothing suitable in the pool - make a new one!
}
}
private VisualPartRuntime Instantiate()
{
string newName = string.Format("New Visual Part {0}", _internalCounter);
_internalCounter++;
return VisualPartRuntime.Create(transform, newName);
}
private void Awake()
{
Init();
}
}
}