Skip to content

Instantly share code, notes, and snippets.

@thebeardphantom
Created March 4, 2021 23:34
Show Gist options
  • Save thebeardphantom/1ad9aee0ef8de6271fff39f1a6a3d66d to your computer and use it in GitHub Desktop.
Save thebeardphantom/1ad9aee0ef8de6271fff39f1a6a3d66d to your computer and use it in GitHub Desktop.
Find FieldInfo for a given SerializedProperty.
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