@@ -10,6 +10,12 @@ namespace Toolbox.Editor
1010{
1111 public static partial class PropertyUtility
1212 {
13+ //NOTE: last non-reflection implementation was ok but support for [SerializeReference] makes it a bit slow
14+ // unfortunately UnityEditor.ScriptAttributeUtility.GetFieldInfoFromProperty is internal so we have to retrive it using reflection
15+ private static readonly MethodInfo getGetFieldInfoFromPropertyMethod =
16+ ReflectionUtility . GetEditorMethod ( "UnityEditor.ScriptAttributeUtility" , "GetFieldInfoFromProperty" ,
17+ BindingFlags . NonPublic | BindingFlags . Static ) ;
18+
1319 /// <summary>
1420 /// Indicates if the property has all changes applied and can be safely used for reflection-based features.
1521 /// </summary>
@@ -249,22 +255,20 @@ public static Type GetScriptTypeFromProperty(SerializedProperty property)
249255 return scriptInstance . GetClass ( ) ;
250256 }
251257
252-
253258 public static FieldInfo GetFieldInfo ( this SerializedProperty property )
254259 {
255260 return GetFieldInfo ( property , out _ ) ;
256261 }
257262
258263 public static FieldInfo GetFieldInfo ( this SerializedProperty property , out Type propertyType )
259264 {
260- return GetFieldInfoFromProperty ( property , out propertyType ) ;
261- }
262-
263- public static FieldInfo GetFieldInfo ( this SerializedProperty property , out Type propertyType , Object target )
264- {
265- return GetFieldInfoFromProperty ( property , out propertyType , target . GetType ( ) ) ;
265+ var parameters = new object [ ] { property , null } ;
266+ var result = getGetFieldInfoFromPropertyMethod . Invoke ( null , parameters ) as FieldInfo ;
267+ propertyType = parameters [ 1 ] as Type ;
268+ return result ;
266269 }
267270
271+ [ Obsolete ( "This method is no longer safe, use GetFieldInfo() instead." ) ]
268272 public static FieldInfo GetFieldInfoFromProperty ( SerializedProperty property , out Type type )
269273 {
270274 var classType = GetScriptTypeFromProperty ( property ) ;
@@ -277,8 +281,11 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
277281 return GetFieldInfoFromProperty ( property , out type , classType ) ;
278282 }
279283
284+ [ Obsolete ( "This method is no longer safe, use GetFieldInfo() instead." ) ]
280285 public static FieldInfo GetFieldInfoFromProperty ( SerializedProperty property , out Type type , Type host )
281286 {
287+ const BindingFlags fieldFlags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ;
288+
282289 FieldInfo field = null ;
283290 type = host ;
284291
@@ -297,7 +304,6 @@ public static FieldInfo GetFieldInfoFromProperty(SerializedProperty property, ou
297304 continue ;
298305 }
299306
300- const BindingFlags fieldFlags = BindingFlags . Instance | BindingFlags . Public | BindingFlags . NonPublic ;
301307 FieldInfo foundField = null ;
302308 for ( var currentType = type ; foundField == null && currentType != null ; currentType = currentType . BaseType )
303309 {
@@ -383,7 +389,7 @@ public static SerializedProperty GetSize(this SerializedProperty array)
383389
384390 public static T GetAttribute < T > ( SerializedProperty property ) where T : Attribute
385391 {
386- return GetAttribute < T > ( property , GetFieldInfoFromProperty ( property , out _ ) ) ;
392+ return GetAttribute < T > ( property , GetFieldInfo ( property , out _ ) ) ;
387393 }
388394
389395 public static T GetAttribute < T > ( SerializedProperty property , FieldInfo fieldInfo ) where T : Attribute
@@ -393,7 +399,7 @@ public static T GetAttribute<T>(SerializedProperty property, FieldInfo fieldInfo
393399
394400 public static T [ ] GetAttributes < T > ( SerializedProperty property ) where T : Attribute
395401 {
396- return GetAttributes < T > ( property , GetFieldInfoFromProperty ( property , out _ ) ) ;
402+ return GetAttributes < T > ( property , GetFieldInfo ( property , out _ ) ) ;
397403 }
398404
399405 public static T [ ] GetAttributes < T > ( SerializedProperty property , FieldInfo fieldInfo ) where T : Attribute
0 commit comments