using UnityEditor; using UnityEngine; using UnityForge.PropertyDrawers.Editor; namespace KitsuneCafe.Editor.PropertyDrawers { public abstract class ComponentObjectFieldPropertyDrawer : 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( 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); } }