@@ -160,38 +160,56 @@ public static int GetRootCount(int handle)
160
160
. Invoke ( handle ) ;
161
161
}
162
162
163
- internal static bool ? s_doPropertiesExist ;
163
+ internal static bool triedToGetProperties ;
164
+ internal static PropertyInfo _normalColorProp ;
165
+ internal static PropertyInfo _highlightColorProp ;
166
+ internal static PropertyInfo _pressedColorProp ;
164
167
165
168
public override ColorBlock SetColorBlock ( ColorBlock colors , Color ? normal = null , Color ? highlighted = null , Color ? pressed = null )
166
169
{
167
- if ( s_doPropertiesExist == null )
168
- {
169
- var prop = ReflectionUtility . GetPropertyInfo ( typeof ( ColorBlock ) , "normalColor" ) as PropertyInfo ;
170
- s_doPropertiesExist = prop != null && prop . CanWrite ;
171
- }
172
-
173
170
colors . colorMultiplier = 1 ;
174
171
175
172
object boxed = ( object ) colors ;
176
173
177
- if ( s_doPropertiesExist == true )
174
+ if ( ! triedToGetProperties )
178
175
{
179
- if ( normal != null )
180
- ReflectionUtility . GetPropertyInfo ( typeof ( ColorBlock ) , "normalColor" ) . SetValue ( boxed , ( Color ) normal ) ;
181
- if ( pressed != null )
182
- ReflectionUtility . GetPropertyInfo ( typeof ( ColorBlock ) , "pressedColor" ) . SetValue ( boxed , ( Color ) pressed ) ;
183
- if ( highlighted != null )
184
- ReflectionUtility . GetPropertyInfo ( typeof ( ColorBlock ) , "highlightedColor" ) . SetValue ( boxed , ( Color ) highlighted ) ;
176
+ triedToGetProperties = true ;
177
+
178
+ if ( ReflectionUtility . GetPropertyInfo ( typeof ( ColorBlock ) , "normalColor" ) is PropertyInfo norm && norm . CanWrite )
179
+ _normalColorProp = norm ;
180
+ if ( ReflectionUtility . GetPropertyInfo ( typeof ( ColorBlock ) , "highlightedColor" ) is PropertyInfo high && high . CanWrite )
181
+ _highlightColorProp = high ;
182
+ if ( ReflectionUtility . GetPropertyInfo ( typeof ( ColorBlock ) , "pressedColor" ) is PropertyInfo pres && pres . CanWrite )
183
+ _pressedColorProp = pres ;
185
184
}
186
- else if ( s_doPropertiesExist == false )
185
+
186
+ try
187
187
{
188
188
if ( normal != null )
189
- ReflectionUtility . GetFieldInfo ( typeof ( ColorBlock ) , "m_NormalColor" ) . SetValue ( boxed , ( Color ) normal ) ;
190
- if ( pressed != null )
191
- ReflectionUtility . GetFieldInfo ( typeof ( ColorBlock ) , "m_PressedColor" ) . SetValue ( boxed , ( Color ) pressed ) ;
189
+ {
190
+ if ( _normalColorProp != null )
191
+ _normalColorProp . SetValue ( boxed , ( Color ) normal ) ;
192
+ else if ( ReflectionUtility . GetFieldInfo ( typeof ( ColorBlock ) , "m_NormalColor" ) is FieldInfo fi )
193
+ fi . SetValue ( boxed , ( Color ) normal ) ;
194
+ }
195
+
192
196
if ( highlighted != null )
193
- ReflectionUtility . GetFieldInfo ( typeof ( ColorBlock ) , "m_HighlightedColor" ) . SetValue ( boxed , ( Color ) highlighted ) ;
197
+ {
198
+ if ( _highlightColorProp != null )
199
+ _highlightColorProp . SetValue ( boxed , ( Color ) highlighted ) ;
200
+ else if ( ReflectionUtility . GetFieldInfo ( typeof ( ColorBlock ) , "m_HighlightedColor" ) is FieldInfo fi )
201
+ fi . SetValue ( boxed , ( Color ) highlighted ) ;
202
+ }
203
+
204
+ if ( pressed != null )
205
+ {
206
+ if ( _pressedColorProp != null )
207
+ _pressedColorProp . SetValue ( boxed , ( Color ) pressed ) ;
208
+ else if ( ReflectionUtility . GetFieldInfo ( typeof ( ColorBlock ) , "m_PressedColor" ) is FieldInfo fi )
209
+ fi . SetValue ( boxed , ( Color ) pressed ) ;
210
+ }
194
211
}
212
+ catch { }
195
213
196
214
colors = ( ColorBlock ) boxed ;
197
215
0 commit comments