module-inspector/Editor/AbstractPropertyDrawer.cs
Anders Ejlersen ee7326c33a 1.4.0: Added ReadableScriptableObjectAttribute and fixed an GUI.enable isssue
* Attribute: Added ReadableScriptableObjectAttribute to allow for inline editing of ScriptableObjects in the inspector
* Attribute: Fixed issue, where root drawer wouldn't use existing value of GUI.enabled, when setting GUI.enabled state with new access value
2021-12-19 13:45:23 +01:00

101 lines
3.8 KiB
C#

using Module.Inspector.Editor.Utilities;
using UnityEditor;
using UnityEngine;
namespace Module.Inspector.Editor
{
public abstract class AbstractPropertyDrawer : PropertyDrawer
{
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;
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);
}
var isValid = true;
var validationError = string.Empty;
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(property);
isValid = false;
}
Color prevColor = GUI.color;
if (!isValid)
{
if (string.IsNullOrEmpty(label.tooltip))
label.tooltip = validationError;
else
label.tooltip += "\n" + validationError;
GUI.color = Color.red;
}
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
{
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);
}
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
EditorPropertyUtility.Result result = EditorPropertyUtility.Query(fieldInfo);
EAccessType accessType = GetAccessType(property, result);
return accessType != EAccessType.Hidden ? EditorGUI.GetPropertyHeight(property, label) : 0.0f;
}
private static EAccessType GetAccessType(SerializedProperty property, EditorPropertyUtility.Result result)
{
if (result.accessModifiers.Count == 0)
return EAccessType.Enabled;
EAccessType accessType = 0;
for (var i = 0; i < result.accessModifiers.Count; i++)
{
EditorPropertyUtility.ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer> value = result.accessModifiers[i];
accessType = value.drawer.GetAccessType(value.attribute, property, accessType);
}
return accessType;
}
}
}