@@ -6,144 +6,137 @@ namespace ToolBox.Pools
66 public sealed class Pool
77 {
88 private Poolable _prefab = null ;
9- private Queue < Poolable > _entities = null ;
9+ private Stack < Poolable > _entities = null ;
1010
11- private static Dictionary < int , Pool > _pools = new Dictionary < int , Pool > ( ) ;
11+ private static Dictionary < int , Pool > _prefabLookup = new Dictionary < int , Pool > ( ) ;
12+ private static Dictionary < int , Pool > _instanceLookup = new Dictionary < int , Pool > ( ) ;
1213
13- public Pool ( Poolable prefab , int count )
14- {
15- _prefab = prefab ;
16- _entities = new Queue < Poolable > ( count ) ;
17- _pools . Add ( prefab . gameObject . GetHashCode ( ) , this ) ;
18-
19- Populate ( count ) ;
20- }
21-
22- public Pool ( GameObject prefab , int count )
14+ public Pool ( GameObject prefab )
2315 {
2416 _prefab = prefab . GetComponent < Poolable > ( ) ;
2517
2618 if ( _prefab == null )
2719 {
2820 _prefab = Object . Instantiate ( prefab ) . AddComponent < Poolable > ( ) ;
29- UnityEngine . Object . DontDestroyOnLoad ( _prefab ) ;
21+ Object . DontDestroyOnLoad ( _prefab ) ;
3022 _prefab . gameObject . SetActive ( false ) ;
3123 }
3224
33- _entities = new Queue < Poolable > ( count ) ;
34- _pools . Add ( prefab . GetHashCode ( ) , this ) ;
35-
36- Populate ( count ) ;
25+ _entities = new Stack < Poolable > ( ) ;
26+ _prefabLookup . Add ( prefab . GetHashCode ( ) , this ) ;
3727 }
3828
39- public static Pool Get ( GameObject prefab )
29+ public static Pool GetPrefabPool ( GameObject prefab )
4030 {
41- var hasPool = _pools . TryGetValue ( prefab . GetHashCode ( ) , out var pool ) ;
31+ var hasPool = _prefabLookup . TryGetValue ( prefab . GetHashCode ( ) , out var pool ) ;
4232
4333 if ( ! hasPool )
44- pool = new Pool ( prefab , 0 ) ;
34+ pool = new Pool ( prefab ) ;
4535
4636 return pool ;
4737 }
4838
39+ public static Pool GetInstancePool ( GameObject instance )
40+ {
41+ _instanceLookup . TryGetValue ( instance . GetHashCode ( ) , out var pool ) ;
42+ return pool ;
43+ }
44+
4945 public void Populate ( int count )
5046 {
5147 for ( int i = 0 ; i < count ; i ++ )
5248 {
53- Poolable entity = Object . Instantiate ( _prefab ) ;
54- entity . SetPool ( this ) ;
55- _entities . Enqueue ( entity ) ;
49+ var entity = Object . Instantiate ( _prefab ) ;
50+ _entities . Push ( entity ) ;
5651 entity . gameObject . SetActive ( false ) ;
5752 }
5853 }
5954
60- public Poolable GetEntity ( )
55+ public GameObject Get ( )
6156 {
62- Poolable entity = GetEntityFromPool ( ) ;
63- entity . ReturnFromPool ( ) ;
57+ var entity = GetEntityFromPool ( ) ;
6458
65- return entity ;
59+ return entity . gameObject ;
6660 }
6761
68- public Poolable GetEntity ( Transform parent , bool spawnInWorldSpace )
62+ public GameObject Get ( Transform parent , bool spawnInWorldSpace )
6963 {
70- Poolable entity = GetEntityFromPool ( ) ;
64+ var entity = GetEntityFromPool ( ) ;
7165
7266 entity . transform . SetParent ( parent , spawnInWorldSpace ) ;
73- entity . ReturnFromPool ( ) ;
7467
75- return entity ;
68+ return entity . gameObject ;
7669 }
7770
78- public Poolable GetEntity ( Vector3 position , Quaternion rotation )
71+ public GameObject Get ( Vector3 position , Quaternion rotation )
7972 {
80- Poolable entity = GetEntityFromPool ( ) ;
73+ var entity = GetEntityFromPool ( ) ;
8174
8275 entity . transform . SetPositionAndRotation ( position , rotation ) ;
83- entity . ReturnFromPool ( ) ;
8476
85- return entity ;
77+ return entity . gameObject ;
8678 }
8779
88- public Poolable GetEntity ( Vector3 position , Quaternion rotation , Transform parent , bool spawnInWorldSpace )
80+ public GameObject Get ( Vector3 position , Quaternion rotation , Transform parent , bool spawnInWorldSpace )
8981 {
90- Poolable entity = GetEntityFromPool ( ) ;
91- Transform entityTransform = entity . transform ;
82+ var entity = GetEntityFromPool ( ) ;
83+ var entityTransform = entity . transform ;
9284
9385 entityTransform . SetParent ( parent , spawnInWorldSpace ) ;
9486 entityTransform . SetPositionAndRotation ( position , rotation ) ;
95- entity . ReturnFromPool ( ) ;
9687
97- return entity ;
88+ return entity . gameObject ;
9889 }
9990
100- public T GetEntity < T > ( ) where T : Component =>
101- GetEntity ( ) . GetComponent < T > ( ) ;
91+ public T Get < T > ( ) where T : Component =>
92+ Get ( ) . GetComponent < T > ( ) ;
10293
103- public T GetEntity < T > ( Transform parent , bool spawnInWorldSpace ) where T : Component =>
104- GetEntity ( parent , spawnInWorldSpace ) . GetComponent < T > ( ) ;
94+ public T Get < T > ( Transform parent , bool spawnInWorldSpace ) where T : Component =>
95+ Get ( parent , spawnInWorldSpace ) . GetComponent < T > ( ) ;
10596
106- public T GetEntity < T > ( Vector3 position , Quaternion rotation ) where T : Component =>
107- GetEntity ( position , rotation ) . GetComponent < T > ( ) ;
97+ public T Get < T > ( Vector3 position , Quaternion rotation ) where T : Component =>
98+ Get ( position , rotation ) . GetComponent < T > ( ) ;
10899
109- public T GetEntity < T > ( Vector3 position , Quaternion rotation , Transform parent , bool spawnInWorldSpace ) where T : Component =>
110- GetEntity ( position , rotation , parent , spawnInWorldSpace ) . GetComponent < T > ( ) ;
100+ public T Get < T > ( Vector3 position , Quaternion rotation , Transform parent , bool spawnInWorldSpace ) where T : Component =>
101+ Get ( position , rotation , parent , spawnInWorldSpace ) . GetComponent < T > ( ) ;
111102
112- public void ReturnEntity ( Poolable entity )
103+ public void Release ( Poolable entity )
113104 {
114- if ( entity . Pool != this )
115- return ;
116-
117- _entities . Enqueue ( entity ) ;
105+ _entities . Push ( entity ) ;
118106
119107 entity . transform . SetParent ( null , false ) ;
120108 entity . gameObject . SetActive ( false ) ;
109+ entity . ReturnToPool ( ) ;
121110 }
122111
123112 private Poolable GetEntityFromPool ( )
124113 {
125- Poolable entity ;
126-
127114 if ( _entities . Count == 0 )
128115 {
129- entity = Object . Instantiate ( _prefab ) ;
130- entity . SetPool ( this ) ;
116+ var entity = Object . Instantiate ( _prefab ) ;
117+ _instanceLookup . Add ( entity . gameObject . GetHashCode ( ) , this ) ;
131118 entity . gameObject . SetActive ( true ) ;
132119
133120 return entity ;
134121 }
135-
136- entity = _entities . Dequeue ( ) ;
137-
138- if ( entity == null )
122+ else
139123 {
140- entity = Object . Instantiate ( _prefab ) ;
141- entity . SetPool ( this ) ;
142- }
124+ var entity = _entities . Pop ( ) ;
125+
126+ if ( entity == null )
127+ {
128+ entity = Object . Instantiate ( _prefab ) ;
129+ _instanceLookup . Add ( entity . gameObject . GetHashCode ( ) , this ) ;
130+ }
131+ else
132+ {
133+ entity . ReturnFromPool ( ) ;
134+ }
143135
144- entity . gameObject . SetActive ( true ) ;
136+ entity . gameObject . SetActive ( true ) ;
145137
146- return entity ;
138+ return entity ;
139+ }
147140 }
148141 }
149142}
0 commit comments