Created
March 4, 2021 23:34
-
-
Save thebeardphantom/1ad9aee0ef8de6271fff39f1a6a3d66d to your computer and use it in GitHub Desktop.
Find FieldInfo for a given SerializedProperty.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class SerializedPropertyUtility | |
{ | |
private static readonly MethodInfo getFieldInfoFromProperty; | |
static SerializedPropertyUtility() | |
{ | |
var scriptAttributeUtility = typeof(UnityEditor.Editor).Assembly.GetType("UnityEditor.ScriptAttributeUtility"); | |
Assert.IsNotNull(scriptAttributeUtility, "ScriptAttributeUtility != null"); | |
getFieldInfoFromProperty = scriptAttributeUtility.GetMethod(nameof(GetFieldInfoFromProperty), BindingFlags.NonPublic | BindingFlags.Static); | |
Assert.IsNotNull(getFieldInfoFromProperty, "getFieldInfoFromProperty != null"); | |
} | |
public static FieldInfo GetFieldInfoFromProperty(this SerializedProperty property, out Type type) | |
{ | |
type = null; | |
var fieldInfo = (FieldInfo)getFieldInfoFromProperty.Invoke(null, | |
new object[] | |
{ | |
property, type | |
}); | |
return fieldInfo; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment