diff --git a/Editor/AbstractPropertyDrawer.cs b/Editor/AbstractPropertyDrawer.cs index 7e3416b..37f0a48 100644 --- a/Editor/AbstractPropertyDrawer.cs +++ b/Editor/AbstractPropertyDrawer.cs @@ -15,8 +15,16 @@ namespace Module.Inspector.Editor return; 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); + } + bool prevEnabled = GUI.enabled; GUI.enabled = accessType == EAccessType.Enabled; + // TODO: What to do with [Obsolete]? if (result.draw != null) { @@ -50,7 +58,7 @@ namespace Module.Inspector.Editor return accessType != EAccessType.Hidden ? EditorGUI.GetPropertyHeight(property, label) : 0.0f; } - private EAccessType GetAccessType(SerializedProperty property, EditorPropertyUtility.Result result) + private static EAccessType GetAccessType(SerializedProperty property, EditorPropertyUtility.Result result) { if (result.accessModifiers.Count == 0) return EAccessType.Enabled; diff --git a/Editor/PredrawerModifierPropertyDrawer.cs b/Editor/PredrawerModifierPropertyDrawer.cs new file mode 100644 index 0000000..726b480 --- /dev/null +++ b/Editor/PredrawerModifierPropertyDrawer.cs @@ -0,0 +1,10 @@ +using UnityEditor; +using UnityEngine; + +namespace Module.Inspector.Editor +{ + public abstract class PredrawerModifierPropertyDrawer : AbstractPropertyDrawer + { + public abstract void Modify(PredrawerModifierPropertyAttribute attribute, SerializedProperty property, GUIContent label); + } +} \ No newline at end of file diff --git a/Editor/PredrawerModifierPropertyDrawer.cs.meta b/Editor/PredrawerModifierPropertyDrawer.cs.meta new file mode 100644 index 0000000..2a232cd --- /dev/null +++ b/Editor/PredrawerModifierPropertyDrawer.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5a747366f0b24cf89b7ebfd2108468f7 +timeCreated: 1638612780 \ No newline at end of file diff --git a/Editor/Utilities/EditorPropertyUtility.cs b/Editor/Utilities/EditorPropertyUtility.cs index 5399aad..38c0cef 100644 --- a/Editor/Utilities/EditorPropertyUtility.cs +++ b/Editor/Utilities/EditorPropertyUtility.cs @@ -37,7 +37,12 @@ namespace Module.Inspector.Editor.Utilities for (var k = 0; k < arguments.Count; k++) { - if (arguments[k].Value is Type argType) + if (!(arguments[k].Value is Type argType)) + continue; + + if (CACHED_ATT_TO_DRAWER.ContainsKey(argType)) + Debug.LogWarningFormat("Already contained {0}, but tried to add with {1} and already had {2}.", argType, type, CACHED_ATT_TO_DRAWER[argType]); + else CACHED_ATT_TO_DRAWER.Add(argType, type); } } @@ -67,6 +72,7 @@ namespace Module.Inspector.Editor.Utilities if (typeDrawer != null) drawer = new ResultValue(null, typeDrawer); + var predrawerModifiers = new List>(2); var valueModifiers = new List>(2); var accessModifiers = new List>(2); object[] attributes = fieldInfo.GetCustomAttributes(false); @@ -88,6 +94,13 @@ namespace Module.Inspector.Editor.Utilities if (prop != null) drawer = new ResultValue(attDrawer, prop); } + else if (att is PredrawerModifierPropertyAttribute predawerModifier) + { + var prop = InternalCreateInstanceOf(predawerModifier); + + if (prop != null) + predrawerModifiers.Add(new ResultValue(predawerModifier, prop)); + } else if (att is ValueModifierPropertyAttribute valueModifier) { var prop = InternalCreateInstanceOf(valueModifier); @@ -121,7 +134,7 @@ namespace Module.Inspector.Editor.Utilities tooltip = $"Obsolete: {obsoleteText}"; } - return new Result(drawer, valueModifiers, accessModifiers, tooltip, isObsolete); + return new Result(drawer, predrawerModifiers, valueModifiers, accessModifiers, tooltip, isObsolete); } private static T InternalCreateInstanceOf(AbstractPropertyAttribute att) where T : AbstractPropertyDrawer @@ -146,23 +159,37 @@ namespace Module.Inspector.Editor.Utilities public sealed class Result { public readonly ResultValue draw; + public readonly List> predrawerModifiers; public readonly List> valueModifiers; public readonly List> accessModifiers; public readonly string tooltip; public readonly bool isObsolete; public Result(ResultValue draw, + List> predrawerModifiers, List> valueModifiers, List> accessModifiers, string tooltip, bool isObsolete) { this.draw = draw; + this.predrawerModifiers = predrawerModifiers; this.valueModifiers = valueModifiers; this.accessModifiers = accessModifiers; this.tooltip = tooltip; this.isObsolete = isObsolete; } + + public T GetPredrawerModifier() where T : PredrawerModifierPropertyAttribute + { + for (var i = 0; i < predrawerModifiers.Count; i++) + { + if (predrawerModifiers[i].attribute is T att) + return att; + } + + return null; + } public T GetValueModifier() where T : ValueModifierPropertyAttribute { diff --git a/Runtime/Enums/EPropertyType.cs b/Runtime/Enums/EPropertyType.cs index 6c88df4..bc56450 100644 --- a/Runtime/Enums/EPropertyType.cs +++ b/Runtime/Enums/EPropertyType.cs @@ -2,6 +2,7 @@ { public enum EPropertyType : byte { + PredrawerModifier, Drawer, ValueModifier, AccessModifier diff --git a/Runtime/PredrawerModifierPropertyAttribute.cs b/Runtime/PredrawerModifierPropertyAttribute.cs new file mode 100644 index 0000000..bf6479e --- /dev/null +++ b/Runtime/PredrawerModifierPropertyAttribute.cs @@ -0,0 +1,7 @@ +namespace Module.Inspector +{ + public abstract class PredrawerModifierPropertyAttribute : AbstractPropertyAttribute + { + public override EPropertyType Type => EPropertyType.PredrawerModifier; + } +} \ No newline at end of file diff --git a/Runtime/PredrawerModifierPropertyAttribute.cs.meta b/Runtime/PredrawerModifierPropertyAttribute.cs.meta new file mode 100644 index 0000000..efac107 --- /dev/null +++ b/Runtime/PredrawerModifierPropertyAttribute.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 54936da91a3241debe62f6fe4269f129 +timeCreated: 1638612653 \ No newline at end of file