Skip to content

Commit 3d4e8ab

Browse files
authored
Merge pull request #93 from arimger/develop
Develop - 0.12.7
2 parents c86a67f + 193cfac commit 3d4e8ab

File tree

16 files changed

+322
-38
lines changed

16 files changed

+322
-38
lines changed

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## 0.12.7 [10.12.2023]
2+
3+
### Changed:
4+
- Possibility to interact with ProgressBarDrawer (added IsInteractable property to the ProgressBarAttribute)
5+
- MinMaxAttribute now supports Vector2Int
6+
7+
### Added:
8+
- 'Revert Prefab Name' option for prefabs in the GameObject/Prefabs context menu
9+
110
## 0.12.6 [19.10.2023]
211

312
### Changed:

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

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,49 @@ public class MinMaxSliderAttributeDrawer : PropertyDrawerBase
1010
{
1111
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
1212
{
13-
return base.GetPropertyHeightSafe(property, label);
13+
return EditorGUIUtility.singleLineHeight;
1414
}
1515

1616
protected override void OnGUISafe(Rect position, SerializedProperty property, GUIContent label)
1717
{
1818
var minValue = Attribute.MinValue;
1919
var maxValue = Attribute.MaxValue;
20-
var xValue = property.vector2Value.x;
21-
var yValue = property.vector2Value.y;
20+
21+
var xValue = 0.0f;
22+
var yValue = 0.0f;
23+
switch (property.propertyType)
24+
{
25+
case SerializedPropertyType.Vector2:
26+
xValue = property.vector2Value.x;
27+
yValue = property.vector2Value.y;
28+
break;
29+
case SerializedPropertyType.Vector2Int:
30+
xValue = property.vector2IntValue.x;
31+
yValue = property.vector2IntValue.y;
32+
break;
33+
}
2234

2335
label = EditorGUI.BeginProperty(position, label, property);
2436
EditorGUI.BeginChangeCheck();
37+
position = EditorGUI.PrefixLabel(position, label);
2538
using (new ZeroIndentScope())
2639
{
27-
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
40+
ToolboxEditorGui.DrawMinMaxSlider(position, ref xValue, ref yValue, minValue, maxValue);
2841
}
2942

3043
if (EditorGUI.EndChangeCheck())
3144
{
32-
property.vector2Value = new Vector2(xValue, yValue);
45+
switch (property.propertyType)
46+
{
47+
case SerializedPropertyType.Vector2:
48+
property.vector2Value = new Vector2(xValue, yValue);
49+
break;
50+
case SerializedPropertyType.Vector2Int:
51+
var intXValue = Mathf.RoundToInt(xValue);
52+
var intYValue = Mathf.RoundToInt(yValue);
53+
property.vector2IntValue = new Vector2Int(intXValue, intYValue);
54+
break;
55+
}
3356
}
3457

3558
EditorGUI.EndProperty();
@@ -38,7 +61,8 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
3861

3962
public override bool IsPropertyValid(SerializedProperty property)
4063
{
41-
return property.propertyType == SerializedPropertyType.Vector2;
64+
return property.propertyType == SerializedPropertyType.Vector2 ||
65+
property.propertyType == SerializedPropertyType.Vector2Int;
4266
}
4367

4468

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

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,61 @@ namespace Toolbox.Editor.Drawers
66
[CustomPropertyDrawer(typeof(ProgressBarAttribute))]
77
public class ProgressBarAttributeDrawer : PropertyDrawerBase
88
{
9+
private static readonly int drawerHash = nameof(ProgressBarAttributeDrawer).GetHashCode();
10+
11+
private void HandleGuiEvents(SerializedProperty property, Rect progressBarRect)
12+
{
13+
var mousePosition = Event.current.mousePosition;
14+
var id = GUIUtility.GetControlID(drawerHash, FocusType.Passive, progressBarRect);
15+
switch (Event.current.GetTypeForControl(id))
16+
{
17+
case EventType.MouseDown:
18+
if (progressBarRect.Contains(mousePosition))
19+
{
20+
GUIUtility.hotControl = id;
21+
SetProgressValue(property, progressBarRect, mousePosition.x);
22+
Event.current.Use();
23+
}
24+
break;
25+
case EventType.MouseDrag:
26+
if (GUIUtility.hotControl == id)
27+
{
28+
SetProgressValue(property, progressBarRect, mousePosition.x);
29+
Event.current.Use();
30+
}
31+
break;
32+
case EventType.MouseUp:
33+
if (GUIUtility.hotControl == id)
34+
{
35+
GUIUtility.hotControl = 0;
36+
Event.current.Use();
37+
}
38+
break;
39+
}
40+
}
41+
42+
private void SetProgressValue(SerializedProperty property, Rect progressBarRect, float xPosition)
43+
{
44+
var minValue = Attribute.MinValue;
45+
var maxValue = Attribute.MaxValue;
46+
47+
var range = progressBarRect.xMax - progressBarRect.xMin;
48+
xPosition = Mathf.Clamp(xPosition - progressBarRect.xMin, 0, range);
49+
50+
var fill = Mathf.Clamp01(xPosition / range);
51+
var newValue = (maxValue - minValue) * fill + minValue;
52+
53+
switch (property.propertyType)
54+
{
55+
case SerializedPropertyType.Integer:
56+
property.intValue = Mathf.RoundToInt(newValue);
57+
break;
58+
case SerializedPropertyType.Float:
59+
property.floatValue = newValue;
60+
break;
61+
}
62+
}
63+
964
protected override float GetPropertyHeightSafe(SerializedProperty property, GUIContent label)
1065
{
1166
return Style.barHeight;
@@ -51,19 +106,23 @@ protected override void OnGUISafe(Rect position, SerializedProperty property, GU
51106
position.y -= Style.textOffset;
52107
//finally draw the progress bar label
53108
EditorGUI.DropShadowLabel(position, labelText);
54-
}
55109

110+
if (!attribute.IsInteractable)
111+
{
112+
return;
113+
}
114+
115+
HandleGuiEvents(property, position);
116+
}
56117

57118
public override bool IsPropertyValid(SerializedProperty property)
58119
{
59120
return property.propertyType == SerializedPropertyType.Float ||
60121
property.propertyType == SerializedPropertyType.Integer;
61122
}
62123

63-
64124
private ProgressBarAttribute Attribute => attribute as ProgressBarAttribute;
65125

66-
67126
private static class Style
68127
{
69128
internal static readonly float rowHeight = EditorGUIUtility.singleLineHeight;
Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
1-
using UnityEditor;
1+
using UnityEditor;
22
using UnityEngine;
33

44
namespace Toolbox.Editor.Drawers
55
{
6+
using Toolbox.Editor.Internal;
7+
68
public class DynamicMinMaxSliderAttributeDrawer : DynamicMinMaxBaseDrawer<DynamicMinMaxSliderAttribute>
79
{
810
protected override void OnGuiSafe(SerializedProperty property, GUIContent label, float minValue, float maxValue)
911
{
10-
var xValue = property.vector2Value.x;
11-
var yValue = property.vector2Value.y;
12+
var xValue = 0.0f;
13+
var yValue = 0.0f;
14+
switch (property.propertyType)
15+
{
16+
case SerializedPropertyType.Vector2:
17+
xValue = property.vector2Value.x;
18+
yValue = property.vector2Value.y;
19+
break;
20+
case SerializedPropertyType.Vector2Int:
21+
xValue = property.vector2IntValue.x;
22+
yValue = property.vector2IntValue.y;
23+
break;
24+
}
25+
1226
ToolboxEditorGui.BeginProperty(property, ref label, out var position);
27+
position = EditorGUI.PrefixLabel(position, label);
1328
EditorGUI.BeginChangeCheck();
14-
ToolboxEditorGui.DrawMinMaxSlider(position, label, ref xValue, ref yValue, minValue, maxValue);
29+
using (new ZeroIndentScope())
30+
{
31+
ToolboxEditorGui.DrawMinMaxSlider(position, ref xValue, ref yValue, minValue, maxValue);
32+
}
33+
1534
if (EditorGUI.EndChangeCheck())
1635
{
17-
property.vector2Value = new Vector2(xValue, yValue);
36+
switch (property.propertyType)
37+
{
38+
case SerializedPropertyType.Vector2:
39+
property.vector2Value = new Vector2(xValue, yValue);
40+
break;
41+
case SerializedPropertyType.Vector2Int:
42+
var intXValue = Mathf.RoundToInt(xValue);
43+
var intYValue = Mathf.RoundToInt(yValue);
44+
property.vector2IntValue = new Vector2Int(intXValue, intYValue);
45+
break;
46+
}
1847
}
1948

2049
ToolboxEditorGui.CloseProperty();
@@ -23,7 +52,8 @@ protected override void OnGuiSafe(SerializedProperty property, GUIContent label,
2352

2453
public override bool IsPropertyValid(SerializedProperty property)
2554
{
26-
return property.propertyType == SerializedPropertyType.Vector2;
55+
return property.propertyType == SerializedPropertyType.Vector2 ||
56+
property.propertyType == SerializedPropertyType.Vector2Int;
2757
}
2858
}
2959
}

Assets/Editor Toolbox/Editor/ToolboxEditorGui.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,14 @@ public static void DrawTexture(Rect rect, Texture texture, ScaleMode scaleMode,
199199
GUI.DrawTexture(rect, texture, scaleMode, alphaBlend);
200200
}
201201

202-
public static void DrawMinMaxSlider(Rect rect, string label, ref float xValue, ref float yValue, float minValue, float maxValue)
202+
public static void DrawMinMaxSlider(Rect rect, ref float xValue, ref float yValue, float minValue, float maxValue)
203203
{
204-
DrawMinMaxSlider(rect, new GUIContent(label), ref xValue, ref yValue, minValue, maxValue);
205-
}
206-
207-
public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValue, ref float yValue, float minValue, float maxValue)
208-
{
209-
rect = EditorGUI.PrefixLabel(rect, label);
210-
211204
var fieldWidth = EditorGUIUtility.fieldWidth;
212205
var minFieldRect = new Rect(rect.xMin, rect.y, fieldWidth, rect.height);
213206
var maxFieldRect = new Rect(rect.xMax - fieldWidth, rect.y, fieldWidth, rect.height);
214207

215208
//set slider rect between min and max fields + additional padding
216-
var spacing = 8.0f;
209+
const float spacing = 8.0f;
217210
var sliderRect = Rect.MinMaxRect(minFieldRect.xMax + spacing,
218211
rect.yMin,
219212
maxFieldRect.xMin - spacing,
@@ -229,6 +222,17 @@ public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValu
229222
yValue = Mathf.Clamp(yValue, Mathf.Max(minValue, xValue), maxValue);
230223
}
231224

225+
public static void DrawMinMaxSlider(Rect rect, string label, ref float xValue, ref float yValue, float minValue, float maxValue)
226+
{
227+
DrawMinMaxSlider(rect, new GUIContent(label), ref xValue, ref yValue, minValue, maxValue);
228+
}
229+
230+
public static void DrawMinMaxSlider(Rect rect, GUIContent label, ref float xValue, ref float yValue, float minValue, float maxValue)
231+
{
232+
rect = EditorGUI.PrefixLabel(rect, label);
233+
DrawMinMaxSlider(rect, ref xValue, ref yValue, minValue, maxValue);
234+
}
235+
232236
public static void BoldLabel(Rect rect, string label)
233237
{
234238
BoldLabel(rect, new GUIContent(label));

Assets/Editor Toolbox/Editor/ToolboxEditorSceneView.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,16 @@ private static List<GameObject> GetObjectsUnderCursor()
4040
private static void UpdateEventCallback()
4141
{
4242
#if UNITY_2019_1_OR_NEWER
43-
UnityEditor.SceneView.duringSceneGui -= SceneViewDuringSceneGUI;
43+
UnityEditor.SceneView.duringSceneGui -= SceneViewDuringSceneGui;
4444

4545
if (UseToolboxSceneView)
4646
{
47-
UnityEditor.SceneView.duringSceneGui += SceneViewDuringSceneGUI;
47+
UnityEditor.SceneView.duringSceneGui += SceneViewDuringSceneGui;
4848
}
4949
#endif
5050
}
5151

52-
private static void SceneViewDuringSceneGUI(UnityEditor.SceneView sceneView)
52+
private static void SceneViewDuringSceneGui(UnityEditor.SceneView sceneView)
5353
{
5454
if (Event.current.type != EventType.KeyDown ||
5555
Event.current.keyCode != SelectorKey)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
using UnityEditor;
2+
3+
namespace Toolbox.Editor.Utilities
4+
{
5+
public static class PrefabUtility
6+
{
7+
[MenuItem("GameObject/Prefab/Revert Prefab Name", true, -100)]
8+
public static bool ValidateRevertPrefabName()
9+
{
10+
var gameObjects = Selection.gameObjects;
11+
for (int i = 0; i < gameObjects.Length; i++)
12+
{
13+
var gameObject = gameObjects[i];
14+
if (UnityEditor.PrefabUtility.IsAnyPrefabInstanceRoot(gameObject))
15+
{
16+
return true;
17+
}
18+
}
19+
20+
return false;
21+
}
22+
23+
[MenuItem("GameObject/Prefab/Revert Prefab Name", false, -100)]
24+
public static void RevertPrefabName()
25+
{
26+
var gameObjects = Selection.gameObjects;
27+
Undo.RecordObjects(gameObjects, "Revert Prefab Name");
28+
for (int i = 0; i < gameObjects.Length; i++)
29+
{
30+
var gameObject = gameObjects[i];
31+
var prefabObject = UnityEditor.PrefabUtility.GetCorrespondingObjectFromSource(gameObject);
32+
if (prefabObject == null)
33+
{
34+
continue;
35+
}
36+
37+
gameObject.name = prefabObject.name;
38+
}
39+
}
40+
}
41+
}

Assets/Editor Toolbox/Editor/Utilities/PrefabUtility.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)