11using Sirenix . OdinInspector ;
22using System . Collections . Generic ;
3+ using ToolBox . Modules ;
34using UnityEngine ;
45
56namespace ToolBox . Pools
67{
7- [ System . Serializable ]
88 public class Pool
99 {
1010 [ SerializeField , AssetsOnly , ValueDropdown ( nameof ( GetPoolables ) ) ] private Poolable prefab = null ;
1111 [ SerializeField ] private int startCount = 0 ;
1212 [ SerializeField , SceneObjectsOnly ] private Transform holder = null ;
13- [ SerializeField , ReadOnly ] private int currentCount = 0 ;
13+ [ SerializeField ] private ModulesContainer < GameObject > objectInitializator = null ;
1414
15+ private int currentCount = 0 ;
1516 private Queue < Poolable > entities = null ;
1617
17- public Pool ( Poolable prefab , int startCount , Transform holder )
18+ public Pool ( Poolable prefab , int startCount , Transform holder , ModulesContainer < GameObject > objectInitializator )
1819 {
1920 this . prefab = prefab ;
2021 this . startCount = startCount ;
2122 this . holder = holder ;
23+ this . objectInitializator = objectInitializator ;
2224 }
2325
2426 private IEnumerable < Poolable > GetPoolables ( ) =>
@@ -29,19 +31,22 @@ public void Fill()
2931 entities = new Queue < Poolable > ( startCount ) ;
3032 currentCount = startCount ;
3133
32- Poolable original = CreateObject ( prefab ) ;
34+ Poolable original = Object . Instantiate ( prefab , holder ) ;
35+ objectInitializator . Process ( original . gameObject ) ;
3336
34- for ( int i = 0 ; i < startCount - 1 ; i ++ )
35- CreateObject ( original ) ;
37+ AddToPool ( original ) ;
3638
37- Poolable CreateObject ( Poolable prototype )
39+ for ( int i = 0 ; i < startCount - 1 ; i ++ )
3840 {
39- Poolable entity = Object . Instantiate ( prototype , holder ) ;
40- entity . SetPool ( this ) ;
41- entities . Enqueue ( entity ) ;
42- entity . gameObject . SetActive ( false ) ;
41+ Poolable entity = Object . Instantiate ( original , holder ) ;
42+ AddToPool ( entity ) ;
43+ }
4344
44- return entity ;
45+ void AddToPool ( Poolable newEntity )
46+ {
47+ newEntity . SetPool ( this ) ;
48+ entities . Enqueue ( newEntity ) ;
49+ newEntity . gameObject . SetActive ( false ) ;
4550 }
4651 }
4752
@@ -117,6 +122,7 @@ private Poolable TakeEntity()
117122 {
118123 entity = Object . Instantiate ( prefab , holder ) ;
119124 entity . SetPool ( this ) ;
125+
120126 return entity ;
121127 }
122128
0 commit comments