Skip to content

Commit 4392c10

Browse files
committed
Ability to decide if EditorButton should be rendered below or above the target property; minor SerializedScene API improvements; minor refactor changes
1 parent 7e850c5 commit 4392c10

File tree

5 files changed

+75
-27
lines changed

5 files changed

+75
-27
lines changed

Assets/Editor Toolbox/Editor/Drawers/Toolbox/Decorator/EditorButtonAttributeDrawer.cs

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,31 @@ namespace Toolbox.Editor.Drawers
88
{
99
public class EditorButtonAttributeDrawer : ToolboxDecoratorDrawer<EditorButtonAttribute>
1010
{
11+
private void DrawButton(EditorButtonAttribute attribute)
12+
{
13+
var declaringObjects = GetDeclaringObjects();
14+
if (declaringObjects == null || declaringObjects.Length == 0)
15+
{
16+
//NOTE: something went really wrong, internal bug or OnGuiBeginSafe was called out of the Toolbox scope
17+
return;
18+
}
19+
20+
var disable = !IsClickable(attribute, declaringObjects);
21+
using (new EditorGUI.DisabledScope(disable))
22+
{
23+
var label = string.IsNullOrEmpty(attribute.ExtraLabel)
24+
? ObjectNames.NicifyVariableName(attribute.MethodName)
25+
: attribute.ExtraLabel;
26+
var tooltip = attribute.Tooltip;
27+
var content = new GUIContent(label, tooltip);
28+
29+
if (GUILayout.Button(content, Style.buttonStyle))
30+
{
31+
CallMethods(attribute, declaringObjects);
32+
}
33+
}
34+
}
35+
1136
private MethodInfo GetMethod(EditorButtonAttribute attribute, object[] targetObjects, string methodName)
1237
{
1338
var methodInfo = ReflectionUtility.GetMethod(methodName, targetObjects);
@@ -119,29 +144,25 @@ private void CallMethods(EditorButtonAttribute attribute, object[] targetObjects
119144
}
120145
}
121146

122-
protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
147+
protected override void OnGuiBeginSafe(EditorButtonAttribute attribute)
123148
{
124-
var declaringObjects = GetDeclaringObjects();
125-
if (declaringObjects == null || declaringObjects.Length == 0)
149+
if (attribute.PositionType != ButtonPositionType.Above)
126150
{
127-
//NOTE: something went really wrong, internal bug or OnGuiBeginSafe was called out of the Toolbox scope
128151
return;
129152
}
130153

131-
var disable = !IsClickable(attribute, declaringObjects);
132-
using (new EditorGUI.DisabledScope(disable))
133-
{
134-
var label = string.IsNullOrEmpty(attribute.ExtraLabel)
135-
? ObjectNames.NicifyVariableName(attribute.MethodName)
136-
: attribute.ExtraLabel;
137-
var tooltip = attribute.Tooltip;
138-
var content = new GUIContent(label, tooltip);
154+
DrawButton(attribute);
155+
}
139156

140-
if (GUILayout.Button(content, Style.buttonStyle))
141-
{
142-
CallMethods(attribute, declaringObjects);
143-
}
157+
protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
158+
{
159+
if (attribute.PositionType != ButtonPositionType.Default &&
160+
attribute.PositionType != ButtonPositionType.Below)
161+
{
162+
return;
144163
}
164+
165+
DrawButton(attribute);
145166
}
146167

147168
private static class Style

Assets/Editor Toolbox/Editor/ToolboxEditorGui.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -528,12 +528,7 @@ public static void DrawPropertyChildren(SerializedProperty property, Action<Seri
528528
enterChildren = false;
529529
var childProperty = targetProperty.Copy();
530530
//handle current property using Toolbox features
531-
EditorGUI.BeginChangeCheck();
532531
drawElementAction(childProperty);
533-
if (EditorGUI.EndChangeCheck())
534-
{
535-
break;
536-
}
537532
}
538533
}
539534

Assets/Editor Toolbox/Runtime/Attributes/Property/Toolbox/DecoratorAttributes/EditorButtonAttribute.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ public EditorButtonAttribute(string methodName, string extraLabel = null, Button
2727
public string ValidateMethodName { get; set; }
2828
public string ExtraLabel { get; private set; }
2929
public string Tooltip { get; set; }
30-
public ButtonActivityType ActivityType { get; private set; }
30+
public ButtonActivityType ActivityType { get; set; }
31+
public ButtonPositionType PositionType { get; set; } = ButtonPositionType.Default;
32+
}
33+
34+
public enum ButtonPositionType
35+
{
36+
Default,
37+
Below,
38+
Above
3139
}
3240

3341
[Flags]

Assets/Editor Toolbox/Runtime/Serialization/SerializedScene.cs

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,42 @@ void ISerializationCallbackReceiver.OnBeforeSerialize()
3333
void ISerializationCallbackReceiver.OnAfterDeserialize()
3434
{ }
3535

36+
public void UpdateProperties()
37+
{
38+
UpdateProperties(out _);
39+
}
3640

37-
private void UpdateProperties()
41+
public void UpdateProperties(out bool anythingChanged)
3842
{
43+
anythingChanged = false;
3944
#if UNITY_EDITOR
4045
if (SceneSerializationUtility.TryGetSceneData(sceneReference, out var sceneData))
4146
{
42-
SceneName = sceneData.SceneName;
43-
ScenePath = sceneData.ScenePath;
44-
BuildIndex = sceneData.BuildIndex;
47+
if (SceneName != sceneData.SceneName)
48+
{
49+
SceneName = sceneData.SceneName;
50+
anythingChanged = true;
51+
}
52+
53+
if (ScenePath != sceneData.ScenePath)
54+
{
55+
ScenePath = sceneData.ScenePath;
56+
anythingChanged = true;
57+
}
58+
59+
if (BuildIndex != sceneData.BuildIndex)
60+
{
61+
BuildIndex = sceneData.BuildIndex;
62+
anythingChanged = true;
63+
}
4564
}
4665
else
4766
{
67+
if (!string.IsNullOrEmpty(sceneName))
68+
{
69+
anythingChanged = true;
70+
}
71+
4872
SceneName = string.Empty;
4973
ScenePath = string.Empty;
5074
BuildIndex = -1;

Assets/Examples/Scripts/SampleBehaviour4.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ private string GetHelpMessage()
3434

3535
[Label("Button", skinStyle: SkinStyle.Box)]
3636

37-
[EditorButton(nameof(TestMethod), Tooltip = "Custom Tooltip", ValidateMethodName = nameof(ValidationMethod))]
37+
[EditorButton(nameof(TestMethod), Tooltip = "Custom Tooltip", ValidateMethodName = nameof(ValidationMethod), PositionType = ButtonPositionType.Above)]
3838
[EditorButton(nameof(TestCoroutine), "<b>Test Coroutine</b>", activityType: ButtonActivityType.OnPlayMode)]
3939
[EditorButton(nameof(TestStaticMethod), activityType: ButtonActivityType.OnEditMode)]
4040
public int var1;

0 commit comments

Comments
 (0)