@@ -10,8 +10,10 @@ public sealed class Pool
10
10
private readonly Quaternion _rotation = default ;
11
11
private readonly Vector3 _scale = default ;
12
12
13
- private static readonly Dictionary < int , Pool > _prefabLookup = new Dictionary < int , Pool > ( ) ;
14
- private static readonly Dictionary < int , Pool > _instanceLookup = new Dictionary < int , Pool > ( ) ;
13
+ private static readonly Dictionary < int , Pool > _prefabLookup = new Dictionary < int , Pool > ( 100 ) ;
14
+ private static readonly Dictionary < int , Pool > _instanceLookup = new Dictionary < int , Pool > ( 10000 ) ;
15
+
16
+ private const int CAPACITY = 100 ;
15
17
16
18
public Pool ( GameObject prefab )
17
19
{
@@ -24,7 +26,7 @@ public Pool(GameObject prefab)
24
26
_prefab . gameObject . SetActive ( false ) ;
25
27
}
26
28
27
- _instances = new Stack < Poolable > ( ) ;
29
+ _instances = new Stack < Poolable > ( CAPACITY ) ;
28
30
_prefabLookup . Add ( prefab . GetHashCode ( ) , this ) ;
29
31
30
32
var transform = prefab . transform ;
@@ -48,42 +50,37 @@ public static bool TryGetInstancePool(GameObject instance, out Pool pool) =>
48
50
public void Populate ( int count )
49
51
{
50
52
for ( int i = 0 ; i < count ; i ++ )
51
- {
52
- var instance = CreateInstance ( ) ;
53
- _instances . Push ( instance ) ;
54
- }
53
+ _instances . Push ( CreateInstance ( ) ) ;
55
54
}
56
55
57
- public GameObject Get ( ) =>
58
- GetInstance ( ) . gameObject ;
59
-
60
- public GameObject Get ( Transform parent )
56
+ public GameObject Get ( )
61
57
{
62
58
var instance = GetInstance ( ) ;
63
59
64
- instance . transform . SetParent ( parent ) ;
65
-
66
60
return instance . gameObject ;
67
61
}
68
62
69
- public GameObject Get ( Transform parent , bool worldPositionStays )
63
+ public GameObject Get ( Transform parent )
70
64
{
71
65
var instance = GetInstance ( ) ;
72
66
73
- instance . transform . SetParent ( parent , worldPositionStays ) ;
67
+ instance . transform . SetParent ( parent ) ;
74
68
75
69
return instance . gameObject ;
76
70
}
77
71
78
- public GameObject Get ( Vector3 position , Quaternion rotation )
72
+ public GameObject Get ( Transform parent , bool worldPositionStays )
79
73
{
80
74
var instance = GetInstance ( ) ;
81
75
82
- instance . transform . SetPositionAndRotation ( position , rotation ) ;
76
+ instance . transform . SetParent ( parent , worldPositionStays ) ;
83
77
84
78
return instance . gameObject ;
85
79
}
86
80
81
+ public GameObject Get ( Vector3 position , Quaternion rotation ) =>
82
+ Get ( position , rotation , null ) ;
83
+
87
84
public GameObject Get ( Vector3 position , Quaternion rotation , Transform parent )
88
85
{
89
86
var instance = GetInstance ( ) ;
@@ -111,24 +108,51 @@ public void Release(GameObject instance)
111
108
112
109
private Poolable GetInstance ( )
113
110
{
114
- Poolable instance ;
111
+ int count = _instances . Count ;
115
112
116
- if ( _instances . Count = = 0 )
113
+ if ( count ! = 0 )
117
114
{
118
- instance = CreateInstance ( ) ;
119
- }
120
- else
121
- {
122
- instance = _instances . Pop ( ) ;
115
+ var instance = _instances . Pop ( ) ;
123
116
124
117
if ( instance == null )
118
+ {
119
+ count -- ;
120
+
121
+ while ( count != 0 )
122
+ {
123
+ instance = _instances . Pop ( ) ;
124
+
125
+ if ( instance != null )
126
+ {
127
+ instance . OnGet ( ) ;
128
+ instance . gameObject . SetActive ( true ) ;
129
+
130
+ return instance ;
131
+ }
132
+
133
+ count -- ;
134
+ }
135
+
125
136
instance = CreateInstance ( ) ;
137
+ instance . gameObject . SetActive ( true ) ;
138
+
139
+ return instance ;
140
+ }
126
141
else
142
+ {
127
143
instance . OnGet ( ) ;
144
+ instance . gameObject . SetActive ( true ) ;
145
+
146
+ return instance ;
147
+ }
128
148
}
149
+ else
150
+ {
151
+ var instance = CreateInstance ( ) ;
152
+ instance . gameObject . SetActive ( true ) ;
129
153
130
- instance . gameObject . SetActive ( true ) ;
131
- return instance ;
154
+ return instance ;
155
+ }
132
156
}
133
157
134
158
private Poolable CreateInstance ( )
0 commit comments