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

@ -1,4 +1,5 @@
using Module.Inspector.Editor.Utilities;
using System;
using Module.Inspector.Editor.Utilities;
using UnityEditor;
using UnityEngine;
@ -8,79 +9,107 @@ namespace Module.Inspector.Editor
{
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
EditorPropertyUtility.Result result = EditorPropertyUtility.Query(fieldInfo);
EAccessType accessType = GetAccessType(property, result);
if (accessType == EAccessType.Hidden)
return;
bool prevEnabled = GUI.enabled;
GUI.enabled = prevEnabled && accessType == EAccessType.Enabled;
if (Event.current.type == EventType.Repaint)
EditorPropertyUtility.Result result = null;
int propertyHash = EditorPropertyUtility.CalculateHash(property);
bool isRootDrawer = false;
try
{
for (var i = 0; i < result.handleDrawers.Count; i++)
result = EditorPropertyUtility.Query(fieldInfo);
EAccessType accessType = GetAccessType(property, result);
if (accessType == EAccessType.Hidden)
return;
if (result.IsUsedBy(propertyHash))
{
EditorPropertyUtility.ResultValue<HandleDrawerPropertyAttribute, HandlePropertyDrawer> value = result.handleDrawers[i];
HandleDrawerEditor.Add(value.attribute, property, value.drawer);
EditorGUI.PropertyField(position, property, label);
return;
}
}
label.tooltip = result.tooltip;
for (var i = 0; i < result.predrawerModifiers.Count; i++)
{
EditorPropertyUtility.ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer> value = result.predrawerModifiers[i];
value.drawer.Modify(value.attribute, property, label);
}
isRootDrawer = true;
result.AddUsedBy(propertyHash);
bool prevEnabled = GUI.enabled;
GUI.enabled = prevEnabled && accessType == EAccessType.Enabled;
var isValid = true;
var validationError = string.Empty;
if (Event.current.type == EventType.Repaint)
{
for (var i = 0; i < result.handleDrawers.Count; i++)
{
EditorPropertyUtility.ResultValue<HandleDrawerPropertyAttribute, HandlePropertyDrawer> value =
result.handleDrawers[i];
HandleDrawerEditor.Add(value.attribute, property, value.drawer);
}
}
for (var i = 0; i < result.validators.Count; i++)
{
EditorPropertyUtility.ResultValue<ValidatePropertyAttribute, ValidatePropertyDrawer> value = result.validators[i];
label.tooltip = result.tooltip;
if (value.drawer.Validate(value.attribute, property))
continue;
for (var i = 0; i < result.predrawerModifiers.Count; i++)
{
EditorPropertyUtility.ResultValue<PredrawerModifierPropertyAttribute,
PredrawerModifierPropertyDrawer> value = result.predrawerModifiers[i];
value.drawer.Modify(value.attribute, property, label);
}
validationError += value.drawer.GetValidationError(value.attribute, property);
isValid = false;
}
Color prevColor = GUI.color;
var isValid = true;
var validationError = string.Empty;
if (!isValid)
{
if (string.IsNullOrEmpty(label.tooltip))
label.tooltip = validationError;
for (var i = 0; i < result.validators.Count; i++)
{
EditorPropertyUtility.ResultValue<ValidatePropertyAttribute, ValidatePropertyDrawer> value =
result.validators[i];
if (value.drawer.Validate(value.attribute, property))
continue;
validationError += value.drawer.GetValidationError(value.attribute, property);
isValid = false;
}
Color prevColor = GUI.color;
if (!isValid)
{
if (string.IsNullOrEmpty(label.tooltip))
label.tooltip = validationError;
else
label.tooltip += "\n" + validationError;
EditorIcons.SetErrorIcon(label);
}
if (result.draw != null)
{
if (!result.draw.drawer.Draw(position, result.draw.attribute, property, label, result))
{
GUI.color = Color.red;
var errorContent = new GUIContent(result.draw.drawer.GetErrorMessage(property));
EditorGUI.LabelField(position, label, errorContent);
}
}
else
label.tooltip += "\n" + validationError;
EditorIcons.SetErrorIcon(label);
}
if (result.draw != null)
{
if (!result.draw.drawer.Draw(position, result.draw.attribute, property, label, result))
{
GUI.color = Color.red;
var errorContent = new GUIContent(result.draw.drawer.GetErrorMessage(property));
EditorGUI.LabelField(position, label, errorContent);
EditorGUI.PropertyField(position, property, label, true);
}
GUI.color = prevColor;
GUI.enabled = prevEnabled;
for (var i = 0; i < result.valueModifiers.Count; i++)
{
EditorPropertyUtility.ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>
value = result.valueModifiers[i];
value.drawer.Modify(value.attribute, property);
}
}
else
catch (Exception)
{
EditorGUI.PropertyField(position, property, label, true);
// Ignore
}
GUI.color = prevColor;
GUI.enabled = prevEnabled;
for (var i = 0; i < result.valueModifiers.Count; i++)
finally
{
EditorPropertyUtility.ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer> value = result.valueModifiers[i];
value.drawer.Modify(value.attribute, property);
if (isRootDrawer && result != null)
result.RemoveUsedBy(propertyHash);
}
}
@ -97,10 +126,11 @@ namespace Module.Inspector.Editor
return EAccessType.Enabled;
EAccessType accessType = 0;
for (var i = 0; i < result.accessModifiers.Count; i++)
{
EditorPropertyUtility.ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer> value = result.accessModifiers[i];
EditorPropertyUtility.ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer> value =
result.accessModifiers[i];
accessType = value.drawer.GetAccessType(value.attribute, property, accessType);
}