@@ -8,8 +8,8 @@ public sealed class Pool
88 private Poolable _prefab = null ;
99 private Stack < Poolable > _instances = null ;
1010
11- private static Dictionary < int , Pool > _prefabLookup = new Dictionary < int , Pool > ( ) ;
12- private static Dictionary < int , Pool > _instanceLookup = new Dictionary < int , Pool > ( ) ;
11+ private static Dictionary < GameObject , Pool > _prefabLookup = new Dictionary < GameObject , Pool > ( ) ;
12+ private static Dictionary < GameObject , Pool > _instanceLookup = new Dictionary < GameObject , Pool > ( ) ;
1313
1414 public Pool ( GameObject prefab )
1515 {
@@ -23,41 +23,33 @@ public Pool(GameObject prefab)
2323 }
2424
2525 _instances = new Stack < Poolable > ( ) ;
26- _prefabLookup . Add ( prefab . GetHashCode ( ) , this ) ;
26+ _prefabLookup . Add ( prefab , this ) ;
2727 }
2828
2929 public static Pool GetPrefabPool ( GameObject prefab )
3030 {
31- bool hasPool = _prefabLookup . TryGetValue ( prefab . GetHashCode ( ) , out var pool ) ;
31+ bool hasPool = _prefabLookup . TryGetValue ( prefab , out var pool ) ;
3232
3333 if ( ! hasPool )
3434 pool = new Pool ( prefab ) ;
3535
3636 return pool ;
3737 }
3838
39- public static Pool GetInstancePool ( GameObject instance )
40- {
41- _instanceLookup . TryGetValue ( instance . GetHashCode ( ) , out var pool ) ;
42- return pool ;
43- }
39+ public static bool TryGetInstancePool ( GameObject instance , out Pool pool ) =>
40+ _instanceLookup . TryGetValue ( instance , out pool ) ;
4441
4542 public void Populate ( int count )
4643 {
4744 for ( int i = 0 ; i < count ; i ++ )
4845 {
4946 var instance = CreateInstance ( ) ;
5047 _instances . Push ( instance ) ;
51- instance . gameObject . SetActive ( false ) ;
5248 }
5349 }
5450
55- public GameObject Get ( )
56- {
57- var instance = GetInstance ( ) ;
58-
59- return instance . gameObject ;
60- }
51+ public GameObject Get ( ) =>
52+ GetInstance ( ) . gameObject ;
6153
6254 public GameObject Get ( Transform parent , bool spawnInWorldSpace )
6355 {
@@ -100,45 +92,44 @@ public T Get<T>(Vector3 position, Quaternion rotation) where T : Component =>
10092 public T Get < T > ( Vector3 position , Quaternion rotation , Transform parent , bool spawnInWorldSpace ) where T : Component =>
10193 Get ( position , rotation , parent , spawnInWorldSpace ) . GetComponent < T > ( ) ;
10294
103- public void Release ( Poolable instance )
95+ public void Release ( GameObject instance )
10496 {
105- _instances . Push ( instance ) ;
97+ var poolable = instance . GetComponent < Poolable > ( ) ;
98+ _instances . Push ( poolable ) ;
10699
107100 instance . transform . SetParent ( null , false ) ;
108- instance . gameObject . SetActive ( false ) ;
109- instance . OnRelease ( ) ;
101+ instance . SetActive ( false ) ;
102+ poolable . OnRelease ( ) ;
110103 }
111104
112105 private Poolable GetInstance ( )
113106 {
107+ Poolable instance ;
108+
114109 if ( _instances . Count == 0 )
115110 {
116- var instance = CreateInstance ( ) ;
117- instance . gameObject . SetActive ( true ) ;
118-
119- return instance ;
111+ instance = CreateInstance ( ) ;
120112 }
121113 else
122114 {
123- var instance = _instances . Pop ( ) ;
115+ instance = _instances . Pop ( ) ;
124116
125117 if ( instance == null )
126118 instance = CreateInstance ( ) ;
127119 else
128120 instance . OnGet ( ) ;
129-
130- instance . gameObject . SetActive ( true ) ;
131-
132- return instance ;
133121 }
122+
123+ instance . gameObject . SetActive ( true ) ;
124+ return instance ;
134125 }
135126
136127 private Poolable CreateInstance ( )
137128 {
138- var entity = Object . Instantiate ( _prefab ) ;
139- _instanceLookup . Add ( entity . gameObject . GetHashCode ( ) , this ) ;
129+ var instance = Object . Instantiate ( _prefab ) ;
130+ _instanceLookup . Add ( instance . gameObject , this ) ;
140131
141- return entity ;
132+ return instance ;
142133 }
143134 }
144135}
0 commit comments