Skip to content

Commit 9bafe3b

Browse files
committed
Updated Toggle Drawer, Attribtue Drawer, and Slide Drawer.
1 parent dd7ac88 commit 9bafe3b

File tree

6 files changed

+73
-37
lines changed

6 files changed

+73
-37
lines changed

Editor/Attributes/AutoAttributeDrawer.cs

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,74 @@ public class AutoAttributeDrawer : PropertyDrawer
1313

1414
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
1515
{
16-
var attr = attribute as AutoAttribute;
17-
18-
// Null, so lets assign it
19-
if (AutoAttributeEditor.AutoRefreshInspector)
20-
AutoAttributeEditor.ApplyAttributeToSerializedProperty(property, attr);
16+
try
17+
{
2118

22-
var optionalError = AutoAttributeEditor.Validate(property);
19+
var attr = attribute as AutoAttribute;
20+
// Null, so lets assign it
21+
if (AutoAttributeEditor.AutoRefreshInspector)
22+
AutoAttributeEditor.ApplyAttributeToSerializedProperty(property, attr);
2323

24+
var optionalError = AutoAttributeEditor.Validate(property);
2425

26+
if (!attr.Hidden || optionalError.HasValue)
27+
PropertyGUI(position, property, label);
2528

26-
if (!attr.Hidden || optionalError.HasValue)
27-
PropertyGUI(position, property, label);
29+
// If we have an error, render the error stuff
30+
if (optionalError is AutoAttributeEditor.Error error)
31+
{
32+
var baseHeight = base.GetPropertyHeight(property, label);
2833

29-
// If we have an error, render the error stuff
30-
if (optionalError is AutoAttributeEditor.Error error)
34+
Rect errorBox = new Rect(position.x + EditorGUIUtility.labelWidth + 1f, position.y + baseHeight + 1, position.width - EditorGUIUtility.labelWidth, position.height - baseHeight - 1);
35+
EditorGUI.HelpBox(errorBox, error.message, error.blocks ? MessageType.Error : MessageType.Warning);
36+
}
37+
}
38+
catch(System.Exception e)
3139
{
32-
var baseHeight = base.GetPropertyHeight(property, label);
33-
34-
Rect errorBox = new Rect(position.x + EditorGUIUtility.labelWidth + 1f, position.y + baseHeight + 1, position.width - EditorGUIUtility.labelWidth, position.height - baseHeight - 1);
35-
EditorGUI.HelpBox(errorBox, error.message, error.blocks ? MessageType.Error : MessageType.Warning);
40+
EditorGUI.HelpBox(position, e.Message, MessageType.Error);
41+
Debug.Log(e);
3642
}
37-
3843
}
3944

4045
private void PropertyGUI(Rect position, SerializedProperty property, GUIContent label)
4146
{
42-
// Ensure regardless of height we still have a regular sized box
43-
position.height = base.GetPropertyHeight(property, label);
47+
try
48+
{
49+
// Ensure regardless of height we still have a regular sized box
50+
position.height = base.GetPropertyHeight(property, label);
51+
52+
// Not hidden, lets just display it
53+
var iconLabel = new GUIContent(AutoAttributeEditor.IconAuto_16);
54+
iconLabel.tooltip = "Automatically Linked Component";
55+
Rect iconBox = new Rect(position.x + EditorGUIUtility.labelWidth - position.height - 1.5f, position.y + 1.5f, position.height - 3, position.height - 3);
56+
EditorGUI.LabelField(iconBox, iconLabel);
4457

45-
// Not hidden, lets just display it
46-
var iconLabel = new GUIContent(AutoAttributeEditor.IconAuto_16);
47-
iconLabel.tooltip = "Automatically Linked Component";
48-
Rect iconBox = new Rect(position.x + EditorGUIUtility.labelWidth - position.height - 1.5f, position.y + 1.5f, position.height - 3, position.height - 3);
49-
EditorGUI.LabelField(iconBox, iconLabel);
58+
EditorGUI.PropertyField(position, property, label);
59+
GUI.color = Color.white;
5060

51-
EditorGUI.PropertyField(position, property, label);
52-
GUI.color = Color.white;
61+
}
62+
catch (System.Exception e)
63+
{
64+
EditorGUI.HelpBox(position, e.Message, MessageType.Error);
65+
Debug.Log(e);
66+
}
5367
}
5468

5569
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
5670
{
57-
if (AutoAttributeEditor.Validate(property).HasValue)
58-
return base.GetPropertyHeight(property, label) + 20;
71+
try
72+
{
73+
if (AutoAttributeEditor.Validate(property).HasValue)
74+
return base.GetPropertyHeight(property, label) + 20;
5975

60-
var attr = attribute as AutoAttribute;
61-
return attr.Hidden ? 0 : base.GetPropertyHeight(property, label);
76+
var attr = attribute as AutoAttribute;
77+
return attr.Hidden ? 0 : base.GetPropertyHeight(property, label);
78+
}
79+
catch(System.Exception e)
80+
{
81+
Debug.LogError("Failed to determine the best high for an auto attribute\n" + e.Message);
82+
return EditorGUIUtility.singleLineHeight;
83+
}
6284
}
6385
}
6486
}

Editor/Attributes/AutoAttributeEditor.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
namespace Lachee.Attributes.Editor
1111
{
12+
//TODO: Implement this for RequireComponentOfType
13+
// https://github.com/NdubuisiJr/TypeExtender/blob/main/TypeExtender/TypeExtender/TypeExtender.cs#L360-L379
14+
// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.customattributebuilder?view=net-6.0
1215

1316
/// <summary>Observes the Auto-Attribute to rebuild lists.</summary>
1417
[InitializeOnLoad]

Editor/Attributes/SlideDrawer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
5959

6060
private static void MinMaxSlider(Rect position, SerializedProperty property, float min, float max, GUIContent label)
6161
{
62+
//TODO: Look at how unity draws its MinMaxSlider attribute
63+
6264
float minValue = 0;
6365
float maxValue = 0;
6466

Editor/Attributes/ToggleDrawer.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
using Lachee.Utilities.Editor;
2+
using System.Configuration;
23
using System.Reflection;
34
using UnityEditor;
45
using UnityEngine;
56

67
namespace Lachee.Attributes.Editor
78
{
8-
//TODO: Implement this for HideInInspector
9-
// https://github.com/NdubuisiJr/TypeExtender/blob/main/TypeExtender/TypeExtender/TypeExtender.cs#L360-L379
10-
// https://docs.microsoft.com/en-us/dotnet/api/system.reflection.emit.customattributebuilder?view=net-6.0
9+
//TODO: Create a Optional<Component> drawer
1110

1211
[CustomPropertyDrawer(typeof(ToggleAttribute))]
1312
public class ToggleDrawer : PropertyDrawer
@@ -29,19 +28,24 @@ public override void OnGUI(Rect position, SerializedProperty property, GUIConten
2928
else
3029
{
3130
bool wasEnabled = GUI.enabled;
31+
int indentation = 0;
3232

3333
//Check if we should draw the property
34-
Rect iconBox = new Rect(position.x, position.y, 25, position.height);
35-
EditorGUI.PropertyField(iconBox, toggleProperty, new GUIContent("", attr.Tooltip));
34+
if (attr.ShowCheckbox)
35+
{
36+
Rect iconBox = new Rect(position.x, position.y, 25, position.height);
37+
EditorGUI.PropertyField(iconBox, toggleProperty, new GUIContent("", attr.Tooltip));
38+
indentation += 1;
39+
}
3640

3741
Rect propBox = new Rect(position.x, position.y, position.width, position.height);
3842
GUI.enabled = attr.Invert ? !toggleProperty.boolValue : toggleProperty.boolValue;
3943
{
40-
EditorGUI.indentLevel += 1;
44+
EditorGUI.indentLevel += indentation;
4145
{
42-
EditorGUI.PropertyField(propBox, property, new GUIContent($" {label.text}", label.tooltip), true);
46+
EditorGUI.PropertyField(propBox, property, new GUIContent(label.text, label.tooltip), true);
4347
}
44-
EditorGUI.indentLevel -= 1;
48+
EditorGUI.indentLevel -= indentation;
4549
}
4650
GUI.enabled = wasEnabled;
4751

Runtime/Attributes/ToggleAttribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ public sealed class ToggleAttribute : PropertyAttribute
2727
/// </summary>
2828
public bool Invert { get; private set; }
2929

30+
/// <summary>
31+
/// Shows the checkbox to enable/disable he field
32+
/// </summary>
33+
public bool ShowCheckbox { get; set; } = true;
34+
3035
/// <summary>Creates a toggle attribute with the default prefix of @Enabled</summary>
3136
public ToggleAttribute()
3237
{

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.lachee.utilities",
3-
"version": "1.5.2",
3+
"version": "1.5.3",
44
"displayName": "Lachee's Utilities",
55
"description": "Bunch of utility functionality",
66
"unity": "2019.1",

0 commit comments

Comments
 (0)