This repository has been archived on 2024-11-03. You can view files and clone it, but cannot push or open issues or pull requests.
snakeinabox/Assets/NaughtyAttributes/Scripts/Editor/Utility/PropertyUtility.cs
2020-07-09 11:41:01 -07:00

27 lines
837 B
C#

using UnityEditor;
using System;
using System.Reflection;
namespace NaughtyAttributes.Editor
{
public static class PropertyUtility
{
public static T GetAttribute<T>(SerializedProperty property) where T : Attribute
{
T[] attributes = GetAttributes<T>(property);
return attributes.Length > 0 ? attributes[0] : null;
}
public static T[] GetAttributes<T>(SerializedProperty property) where T : Attribute
{
FieldInfo fieldInfo = ReflectionUtility.GetField(GetTargetObject(property), property.name);
return (T[])fieldInfo.GetCustomAttributes(typeof(T), true);
}
public static UnityEngine.Object GetTargetObject(SerializedProperty property)
{
return property.serializedObject.targetObject;
}
}
}