Last active
January 26, 2024 02:37
-
-
Save INeatFreak/b4f48140606f97741e8b26a0766bb8cc to your computer and use it in GitHub Desktop.
Get [Space] Attribute from SerializedProperty
This file contains hidden or 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
// 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