1.9.2: Added OpenPropertyEditor and fixed some issues regarding using multiple drawers with EditorGUI.PropertyField

- Property: `OpenPropertyEditor` adds a "show"-button next to a `ScriptableObject` or `Component` to open a property window
- Property: Fixed issue, where x PropertyDrawers would invoke each other x times
This commit is contained in:
Anders Ejlersen 2024-03-10 14:11:49 +01:00
parent f609ba6f51
commit c87dd743f6
8 changed files with 189 additions and 63 deletions

View file

@ -0,0 +1,49 @@
using Module.Inspector.Editor.Utilities;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
[CustomPropertyDrawer(typeof(OpenPropertyEditorAttribute))]
internal sealed class OpenPropertyEditorAttributeDrawer : DrawerPropertyDrawer
{
public override bool Draw(Rect position, DrawerPropertyAttribute attribute, SerializedProperty property, GUIContent label, EditorPropertyUtility.Result result)
{
if (!IsSupported(property))
return false;
EditorGUI.BeginProperty(position, label, property);
{
const float WIDTH = 50;
var rect0 = new Rect(position.x, position.y, position.width - WIDTH, position.height);
var rect1 = new Rect(rect0.xMax, position.y, WIDTH, position.height);
EditorGUI.PropertyField(rect0, property, label);
var prevValue = GUI.enabled;
GUI.enabled = prevValue && property.objectReferenceValue != null;
if (GUI.Button(rect1, "Show"))
EditorUtility.OpenPropertyEditor(property.objectReferenceValue);
GUI.enabled = prevValue;
}
EditorGUI.EndProperty();
return true;
}
public override string GetErrorMessage(SerializedProperty property)
{
return IsSupported(property)
? "Unknown error"
: "Only supports types which inherits from ScriptableObject/Component type";
}
private static bool IsSupported(SerializedProperty property)
{
return property.propertyType == SerializedPropertyType.ObjectReference &&
(typeof(ScriptableObject).IsAssignableFrom(property.GetValueType()) ||
typeof(Component).IsAssignableFrom(property.GetValueType()));
}
}
}

View file

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 5fbf47456b2f487aa93a36acf40ad59b
timeCreated: 1710067726