@@ -5,11 +5,13 @@ namespace ToolBox.Pools
5
5
{
6
6
public sealed class Pool
7
7
{
8
- private Poolable _prefab = null ;
9
- private Stack < Poolable > _instances = null ;
8
+ private readonly Poolable _prefab = null ;
9
+ private readonly Stack < Poolable > _instances = null ;
10
+ private readonly Quaternion _rotation = default ;
11
+ private readonly Vector3 _scale = default ;
10
12
11
- private static Dictionary < int , Pool > _prefabLookup = new Dictionary < int , Pool > ( ) ;
12
- private static Dictionary < int , Pool > _instanceLookup = new Dictionary < int , Pool > ( ) ;
13
+ private static readonly Dictionary < int , Pool > _prefabLookup = new Dictionary < int , Pool > ( ) ;
14
+ private static readonly Dictionary < int , Pool > _instanceLookup = new Dictionary < int , Pool > ( ) ;
13
15
14
16
public Pool ( GameObject prefab )
15
17
{
@@ -24,6 +26,10 @@ public Pool(GameObject prefab)
24
26
25
27
_instances = new Stack < Poolable > ( ) ;
26
28
_prefabLookup . Add ( prefab . GetHashCode ( ) , this ) ;
29
+
30
+ var transform = prefab . transform ;
31
+ _rotation = transform . rotation ;
32
+ _scale = transform . localScale ;
27
33
}
28
34
29
35
public static Pool GetPrefabPool ( GameObject prefab )
@@ -91,12 +97,16 @@ public GameObject Get(Vector3 position, Quaternion rotation, Transform parent)
91
97
92
98
public void Release ( GameObject instance )
93
99
{
94
- var poolable = instance . GetComponent < Poolable > ( ) ;
95
- _instances . Push ( poolable ) ;
96
-
97
- instance . transform . SetParent ( null ) ;
98
100
instance . SetActive ( false ) ;
101
+
102
+ var instanceTransform = instance . transform ;
103
+ instanceTransform . SetParent ( null ) ;
104
+ instanceTransform . rotation = _rotation ;
105
+ instanceTransform . localScale = _scale ;
106
+
107
+ var poolable = instance . GetComponent < Poolable > ( ) ;
99
108
poolable . OnRelease ( ) ;
109
+ _instances . Push ( poolable ) ;
100
110
}
101
111
102
112
private Poolable GetInstance ( )
0 commit comments