|
2 | 2 | Object Pooling for Unity
|
3 | 3 |
|
4 | 4 | ## Features
|
5 |
| -- Faster in terms of performance than Instantiate/Destroy |
| 5 | +- Faster in terms of performance than Instantiate/Destroy (Test at the end of README) |
6 | 6 | - Easy to use
|
7 | 7 | - Easy to integrate with already written spawn systems
|
8 | 8 | - Callbacks OnSpawn & OnDespawn for resseting after object being used
|
@@ -93,3 +93,73 @@ public class Health : MonoBehaviour, IPoolable
|
93 | 93 | public void OnDespawn() { }
|
94 | 94 | }
|
95 | 95 | ```
|
| 96 | + |
| 97 | +### Peformance test: |
| 98 | +Creating and destroying 1000 objects. |
| 99 | + |
| 100 | +#### Instantiate/Destroy: |
| 101 | + |
| 102 | +```csharp |
| 103 | +using Sirenix.OdinInspector; |
| 104 | +using System.Diagnostics; |
| 105 | +using UnityEngine; |
| 106 | + |
| 107 | +public class Tester : MonoBehaviour |
| 108 | +{ |
| 109 | + [SerializeField] private GameObject _object = null; |
| 110 | + |
| 111 | + [Button] |
| 112 | + private void Test() |
| 113 | + { |
| 114 | + Stopwatch stopwatch = new Stopwatch(); |
| 115 | + stopwatch.Start(); |
| 116 | + |
| 117 | + for (int i = 0; i < 1000; i++) |
| 118 | + { |
| 119 | + var instance = Instantiate(_object); |
| 120 | + Destroy(instance); |
| 121 | + } |
| 122 | + |
| 123 | + stopwatch.Stop(); |
| 124 | + print($"Milliseconds: {stopwatch.ElapsedMilliseconds}"); |
| 125 | + } |
| 126 | +} |
| 127 | +``` |
| 128 | +##### Result: [16:26:15] Milliseconds: 6 |
| 129 | + |
| 130 | +#### Spawn/Despawn: |
| 131 | + |
| 132 | +```csharp |
| 133 | +using Sirenix.OdinInspector; |
| 134 | +using System.Diagnostics; |
| 135 | +using ToolBox.Pools; |
| 136 | +using UnityEngine; |
| 137 | + |
| 138 | +public class Tester : MonoBehaviour |
| 139 | +{ |
| 140 | + [SerializeField] private GameObject _object = null; |
| 141 | + |
| 142 | + private void Awake() |
| 143 | + { |
| 144 | + _object.Populate(1000); |
| 145 | + } |
| 146 | + |
| 147 | + [Button] |
| 148 | + private void Test() |
| 149 | + { |
| 150 | + Stopwatch stopwatch = new Stopwatch(); |
| 151 | + stopwatch.Start(); |
| 152 | + |
| 153 | + for (int i = 0; i < 1000; i++) |
| 154 | + { |
| 155 | + var instance = _object.Spawn(); |
| 156 | + instance.Despawn(); |
| 157 | + } |
| 158 | + |
| 159 | + stopwatch.Stop(); |
| 160 | + print($"Milliseconds: {stopwatch.ElapsedMilliseconds}"); |
| 161 | + } |
| 162 | +} |
| 163 | + |
| 164 | +``` |
| 165 | +##### Result: [16:29:36] Milliseconds: 2 |
0 commit comments