@@ -7,21 +7,17 @@ namespace ToolBox.Pools
77 [ System . Serializable ]
88 public class Pool
99 {
10- [ SerializeField , AssetsOnly , TabGroup ( "Data" ) , ValueDropdown ( nameof ( GetPoolables ) ) ] private Poolable prefab = null ;
11- [ SerializeField , TabGroup ( "Data" ) ] private int startCount = 0 ;
12- [ SerializeField , TabGroup ( "Data" ) ] private bool isResizable = false ;
13- [ SerializeField , SceneObjectsOnly , TabGroup ( "Data" ) ] private Transform holder = null ;
14-
15- [ SerializeField , ReadOnly , TabGroup ( "Debug" ) ] private bool isFilled = false ;
16- [ SerializeField , ReadOnly , TabGroup ( "Debug" ) ] private int currentCount = 0 ;
10+ [ SerializeField , AssetsOnly , ValueDropdown ( nameof ( GetPoolables ) ) ] private Poolable prefab = null ;
11+ [ SerializeField ] private int startCount = 0 ;
12+ [ SerializeField , SceneObjectsOnly ] private Transform holder = null ;
13+ [ SerializeField , ReadOnly ] private int currentCount = 0 ;
1714
1815 private Queue < Poolable > entities = null ;
1916
20- public Pool ( Poolable prefab , int startCount , bool isResizable , Transform holder )
17+ public Pool ( Poolable prefab , int startCount , Transform holder )
2118 {
2219 this . prefab = prefab ;
2320 this . startCount = startCount ;
24- this . isResizable = isResizable ;
2521 this . holder = holder ;
2622 }
2723
@@ -30,30 +26,28 @@ private IEnumerable<Poolable> GetPoolables() =>
3026
3127 public void Fill ( )
3228 {
33- if ( isFilled )
34- return ;
35-
3629 entities = new Queue < Poolable > ( startCount ) ;
3730 currentCount = startCount ;
3831
39- for ( int i = 0 ; i < startCount ; i ++ )
32+ Poolable original = CreateObject ( prefab ) ;
33+
34+ for ( int i = 0 ; i < startCount - 1 ; i ++ )
35+ CreateObject ( original ) ;
36+
37+ Poolable CreateObject ( Poolable prototype )
4038 {
41- Poolable entity = Object . Instantiate ( prefab , holder ) ;
39+ Poolable entity = Object . Instantiate ( prototype , holder ) ;
4240 entity . SetPool ( this ) ;
4341 entities . Enqueue ( entity ) ;
4442 entity . gameObject . SetActive ( false ) ;
45- }
4643
47- isFilled = true ;
44+ return entity ;
45+ }
4846 }
4947
5048 public Poolable GetEntity ( )
5149 {
5250 Poolable entity = TakeEntity ( ) ;
53-
54- if ( IsEmpty ( entity ) )
55- return null ;
56-
5751 entity . ReturnFromPool ( ) ;
5852
5953 return entity ;
@@ -63,9 +57,6 @@ public Poolable GetEntity(Transform parent, bool spawnInWorldSpace)
6357 {
6458 Poolable entity = TakeEntity ( ) ;
6559
66- if ( IsEmpty ( entity ) )
67- return null ;
68-
6960 entity . transform . SetParent ( parent , spawnInWorldSpace ) ;
7061 entity . ReturnFromPool ( ) ;
7162
@@ -76,9 +67,6 @@ public Poolable GetEntity(Vector3 position, Quaternion rotation)
7667 {
7768 Poolable entity = TakeEntity ( ) ;
7869
79- if ( IsEmpty ( entity ) )
80- return null ;
81-
8270 entity . transform . SetPositionAndRotation ( position , rotation ) ;
8371 entity . ReturnFromPool ( ) ;
8472
@@ -88,10 +76,6 @@ public Poolable GetEntity(Vector3 position, Quaternion rotation)
8876 public Poolable GetEntity ( Vector3 position , Quaternion rotation , Transform parent , bool spawnInWorldSpace )
8977 {
9078 Poolable entity = TakeEntity ( ) ;
91-
92- if ( IsEmpty ( entity ) )
93- return null ;
94-
9579 Transform entityTransform = entity . transform ;
9680
9781 entityTransform . SetParent ( parent , spawnInWorldSpace ) ;
@@ -127,23 +111,13 @@ public void ReturnEntity(Poolable entity)
127111
128112 private Poolable TakeEntity ( )
129113 {
130- if ( ! isFilled )
131- Fill ( ) ;
132-
133114 Poolable entity ;
134115
135116 if ( currentCount == 0 )
136117 {
137- if ( isResizable )
138- {
139- entity = Object . Instantiate ( prefab , holder ) ;
140- entity . SetPool ( this ) ;
141- return entity ;
142- }
143- else
144- {
145- return null ;
146- }
118+ entity = Object . Instantiate ( prefab , holder ) ;
119+ entity . SetPool ( this ) ;
120+ return entity ;
147121 }
148122
149123 entity = entities . Dequeue ( ) ;
@@ -160,8 +134,5 @@ private Poolable TakeEntity()
160134
161135 return entity ;
162136 }
163-
164- private bool IsEmpty ( Poolable entity ) =>
165- ! isResizable && entity == null ;
166137 }
167138}
0 commit comments