//Credit to starikcetin via GitHub: https://gist.github.com/starikcetin/583a3b86c22efae35b5a86e9ae23f2f0 using System; using System.Diagnostics; using System.Reflection; using UnityEditor; namespace DistantLands.Cozy.EditorScripts { public static class CozySearchUtils { private const BindingFlags AllBindingFlags = (BindingFlags)(-1); /// /// Returns attributes of type on . /// public static TAttribute[] GetAttributes(this SerializedProperty serializedProperty, bool inherit) where TAttribute : Attribute { if (serializedProperty == null) { throw new ArgumentNullException(nameof(serializedProperty)); } var targetObjectType = serializedProperty.serializedObject.targetObject.GetType(); if (targetObjectType == null) { throw new ArgumentException($"Could not find the {nameof(targetObjectType)} of {nameof(serializedProperty)}"); } foreach (var pathSegment in serializedProperty.propertyPath.Split('.')) { var fieldInfo = targetObjectType.GetField(pathSegment, AllBindingFlags); if (fieldInfo != null) { return (TAttribute[])fieldInfo.GetCustomAttributes(inherit); } var propertyInfo = targetObjectType.GetProperty(pathSegment, AllBindingFlags); if (propertyInfo != null) { return (TAttribute[])propertyInfo.GetCustomAttributes(inherit); } } return new TAttribute[0]; } } }