com.amanotes.prefabpool 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +16 -0
- package/CHANGELOG.md.meta +7 -0
- package/Docs/PrefabPool-API.md +366 -0
- package/Docs/PrefabPool-API.md.meta +7 -0
- package/Docs.meta +8 -0
- package/README.md +80 -69
- package/README.md.meta +7 -0
- package/Runtime/AssemblyInfo.cs +3 -0
- package/Runtime/AssemblyInfo.cs.meta +11 -0
- package/Runtime/MaterialPool.cs.meta +11 -0
- package/Runtime/PoolEntryDrawer.cs.meta +11 -0
- package/Runtime/PrefabPool.asmdef +3 -0
- package/Runtime/PrefabPool.asmdef.meta +7 -0
- package/Runtime/PrefabPool.cs +42 -10
- package/Runtime/PrefabPool.cs.meta +11 -0
- package/Runtime.meta +8 -0
- package/Tests/Runtime/PrefabPool.Test.asmdef +11 -0
- package/Tests/Runtime/PrefabPool.Test.asmdef.meta +7 -0
- package/Tests/Runtime/PrefabPoolGroupTests.cs +224 -0
- package/Tests/Runtime/PrefabPoolGroupTests.cs.meta +11 -0
- package/Tests/Runtime/PrefabPoolLifecycleTests.cs +257 -0
- package/Tests/Runtime/PrefabPoolLifecycleTests.cs.meta +11 -0
- package/Tests/Runtime/PrefabPoolPerformanceTests.cs +212 -0
- package/Tests/Runtime/PrefabPoolPerformanceTests.cs.meta +11 -0
- package/Tests/Runtime/PrefabPoolTestsSetup.cs +14 -0
- package/Tests/Runtime/PrefabPoolTestsSetup.cs.meta +11 -0
- package/Tests/Runtime/TestHelpers/MockPoolPrefab.cs +26 -0
- package/Tests/Runtime/TestHelpers/MockPoolPrefab.cs.meta +11 -0
- package/Tests/Runtime/TestHelpers.meta +8 -0
- package/Tests/Runtime.meta +8 -0
- package/Tests.meta +8 -0
- package/package.json +1 -1
- package/package.json.meta +7 -0
- package/Runtime/Amanotes.PrefabPool.asmdef +0 -14
- package/Runtime/MeshRendererExtensions.cs +0 -80
- /package/{Editor → Runtime}/PoolEntryDrawer.cs +0 -0
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
using System.Collections;
|
|
2
|
+
using NUnit.Framework;
|
|
3
|
+
using Unity.PerformanceTesting;
|
|
4
|
+
using UnityEngine;
|
|
5
|
+
using UnityEngine.TestTools;
|
|
6
|
+
using UnityObject = UnityEngine.Object;
|
|
7
|
+
|
|
8
|
+
namespace Amanotes.Core
|
|
9
|
+
{
|
|
10
|
+
public class PrefabPoolPerformanceTests
|
|
11
|
+
{
|
|
12
|
+
private PrefabPool _pool;
|
|
13
|
+
private GameObject _testPrefab;
|
|
14
|
+
private PoolGroup _testGroup;
|
|
15
|
+
private Transform _spawnParent;
|
|
16
|
+
|
|
17
|
+
[SetUp]
|
|
18
|
+
public void SetUp()
|
|
19
|
+
{
|
|
20
|
+
var poolObject = new GameObject("TestPool");
|
|
21
|
+
_pool = poolObject.AddComponent<PrefabPool>();
|
|
22
|
+
|
|
23
|
+
var spawnParentObject = new GameObject("SpawnParent");
|
|
24
|
+
_spawnParent = spawnParentObject.transform;
|
|
25
|
+
|
|
26
|
+
_testPrefab = new GameObject("TestPrefab");
|
|
27
|
+
_testPrefab.AddComponent<MockPoolPrefab>();
|
|
28
|
+
|
|
29
|
+
_testGroup = new PoolGroup
|
|
30
|
+
{
|
|
31
|
+
id = "TestGroup",
|
|
32
|
+
entries = new System.Collections.Generic.List<PoolEntry>
|
|
33
|
+
{
|
|
34
|
+
new PoolEntry
|
|
35
|
+
{
|
|
36
|
+
id = "TestPrefab",
|
|
37
|
+
prefab = _testPrefab,
|
|
38
|
+
prewarmCount = 10
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
_pool.RegisterGroup(_testGroup, _spawnParent);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
[TearDown]
|
|
47
|
+
public void TearDown()
|
|
48
|
+
{
|
|
49
|
+
if (_pool != null)
|
|
50
|
+
{
|
|
51
|
+
_pool.DestroyPool();
|
|
52
|
+
UnityObject.DestroyImmediate(_pool.gameObject);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
if (_spawnParent != null)
|
|
56
|
+
{
|
|
57
|
+
UnityObject.DestroyImmediate(_spawnParent.gameObject);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (_testPrefab != null)
|
|
61
|
+
{
|
|
62
|
+
UnityObject.DestroyImmediate(_testPrefab);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
var allObjects = UnityObject.FindObjectsOfType<GameObject>();
|
|
66
|
+
foreach (var obj in allObjects)
|
|
67
|
+
{
|
|
68
|
+
if (obj.hideFlags == HideFlags.DontSave)
|
|
69
|
+
{
|
|
70
|
+
UnityObject.DestroyImmediate(obj);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
[UnityTest, Performance]
|
|
76
|
+
public IEnumerator Performance_GetReturnCycles()
|
|
77
|
+
{
|
|
78
|
+
yield return _pool.Initialize();
|
|
79
|
+
|
|
80
|
+
Measure.Method(() =>
|
|
81
|
+
{
|
|
82
|
+
for (int i = 0; i < 1000; i++)
|
|
83
|
+
{
|
|
84
|
+
var obj = _testGroup.Get("TestPrefab");
|
|
85
|
+
_testGroup.Return(obj);
|
|
86
|
+
}
|
|
87
|
+
})
|
|
88
|
+
.WarmupCount(5)
|
|
89
|
+
.MeasurementCount(10)
|
|
90
|
+
.IterationsPerMeasurement(1)
|
|
91
|
+
.GC()
|
|
92
|
+
.Run();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
[UnityTest, Performance]
|
|
96
|
+
public IEnumerator Performance_GCAllocation()
|
|
97
|
+
{
|
|
98
|
+
yield return _pool.Initialize();
|
|
99
|
+
|
|
100
|
+
Measure.Method(() =>
|
|
101
|
+
{
|
|
102
|
+
for (int i = 0; i < 100; i++)
|
|
103
|
+
{
|
|
104
|
+
var obj = _testGroup.Get("TestPrefab");
|
|
105
|
+
_testGroup.Return(obj);
|
|
106
|
+
}
|
|
107
|
+
})
|
|
108
|
+
.WarmupCount(5)
|
|
109
|
+
.MeasurementCount(10)
|
|
110
|
+
.IterationsPerMeasurement(1)
|
|
111
|
+
.GC()
|
|
112
|
+
.Run();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
[UnityTest, Performance]
|
|
116
|
+
public IEnumerator Performance_PoolExpansion()
|
|
117
|
+
{
|
|
118
|
+
yield return _pool.Initialize();
|
|
119
|
+
|
|
120
|
+
var retrievedObjects = new System.Collections.Generic.List<GameObject>();
|
|
121
|
+
|
|
122
|
+
for (int i = 0; i < 10; i++)
|
|
123
|
+
{
|
|
124
|
+
var obj = _testGroup.Get("TestPrefab");
|
|
125
|
+
retrievedObjects.Add(obj);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
Measure.Method(() =>
|
|
129
|
+
{
|
|
130
|
+
for (int i = 0; i < 100; i++)
|
|
131
|
+
{
|
|
132
|
+
var obj = _testGroup.Get("TestPrefab");
|
|
133
|
+
retrievedObjects.Add(obj);
|
|
134
|
+
}
|
|
135
|
+
})
|
|
136
|
+
.WarmupCount(2)
|
|
137
|
+
.MeasurementCount(10)
|
|
138
|
+
.IterationsPerMeasurement(1)
|
|
139
|
+
.GC()
|
|
140
|
+
.Run();
|
|
141
|
+
|
|
142
|
+
foreach (var obj in retrievedObjects)
|
|
143
|
+
{
|
|
144
|
+
_testGroup.Return(obj);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
[UnityTest, Performance]
|
|
149
|
+
public IEnumerator Performance_ParentTransformOverhead()
|
|
150
|
+
{
|
|
151
|
+
yield return _pool.Initialize();
|
|
152
|
+
|
|
153
|
+
var withParentTime = 0.0;
|
|
154
|
+
var withoutParentTime = 0.0;
|
|
155
|
+
|
|
156
|
+
Measure.Method(() =>
|
|
157
|
+
{
|
|
158
|
+
for (int i = 0; i < 100; i++)
|
|
159
|
+
{
|
|
160
|
+
var obj = _testGroup.Get("TestPrefab");
|
|
161
|
+
_testGroup.Return(obj);
|
|
162
|
+
}
|
|
163
|
+
})
|
|
164
|
+
.WarmupCount(5)
|
|
165
|
+
.MeasurementCount(10)
|
|
166
|
+
.IterationsPerMeasurement(1)
|
|
167
|
+
.GC()
|
|
168
|
+
.Run();
|
|
169
|
+
|
|
170
|
+
var poolObjectNoParent = new GameObject("TestPoolNoParent");
|
|
171
|
+
var poolNoParent = poolObjectNoParent.AddComponent<PrefabPool>();
|
|
172
|
+
|
|
173
|
+
var prefabNoParent = new GameObject("TestPrefabNoParent");
|
|
174
|
+
prefabNoParent.AddComponent<MockPoolPrefab>();
|
|
175
|
+
|
|
176
|
+
var groupNoParent = new PoolGroup
|
|
177
|
+
{
|
|
178
|
+
id = "TestGroupNoParent",
|
|
179
|
+
entries = new System.Collections.Generic.List<PoolEntry>
|
|
180
|
+
{
|
|
181
|
+
new PoolEntry
|
|
182
|
+
{
|
|
183
|
+
id = "TestPrefab",
|
|
184
|
+
prefab = prefabNoParent,
|
|
185
|
+
prewarmCount = 10
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
poolNoParent.RegisterGroup(groupNoParent, null);
|
|
191
|
+
yield return poolNoParent.Initialize();
|
|
192
|
+
|
|
193
|
+
Measure.Method(() =>
|
|
194
|
+
{
|
|
195
|
+
for (int i = 0; i < 100; i++)
|
|
196
|
+
{
|
|
197
|
+
var obj = groupNoParent.Get("TestPrefab");
|
|
198
|
+
groupNoParent.Return(obj);
|
|
199
|
+
}
|
|
200
|
+
})
|
|
201
|
+
.WarmupCount(5)
|
|
202
|
+
.MeasurementCount(10)
|
|
203
|
+
.IterationsPerMeasurement(1)
|
|
204
|
+
.GC()
|
|
205
|
+
.Run();
|
|
206
|
+
|
|
207
|
+
poolNoParent.DestroyPool();
|
|
208
|
+
UnityObject.DestroyImmediate(poolObjectNoParent);
|
|
209
|
+
UnityObject.DestroyImmediate(prefabNoParent);
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
using NUnit.Framework;
|
|
2
|
+
using Amanotes.Core;
|
|
3
|
+
|
|
4
|
+
namespace Amanotes.Core
|
|
5
|
+
{
|
|
6
|
+
public class PrefabPoolTestsSetup
|
|
7
|
+
{
|
|
8
|
+
[Test]
|
|
9
|
+
public void TestInfrastructureVerification()
|
|
10
|
+
{
|
|
11
|
+
Assert.IsTrue(true, "PrefabPool test infrastructure is working");
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
using UnityEngine;
|
|
2
|
+
|
|
3
|
+
namespace Amanotes.Core
|
|
4
|
+
{
|
|
5
|
+
public class MockPoolPrefab : MonoBehaviour, IPoolPrefab
|
|
6
|
+
{
|
|
7
|
+
public int getCallCount;
|
|
8
|
+
public int returnCallCount;
|
|
9
|
+
|
|
10
|
+
public void OnGet()
|
|
11
|
+
{
|
|
12
|
+
getCallCount++;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public void OnReturn()
|
|
16
|
+
{
|
|
17
|
+
returnCallCount++;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
public void ResetCounts()
|
|
21
|
+
{
|
|
22
|
+
getCallCount = 0;
|
|
23
|
+
returnCallCount = 0;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
}
|
package/Tests.meta
ADDED
package/package.json
CHANGED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "Amanotes.PrefabPool",
|
|
3
|
-
"rootNamespace": "Amanotes.Core",
|
|
4
|
-
"references": [],
|
|
5
|
-
"includePlatforms": [],
|
|
6
|
-
"excludePlatforms": [],
|
|
7
|
-
"allowUnsafeCode": false,
|
|
8
|
-
"overrideReferences": false,
|
|
9
|
-
"precompiledReferences": [],
|
|
10
|
-
"autoReferenced": true,
|
|
11
|
-
"defineConstraints": [],
|
|
12
|
-
"versionDefines": [],
|
|
13
|
-
"noEngineReferences": false
|
|
14
|
-
}
|
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
using System.Collections.Generic;
|
|
2
|
-
using UnityEditor;
|
|
3
|
-
using UnityEngine;
|
|
4
|
-
|
|
5
|
-
namespace Amanotes.Core
|
|
6
|
-
{
|
|
7
|
-
public static class MeshRendererExtensions
|
|
8
|
-
{
|
|
9
|
-
public static void SetAlpha(this List<SpriteRenderer> sprites, float value)
|
|
10
|
-
{
|
|
11
|
-
for (var i = 0; i < sprites.Count; i++)
|
|
12
|
-
{
|
|
13
|
-
var sprt = sprites[i];
|
|
14
|
-
if (sprt == null) continue;
|
|
15
|
-
|
|
16
|
-
var c = sprt.color;
|
|
17
|
-
c.a = value;
|
|
18
|
-
sprt.color = c;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
public static void SetColor(this List<MeshRenderer> renderers, Color c, bool keepAlpha = true)
|
|
24
|
-
{
|
|
25
|
-
for (var i = 0; i < renderers.Count; i++)
|
|
26
|
-
{
|
|
27
|
-
renderers[i].SetColor(c, keepAlpha);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
public static void SetColor(this List<SpriteRenderer> sprites, Color c, bool keepAlpha = true)
|
|
32
|
-
{
|
|
33
|
-
for (var i = 0; i < sprites.Count; i++)
|
|
34
|
-
{
|
|
35
|
-
sprites[i].SetColor(c, keepAlpha);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
public static void SetColor(this MeshRenderer renderer, Color c, bool keepAlpha = true)
|
|
40
|
-
{
|
|
41
|
-
if (renderer == null) return;
|
|
42
|
-
var m = renderer.sharedMaterial;
|
|
43
|
-
if (m == null) return;
|
|
44
|
-
|
|
45
|
-
if (keepAlpha) c.a = m.color.a;
|
|
46
|
-
renderer.sharedMaterial.color = c;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
public static void SetColor(this SpriteRenderer sprt, Color c, bool keepAlpha = true)
|
|
50
|
-
{
|
|
51
|
-
if (sprt == null) return;
|
|
52
|
-
if (keepAlpha) c.a = sprt.color.a;
|
|
53
|
-
sprt.color = c;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
public static void SetAlpha(this SpriteRenderer sprt, float alpha)
|
|
57
|
-
{
|
|
58
|
-
if (sprt == null) return;
|
|
59
|
-
var c = sprt.color;
|
|
60
|
-
c.a = sprt.color.a;
|
|
61
|
-
sprt.color = c;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
public static void CloneAndSetAlpha(this MeshRenderer renderer, float alpha)
|
|
65
|
-
{
|
|
66
|
-
// Prevent accidentally changes to prefab's material
|
|
67
|
-
#if UNITY_EDITOR
|
|
68
|
-
if (PrefabUtility.IsPartOfPrefabAsset(renderer)) return;
|
|
69
|
-
if (!Application.isPlaying) return;
|
|
70
|
-
#endif
|
|
71
|
-
if (renderer == null) return;
|
|
72
|
-
|
|
73
|
-
var oMat = renderer.sharedMaterial;
|
|
74
|
-
var cMat = MaterialPool.Take(oMat, renderer.GetInstanceID());
|
|
75
|
-
renderer.sharedMaterial = cMat;
|
|
76
|
-
|
|
77
|
-
cMat.SetAlpha(alpha);
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
}
|
|
File without changes
|