canto/Assets/Scripts/Editor/Drawers/ComponentObjectFieldPropertyDrawer.cs
2025-10-02 15:28:03 -04:00

50 lines
1.6 KiB
C#

using UnityEditor;
using UnityEngine;
using UnityForge.PropertyDrawers.Editor;
namespace KitsuneCafe.Editor.PropertyDrawers
{
public abstract class ComponentObjectFieldPropertyDrawer<TAttribute, TObject> : PropertyDrawer
where TAttribute : PropertyAttribute
where TObject : Object
{
private SerializedPropertyType propertyType_;
protected ComponentObjectFieldPropertyDrawer(SerializedPropertyType propertyType)
{
propertyType_ = propertyType;
}
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
position = EditorGUI.PrefixLabel(position, label);
if (property.propertyType != propertyType_)
{
EditorGUI.LabelField(position, string.Format("Error: {0} attribute can be applied only to {1} type", typeof(TAttribute), propertyType_));
return;
}
var field = GetPropertyPath((TAttribute)attribute);
if (!string.IsNullOrEmpty(field))
{
ObjectFieldPropertyDrawerUtils.DrawObjectFieldPoperty<TObject>(
position, property, field, DrawComponentProperty);
return;
}
var targetObject = property.serializedObject.targetObject as TObject;
if (targetObject == null)
{
EditorGUI.LabelField(position, "Error: inspected object type is not an Object");
return;
}
DrawComponentProperty(position, property, targetObject);
}
protected abstract string GetPropertyPath(TAttribute attribute);
protected abstract void DrawComponentProperty(Rect position, SerializedProperty property, TObject target);
}
}