Added PredrawerModifier to help modify either SerializedProperty or GUIContent before drawing
This commit is contained in:
parent
6321fcc107
commit
e0fc2c1e05
|
|
@ -15,8 +15,16 @@ namespace Module.Inspector.Editor
|
|||
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);
|
||||
}
|
||||
|
||||
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;
|
||||
|
|
|
|||
10
Editor/PredrawerModifierPropertyDrawer.cs
Normal file
10
Editor/PredrawerModifierPropertyDrawer.cs
Normal file
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
3
Editor/PredrawerModifierPropertyDrawer.cs.meta
Normal file
3
Editor/PredrawerModifierPropertyDrawer.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 5a747366f0b24cf89b7ebfd2108468f7
|
||||
timeCreated: 1638612780
|
||||
|
|
@ -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<DrawerPropertyAttribute, DrawerPropertyDrawer>(null, typeDrawer);
|
||||
|
||||
var predrawerModifiers = new List<ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer>>(2);
|
||||
var valueModifiers = new List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>>(2);
|
||||
var accessModifiers = new List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>>(2);
|
||||
object[] attributes = fieldInfo.GetCustomAttributes(false);
|
||||
|
|
@ -88,6 +94,13 @@ namespace Module.Inspector.Editor.Utilities
|
|||
if (prop != null)
|
||||
drawer = new ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer>(attDrawer, prop);
|
||||
}
|
||||
else if (att is PredrawerModifierPropertyAttribute predawerModifier)
|
||||
{
|
||||
var prop = InternalCreateInstanceOf<PredrawerModifierPropertyDrawer>(predawerModifier);
|
||||
|
||||
if (prop != null)
|
||||
predrawerModifiers.Add(new ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer>(predawerModifier, prop));
|
||||
}
|
||||
else if (att is ValueModifierPropertyAttribute valueModifier)
|
||||
{
|
||||
var prop = InternalCreateInstanceOf<ValueModifierPropertyDrawer>(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<T>(AbstractPropertyAttribute att) where T : AbstractPropertyDrawer
|
||||
|
|
@ -146,23 +159,37 @@ namespace Module.Inspector.Editor.Utilities
|
|||
public sealed class Result
|
||||
{
|
||||
public readonly ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer> draw;
|
||||
public readonly List<ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer>> predrawerModifiers;
|
||||
public readonly List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>> valueModifiers;
|
||||
public readonly List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>> accessModifiers;
|
||||
public readonly string tooltip;
|
||||
public readonly bool isObsolete;
|
||||
|
||||
public Result(ResultValue<DrawerPropertyAttribute, DrawerPropertyDrawer> draw,
|
||||
List<ResultValue<PredrawerModifierPropertyAttribute, PredrawerModifierPropertyDrawer>> predrawerModifiers,
|
||||
List<ResultValue<ValueModifierPropertyAttribute, ValueModifierPropertyDrawer>> valueModifiers,
|
||||
List<ResultValue<AccessModifierPropertyAttribute, AccessModifierPropertyDrawer>> 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<T>() 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<T>() where T : ValueModifierPropertyAttribute
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
{
|
||||
public enum EPropertyType : byte
|
||||
{
|
||||
PredrawerModifier,
|
||||
Drawer,
|
||||
ValueModifier,
|
||||
AccessModifier
|
||||
|
|
|
|||
7
Runtime/PredrawerModifierPropertyAttribute.cs
Normal file
7
Runtime/PredrawerModifierPropertyAttribute.cs
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
namespace Module.Inspector
|
||||
{
|
||||
public abstract class PredrawerModifierPropertyAttribute : AbstractPropertyAttribute
|
||||
{
|
||||
public override EPropertyType Type => EPropertyType.PredrawerModifier;
|
||||
}
|
||||
}
|
||||
3
Runtime/PredrawerModifierPropertyAttribute.cs.meta
Normal file
3
Runtime/PredrawerModifierPropertyAttribute.cs.meta
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 54936da91a3241debe62f6fe4269f129
|
||||
timeCreated: 1638612653
|
||||
Loading…
Reference in a new issue