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 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 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 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 value = result.accessModifiers[i]; accessType = value.drawer.GetAccessType(value.attribute, property, accessType); } return accessType; } } }