Skip to content

Instantly share code, notes, and snippets.

@INeatFreak
Last active January 26, 2024 02:37
Show Gist options
  • Save INeatFreak/b4f48140606f97741e8b26a0766bb8cc to your computer and use it in GitHub Desktop.
Save INeatFreak/b4f48140606f97741e8b26a0766bb8cc to your computer and use it in GitHub Desktop.
Get [Space] Attribute from SerializedProperty
// source: https://forum.unity.com/threads/serialiedproperty-check-if-it-has-a-propertyattribute.436103/
private static T GetPropertyAttribute<T>(SerializedProperty prop, bool inherit) where T : PropertyAttribute
{
if (prop == null)
return null;
Type t = prop.serializedObject.targetObject.GetType();
FieldInfo f = null;
PropertyInfo p = null;
foreach (var name in prop.propertyPath.Split('.')) {
f = t.GetField(name, (BindingFlags)(-1));
if (f == null) {
p = t.GetProperty(name, (BindingFlags)(-1));
if (p == null) {
return null;
}
t = p.PropertyType;
} else {
t = f.FieldType;
}
}
T[] attributes;
if (f != null) {
attributes = f.GetCustomAttributes(typeof(T), inherit) as T[];
} else if (p != null) {
attributes = p.GetCustomAttributes(typeof(T), inherit) as T[];
} else {
return null;
}
return (attributes.Length > 0)? attributes[0] : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment