69 lines
2.7 KiB
C#
69 lines
2.7 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;
|
|
|
|
label.tooltip = result.tooltip;
|
|
bool prevEnabled = GUI.enabled;
|
|
GUI.enabled = accessType == EAccessType.Enabled;
|
|
|
|
if (result.draw != null)
|
|
{
|
|
if (!result.draw.drawer.Draw(position, result.draw.attribute, property, label, result))
|
|
{
|
|
Color prevColor = GUI.color;
|
|
GUI.color = Color.red;
|
|
var errorContent = new GUIContent(result.draw.drawer.GetErrorMessage(property));
|
|
EditorGUI.LabelField(position, label, errorContent);
|
|
GUI.color = prevColor;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
EditorGUI.PropertyField(position, property, label, true);
|
|
}
|
|
|
|
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 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;
|
|
}
|
|
}
|
|
} |