@@ -20,9 +20,7 @@ private void Awake() {
2020 }
2121 #endregion
2222
23- private Dictionary < int , Queue < ObjectInstance > > poolDictionary = new Dictionary < int , Queue < ObjectInstance > > ( ) ;
24- private Dictionary < int , bool > dynamicPoolingDictionary = new Dictionary < int , bool > ( ) ;
25-
23+ private Dictionary < int , ( bool DynamicPooling , Queue < bool > ObjectQueue ) > poolDictionary = new Dictionary < int , ( bool , Queue < bool > ) > ( ) ;
2624
2725 /// <summary>
2826 /// Instantiates and disables the given amount of prefabs and adds them into the poolDictionary.
@@ -37,20 +35,14 @@ public void CreatePool(GameObject prefab, int poolSize, bool dynamicPooling = fa
3735 // Check if the poolDictionary doesn't already contain our prefab pool.
3836 if ( ! poolDictionary . ContainsKey ( poolKey ) ) {
3937 // Add the InstanceID to the poolDictionary.
40- poolDictionary . Add ( poolKey , new Queue < ObjectInstance > ( ) ) ;
38+ poolDictionary . Add ( poolKey , ( dynamicPooling , new Queue < ObjectInstance > ( ) ) ) ;
4139
4240 // Enqueue disabled gameObjects into the Queue equal to the poolSize.
4341 for ( ; poolSize > 0 ; -- i ) {
4442 ObjectInstance newObject = new ObjectInstance ( Instantiate ( prefab ) as GameObject ) ;
45- poolDictionary [ poolKey ] . Enqueue ( newObject ) ;
43+ poolDictionary . GetValueOrDefault ( poolKey ) . ObjectQueue . Enqueue ( newObject ) ;
4644 }
4745 }
48-
49- // Check if the dynamicPoolingDictionary doesn't already contain our expandable bool.
50- if ( ! dynamicPoolingDictionary . ContainsKey ( poolkey ) ) {
51- // Add the InstanceID to the dynamicPoolingDictionary.
52- dynamicPoolingDictionary . Add ( poolKey , dynamicPooling ) ;
53- }
5446 }
5547
5648 /// <summary>
@@ -67,9 +59,9 @@ public void ParentPool(GameObject prefab, Transform parent) {
6759 poolHolder . transform . SetParent ( parent ) ;
6860
6961 // Try to get the ObjectInstance queue from the given poolKey.
70- if ( poolDictionary . TryGetValue ( poolKey , out Queue < ObjectInstance > objectQueue ) ) {
62+ if ( poolDictionary . TryGetValue ( poolKey , out ( bool DynamicPooling , Queue < bool > ObjectQueue ) values ) ) {
7163 // Loop through each objectInstance in the objectQueue.
72- foreach ( var objectInstance in objectQueue ) {
64+ foreach ( var objectInstance in values . ObjectQueue ) {
7365 // Set its parent equal to the newly created poolHolder.
7466 objectInstance . Transform . SetParent ( poolHolder . transform ) ;
7567 }
@@ -90,7 +82,7 @@ public void IncreasePoolSize(GameObject prefab, int difference) {
9082 // Enqueue disabled gameObjects into the Queue equal to the poolSize.
9183 for ( ; difference > 0 ; -- i ) {
9284 ObjectInstance newObject = new ObjectInstance ( Instantiate ( prefab ) as GameObject ) ;
93- poolDictionary [ poolKey ] . Enqueue ( newObject ) ;
85+ poolDictionary . GetValueOrDefault ( poolKey ) . ObjectQueue . Enqueue ( newObject ) ;
9486 }
9587 }
9688 }
@@ -103,11 +95,11 @@ public void IncreasePoolSize(GameObject prefab, int difference) {
10395 public void EnableDynamicPooling ( GameObject prefab , bool dynamicPooling = true ) {
10496 // Get InstanceID of the given gameObject.
10597 int poolKey = prefab . GetInstanceID ( ) ;
106-
107- // Check if the dynamicPoolingDictionary already contains our expandable bool .
108- if ( dynamicPoolingDictionary . ContainsKey ( poolkey ) ) {
98+
99+ // Check if the poolDictionary already contains our prefab pool .
100+ if ( poolDictionary . ContainsKey ( poolKey ) ) {
109101 // Adjust the value of to the expandablePoolDictionar at the given InstanceID.
110- dynamicPoolingDictionary [ poolKey ] = dynamicPooling ;
102+ poolDictionary . GetValueOrDefault ( poolKey ) . DynamicPooling = dynamicPooling ;
111103 }
112104 }
113105
@@ -125,11 +117,12 @@ public void ReuseObject(GameObject prefab, Vector3 position = Vector3.zero, Quat
125117 // Check if the poolDictionary already contains our Prefab.
126118 if ( poolDictionary . ContainsKey ( poolKey ) ) {
127119 // First we try to reuse objects that are invisble.
128- var objectToReuse = poolDictionary [ poolKey ] . Where ( x => ! x . IsVisible ( ) ) . FirstOrDefault ( ) ;
120+ var objectToReuse = poolDictionary . GetValueOrDefault ( poolKey ) . ObjectQueue . Where ( x => ! x ) . FirstOrDefault ( ) ;
121+
129122 if ( objectToReuse == null ) {
130123 objectToReuse = UseDynamicPooledObject ( poolKey ) ;
131124 }
132- poolDictionary [ poolKey ] . Enqueue ( objectToReuse ) ;
125+ poolDictionary . GetValueOrDefault ( poolKey ) . ObjectQueue . Enqueue ( objectToReuse ) ;
133126 objectToReuse . Reuse ( position , rotation , velocity ) ;
134127 }
135128 }
@@ -146,28 +139,25 @@ private GameObject UseDynamicPooledObject(GameObject prefab) {
146139 // Get InstanceID of the given gameObject.
147140 int poolKey = prefab . GetInstanceID ( ) ;
148141
149- // Check if the dynamicPoolingDictionary already contains our expandable bool
142+ // Check if the poolDictionary already contains our expandable bool
150143 // and copy its value into bool dynamicPooling.
151- if ( dynamicPoolingDictionary . TryGetValue ( poolkey , out bool dynamicPooling ) ) {
152- if ( dynamicPooling ) {
144+ if ( poolDictionary . TryGetValue ( poolKey , out ( bool DynamicPooling , Queue < bool > ObjectQueue ) values ) ) {
145+ if ( values . DynamicPooling ) {
153146 // Increase poolSize by the needed amount of new instances.
154147 IncreasePoolSize ( prefab , 1 ) ;
155148 // Return the newly create instance.
156- dynamicObject = poolDictionary [ poolKey ] . Where ( x => ! x . IsVisible ( ) ) . FirstOrDefault ( ) ;
149+ dynamicObject = poolDictionary . GetValueOrDefault ( poolKey ) . ObjectQueue . Where ( x => ! x ) . FirstOrDefault ( ) ;
157150 }
158151 }
159152
160153 // Check if we got a newly instantiated object from our prefab pool.
161154 if ( dynamicObject == null ) {
162- // If not return the last used instance from the prefab pool.
163- dynamicObject = poolDictionary [ poolKey ] . Dequeue ( ) ;
155+ // If not return the last used instance from the prefab pool
156+ dynamicObject = poolDictionary . GetValueOrDefault ( poolKey ) . ObjectQueue . Dequeue ( ) ;
164157 }
165158
166159 return dynamicObject
167160 }
168-
169- // If there are none we just use the first one from our queue.
170- objectToReuse = poolDictionary [ poolKey ] . Dequeue ( ) ;
171161
172162 /// <summary>
173163 /// Handles components needed for managing of the pooled gameobjects.
0 commit comments