@@ -6,7 +6,7 @@ namespace ToolBox.Pools
66 public sealed class Pool
77 {
88 private Poolable _prefab = null ;
9- private Stack < Poolable > _entities = null ;
9+ private Stack < Poolable > _instances = null ;
1010
1111 private static Dictionary < int , Pool > _prefabLookup = new Dictionary < int , Pool > ( ) ;
1212 private static Dictionary < int , Pool > _instanceLookup = new Dictionary < int , Pool > ( ) ;
@@ -22,7 +22,7 @@ public Pool(GameObject prefab)
2222 _prefab . gameObject . SetActive ( false ) ;
2323 }
2424
25- _entities = new Stack < Poolable > ( ) ;
25+ _instances = new Stack < Poolable > ( ) ;
2626 _prefabLookup . Add ( prefab . GetHashCode ( ) , this ) ;
2727 }
2828
@@ -46,47 +46,46 @@ public void Populate(int count)
4646 {
4747 for ( int i = 0 ; i < count ; i ++ )
4848 {
49- var entity = Object . Instantiate ( _prefab ) ;
50- _entities . Push ( entity ) ;
51- _instanceLookup . Add ( entity . gameObject . GetHashCode ( ) , this ) ;
52- entity . gameObject . SetActive ( false ) ;
49+ var instance = CreateInstance ( ) ;
50+ _instances . Push ( instance ) ;
51+ instance . gameObject . SetActive ( false ) ;
5352 }
5453 }
5554
5655 public GameObject Get ( )
5756 {
58- var entity = GetEntityFromPool ( ) ;
57+ var instance = GetInstance ( ) ;
5958
60- return entity . gameObject ;
59+ return instance . gameObject ;
6160 }
6261
6362 public GameObject Get ( Transform parent , bool spawnInWorldSpace )
6463 {
65- var entity = GetEntityFromPool ( ) ;
64+ var instance = GetInstance ( ) ;
6665
67- entity . transform . SetParent ( parent , spawnInWorldSpace ) ;
66+ instance . transform . SetParent ( parent , spawnInWorldSpace ) ;
6867
69- return entity . gameObject ;
68+ return instance . gameObject ;
7069 }
7170
7271 public GameObject Get ( Vector3 position , Quaternion rotation )
7372 {
74- var entity = GetEntityFromPool ( ) ;
73+ var instance = GetInstance ( ) ;
7574
76- entity . transform . SetPositionAndRotation ( position , rotation ) ;
75+ instance . transform . SetPositionAndRotation ( position , rotation ) ;
7776
78- return entity . gameObject ;
77+ return instance . gameObject ;
7978 }
8079
8180 public GameObject Get ( Vector3 position , Quaternion rotation , Transform parent , bool spawnInWorldSpace )
8281 {
83- var entity = GetEntityFromPool ( ) ;
84- var entityTransform = entity . transform ;
82+ var instance = GetInstance ( ) ;
83+ var instanceTransform = instance . transform ;
8584
86- entityTransform . SetParent ( parent , spawnInWorldSpace ) ;
87- entityTransform . SetPositionAndRotation ( position , rotation ) ;
85+ instanceTransform . SetParent ( parent , spawnInWorldSpace ) ;
86+ instanceTransform . SetPositionAndRotation ( position , rotation ) ;
8887
89- return entity . gameObject ;
88+ return instance . gameObject ;
9089 }
9190
9291 public T Get < T > ( ) where T : Component =>
@@ -101,43 +100,45 @@ public T Get<T>(Vector3 position, Quaternion rotation) where T : Component =>
101100 public T Get < T > ( Vector3 position , Quaternion rotation , Transform parent , bool spawnInWorldSpace ) where T : Component =>
102101 Get ( position , rotation , parent , spawnInWorldSpace ) . GetComponent < T > ( ) ;
103102
104- public void Release ( Poolable entity )
103+ public void Release ( Poolable instance )
105104 {
106- _entities . Push ( entity ) ;
105+ _instances . Push ( instance ) ;
107106
108- entity . transform . SetParent ( null , false ) ;
109- entity . gameObject . SetActive ( false ) ;
110- entity . OnRelease ( ) ;
107+ instance . transform . SetParent ( null , false ) ;
108+ instance . gameObject . SetActive ( false ) ;
109+ instance . OnRelease ( ) ;
111110 }
112111
113- private Poolable GetEntityFromPool ( )
112+ private Poolable GetInstance ( )
114113 {
115- if ( _entities . Count == 0 )
114+ if ( _instances . Count == 0 )
116115 {
117- var entity = Object . Instantiate ( _prefab ) ;
118- _instanceLookup . Add ( entity . gameObject . GetHashCode ( ) , this ) ;
119- entity . gameObject . SetActive ( true ) ;
116+ var instance = CreateInstance ( ) ;
117+ instance . gameObject . SetActive ( true ) ;
120118
121- return entity ;
119+ return instance ;
122120 }
123121 else
124122 {
125- var entity = _entities . Pop ( ) ;
123+ var instance = _instances . Pop ( ) ;
126124
127- if ( entity == null )
128- {
129- entity = Object . Instantiate ( _prefab ) ;
130- _instanceLookup . Add ( entity . gameObject . GetHashCode ( ) , this ) ;
131- }
125+ if ( instance == null )
126+ instance = CreateInstance ( ) ;
132127 else
133- {
134- entity . OnGet ( ) ;
135- }
128+ instance . OnGet ( ) ;
136129
137- entity . gameObject . SetActive ( true ) ;
130+ instance . gameObject . SetActive ( true ) ;
138131
139- return entity ;
132+ return instance ;
140133 }
141134 }
135+
136+ private Poolable CreateInstance ( )
137+ {
138+ var entity = Object . Instantiate ( _prefab ) ;
139+ _instanceLookup . Add ( entity . gameObject . GetHashCode ( ) , this ) ;
140+
141+ return entity ;
142+ }
142143 }
143144}
0 commit comments