Skip to content

Commit 27d2a2f

Browse files
committed
Better support for the LabelByChildAttribute across Toolbox Editors
1 parent afe92ad commit 27d2a2f

9 files changed

Lines changed: 97 additions & 180 deletions

File tree

Assets/Editor Toolbox/Editor/Drawers/Regular/LabelByChildAttributeDrawer.cs

Lines changed: 0 additions & 113 deletions
This file was deleted.

Assets/Editor Toolbox/Editor/ToolboxPropertyHandler.cs

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Toolbox.Editor
99
{
10+
using Toolbox.Attributes;
1011
using Toolbox.Editor.Drawers;
1112

1213
/// <summary>
@@ -83,6 +84,12 @@ internal class ToolboxPropertyHandler
8384
/// </summary>
8485
private bool hasToolboxConditionDrawer;
8586

87+
/// <summary>
88+
/// Temporary solution to cache all attributes responsible for overriding label.
89+
/// This will be replaced with validation attributes.
90+
/// </summary>
91+
private ILabelProcessorAttribute labelProcessorAttribute;
92+
8693

8794
/// <summary>
8895
/// Constructor prepares all property-related data for custom drawing.
@@ -166,26 +173,27 @@ private void CheckIfPropertyHasPropertyDrawer(Type type)
166173

167174
private void HandleNewAttribute(PropertyAttribute attribute)
168175
{
169-
if (!isChild)
176+
//NOTE: setting tooltip and labels is valid only for parent or single properties
177+
//it's a bit ugly but, it's the only semi-acceptable way to support built-in TooltipAttribute
178+
//TODO: move these attributes to validation drawers
179+
switch (attribute)
170180
{
171-
//NOTE: setting tooltip and labels is valid only for parent or single properties
172-
//it's a bit ugly but, it's the only semi-acceptable way to support built-in TooltipAttribute
173-
switch (attribute)
174-
{
175-
case TooltipAttribute a:
176-
label.tooltip = a.tooltip;
177-
return;
178-
case NewLabelAttribute a:
179-
label.text = a.NewLabel;
180-
return;
181-
case LabelByChildAttribute a:
182-
if (!isArray)
183-
{
184-
PropertyUtility.OverrideLabelByChild(label, property, a.ChildName);
185-
}
186-
187-
return;
188-
}
181+
case TooltipAttribute a:
182+
label.tooltip = a.tooltip;
183+
return;
184+
case NewLabelAttribute a:
185+
if (!isChild)
186+
{
187+
labelProcessorAttribute = a;
188+
}
189+
return;
190+
case LabelByChildAttribute a:
191+
if (!isArray)
192+
{
193+
labelProcessorAttribute = a;
194+
}
195+
196+
return;
189197
}
190198

191199
var attributeType = attribute.GetType();
@@ -276,6 +284,8 @@ private bool TryAssignConditionAttribute(ToolboxConditionAttribute attribute)
276284

277285
private void DrawProperty(SerializedProperty property, GUIContent label)
278286
{
287+
ProcessLabel(property, label);
288+
279289
//get toolbox drawer for the property or draw it in the default way
280290
if (hasToolboxPropertyDrawer && (!hasBuiltInPropertyDrawer || isArray))
281291
{
@@ -326,6 +336,25 @@ private void CloseDecoratorDrawers(PropertyCondition conditionState = PropertyCo
326336
}
327337
}
328338

339+
//TODO: replace this method with validation attributes
340+
private void ProcessLabel(SerializedProperty property, GUIContent label)
341+
{
342+
if (labelProcessorAttribute == null)
343+
{
344+
return;
345+
}
346+
347+
switch (labelProcessorAttribute)
348+
{
349+
case NewLabelAttribute a:
350+
label.text = a.NewLabel;
351+
return;
352+
case LabelByChildAttribute a:
353+
PropertyUtility.OverrideLabelByChild(label, property, a.ChildName);
354+
return;
355+
}
356+
}
357+
329358
private void HandleDecorator(ToolboxDecoratorAttribute attribute, bool onBegin, PropertyCondition conditionState = PropertyCondition.Valid)
330359
{
331360
var drawer = ToolboxDrawerModule.GetDecoratorDrawer(attribute);
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Toolbox.Attributes
2+
{
3+
/// <summary>
4+
/// Temporary interface used to wrap all attributes used to override property labels.
5+
/// </summary>
6+
public interface ILabelProcessorAttribute
7+
{ }
8+
}

Assets/Editor Toolbox/Editor/Drawers/Regular/LabelByChildAttributeDrawer.cs.meta renamed to Assets/Editor Toolbox/Runtime/Attributes/ILabelProcessorAttribute.cs.meta

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Editor Toolbox/Runtime/Attributes/Special/LabelByChildAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Diagnostics;
33

4+
using Toolbox.Attributes;
5+
46
namespace UnityEngine
57
{
68
/// <summary>
@@ -22,7 +24,7 @@ namespace UnityEngine
2224
/// </summary>
2325
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
2426
[Conditional("UNITY_EDITOR")]
25-
public class LabelByChildAttribute : PropertyAttribute
27+
public class LabelByChildAttribute : PropertyAttribute, ILabelProcessorAttribute
2628
{
2729
public LabelByChildAttribute(string childName)
2830
{

Assets/Editor Toolbox/Runtime/Attributes/Special/NewLabelAttribute.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Diagnostics;
33

4+
using Toolbox.Attributes;
5+
46
namespace UnityEngine
57
{
68
/// <summary>
@@ -10,7 +12,7 @@ namespace UnityEngine
1012
/// </summary>
1113
[AttributeUsage(AttributeTargets.Field, AllowMultiple = false)]
1214
[Conditional("UNITY_EDITOR")]
13-
public class NewLabelAttribute : PropertyAttribute
15+
public class NewLabelAttribute : PropertyAttribute, ILabelProcessorAttribute
1416
{
1517
public NewLabelAttribute(string newLabel)
1618
{

Assets/Examples/Scenes/SampleScene.unity

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -703,28 +703,6 @@ MonoBehaviour:
703703
sceneReference: {fileID: 0}
704704
childReference: {fileID: 0}
705705
prefabReference: {fileID: 0}
706-
sampleField:
707-
var1: {fileID: 2100000, guid: 7404c70251f9d0045a4aabaa49d83963, type: 2}
708-
var2: 0
709-
var3:
710-
var1: 0
711-
var2: Label By Value
712-
sampleFields:
713-
- var1: {fileID: 0}
714-
var2: 64
715-
var3:
716-
var1: 0
717-
var2:
718-
- var1: {fileID: 0}
719-
var2: 53
720-
var3:
721-
var1: 0
722-
var2:
723-
- var1: {fileID: 0}
724-
var2: 38
725-
var3:
726-
var1: 0
727-
var2:
728706
bigNumber: 12345678
729707
currency: 20.41
730708
--- !u!4 &959025299
@@ -1171,6 +1149,16 @@ MonoBehaviour:
11711149
var1: 25.4
11721150
var2: {fileID: 977748988}
11731151
var3: 0
1152+
var4:
1153+
- var1: 36
1154+
- var1: 52
1155+
sampleField:
1156+
var1: {fileID: 0}
1157+
var2: 266
1158+
var3:
1159+
var1: 0
1160+
var2: New Label from var2.var3
1161+
sampleFields: []
11741162
--- !u!4 &1583836799
11751163
Transform:
11761164
m_ObjectHideFlags: 2

Assets/Examples/Scripts/SampleBehaviour1.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -103,28 +103,6 @@ public enum FlagExample
103103
[PrefabObjectOnly]
104104
public GameObject prefabReference;
105105

106-
[Label("Label By Child", skinStyle: SkinStyle.Box)]
107-
108-
[LabelByChild("var3.var2")]
109-
public SampleClass1 sampleField;
110-
[LabelByChild("var2")]
111-
public SampleClass1[] sampleFields;
112-
113-
[System.Serializable]
114-
public class SampleClass1
115-
{
116-
public Material var1;
117-
public KeyCode var2;
118-
public SampleClass2 var3;
119-
}
120-
121-
[System.Serializable]
122-
public class SampleClass2
123-
{
124-
public int var1;
125-
public string var2;
126-
}
127-
128106
[Label("Formatted Number", skinStyle: SkinStyle.Box)]
129107

130108
[FormattedNumber]

Assets/Examples/Scripts/SampleBehaviour7.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using UnityEngine;
1+
using System;
2+
using UnityEngine;
23

34
[ExecuteAlways]
45
[AddComponentMenu("Editor Toolbox/Cheat Sheet 7 (Special & Others)")]
@@ -16,4 +17,26 @@ public class SampleBehaviour7 : MonoBehaviour
1617

1718
[HideLabel]
1819
public int var3;
20+
21+
[Label("LabelByChild", skinStyle: SkinStyle.Box)]
22+
23+
[LabelByChild("var3.var2")]
24+
public SampleClass1 sampleField;
25+
[LabelByChild("var2"), ReorderableList]
26+
public SampleClass1[] sampleFields;
27+
28+
[Serializable]
29+
public class SampleClass1
30+
{
31+
public Material var1;
32+
public KeyCode var2;
33+
public SampleClass2 var3;
34+
}
35+
36+
[Serializable]
37+
public class SampleClass2
38+
{
39+
public int var1;
40+
public string var2;
41+
}
1942
}

0 commit comments

Comments
 (0)