51 lines
1.8 KiB
C#
51 lines
1.8 KiB
C#
//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);
|
|
|
|
/// <summary>
|
|
/// Returns attributes of type <typeparamref name="TAttribute"/> on <paramref name="serializedProperty"/>.
|
|
/// </summary>
|
|
public static TAttribute[] GetAttributes<TAttribute>(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<TAttribute>(inherit);
|
|
}
|
|
|
|
var propertyInfo = targetObjectType.GetProperty(pathSegment, AllBindingFlags);
|
|
if (propertyInfo != null)
|
|
{
|
|
return (TAttribute[])propertyInfo.GetCustomAttributes<TAttribute>(inherit);
|
|
}
|
|
}
|
|
|
|
return new TAttribute[0];
|
|
}
|
|
}
|
|
} |