Skip to content

Commit 830e64c

Browse files
authored
Merge pull request #86 from arimger/develop
Develop - 0.12.5
2 parents 1f2c62a + 8cdab67 commit 830e64c

13 files changed

+94
-98
lines changed

Assets/Editor Toolbox/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
## 0.12.5 [11.09.2023]
2+
3+
### Changed:
4+
- Make ToolboxEditorHandler public
5+
6+
### Added:
7+
- Add public OnCacheRefreshed event to the SceneSerializationUtility
8+
19
## 0.12.4 [31.07.2023]
210

311
### Changed:

Assets/Editor Toolbox/Editor/Drawers/Helpers/DrawerDataStorageBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ public abstract class DrawerDataStorageBase
66
{
77
static DrawerDataStorageBase()
88
{
9-
InspectorUtility.OnEditorReload += () =>
9+
ToolboxEditorHandler.OnEditorReload += () =>
1010
{
1111
for (var i = 0; i < storages.Count; i++)
1212
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public class DynamicHelpAttributeDrawer : ToolboxDecoratorDrawer<DynamicHelpAttr
88
protected override void OnGuiBeginSafe(DynamicHelpAttribute attribute)
99
{
1010
var sourceHandle = attribute.SourceHandle;
11-
var targetObjects = InspectorUtility.CurrentTargetObjects;
11+
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
1212
if (ValueExtractionHelper.TryGetValue(sourceHandle, targetObjects, out var value, out var hasMixedValues))
1313
{
1414
var messageText = hasMixedValues ? "-" : value?.ToString();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ private void CallMethods(EditorButtonAttribute attribute, Object[] targetObjects
123123

124124
protected override void OnGuiCloseSafe(EditorButtonAttribute attribute)
125125
{
126-
var targetObjects = InspectorUtility.CurrentTargetObjects;
126+
var targetObjects = ToolboxEditorHandler.CurrentTargetObjects;
127127
if (targetObjects == null || targetObjects.Length == 0)
128128
{
129129
//NOTE: something went really wrong, internal bug or OnGuiBeginSafe was called out of the Toolbox scope

Assets/Editor Toolbox/Editor/Editors/ToolboxScriptedImporterEditor.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ public void IgnoreProperty(string propertyPath)
3838
Drawer.IgnoreProperty(propertyPath);
3939
}
4040

41-
4241
Editor IToolboxEditor.ContextEditor => this;
4342
public IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();
4443
}

Assets/Editor Toolbox/Editor/ToolboxDrawerModule.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ internal static class ToolboxDrawerModule
2020
[InitializeOnLoadMethod]
2121
internal static void InitializeModule()
2222
{
23-
InspectorUtility.OnEditorReload += ReloadDrawers;
23+
ToolboxEditorHandler.OnEditorReload += ReloadDrawers;
2424
}
2525

2626

@@ -405,7 +405,7 @@ internal static List<Type> GetAllPossibleTargetTypeDrawers()
405405
/// </summary>
406406
internal static ToolboxPropertyHandler GetPropertyHandler(SerializedProperty property)
407407
{
408-
if (InspectorUtility.InToolboxEditor)
408+
if (ToolboxEditorHandler.InToolboxEditor)
409409
{
410410
//NOTE: maybe type-based key?
411411
var propertyKey = property.GetPropertyHashKey();

Assets/Editor Toolbox/Editor/ToolboxEditor.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
32
using UnityEditor;
43

54
namespace Toolbox.Editor
@@ -45,7 +44,6 @@ public void IgnoreProperty(string propertyPath)
4544
Drawer.IgnoreProperty(propertyPath);
4645
}
4746

48-
4947
Editor IToolboxEditor.ContextEditor => this;
5048
/// <inheritdoc />
5149
public virtual IToolboxEditorDrawer Drawer { get; } = new ToolboxEditorDrawer();

Assets/Editor Toolbox/Editor/ToolboxEditorDrawer.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
43
using UnityEditor;
54

65
namespace Toolbox.Editor
Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,94 @@
11
using System;
2+
using System.Collections.Generic;
23

34
namespace Toolbox.Editor
45
{
56
using Editor = UnityEditor.Editor;
7+
using Object = UnityEngine.Object;
68

7-
internal static class ToolboxEditorHandler
9+
public static class ToolboxEditorHandler
810
{
11+
private static int lastCachedEditorId;
12+
private static Editor lastCachedEditor;
13+
private static readonly Stack<Editor> cachedEditors = new Stack<Editor>();
14+
15+
private static void OnBeginEditor(Editor editor)
16+
{
17+
//NOTE: it means that last Editor was null or disposed, anyway we probably want to reload drawers-related cache
18+
if (lastCachedEditor == null || lastCachedEditorId != lastCachedEditor.GetInstanceID())
19+
{
20+
lastCachedEditor = editor;
21+
lastCachedEditorId = editor.GetInstanceID();
22+
OnEditorReload?.Invoke();
23+
}
24+
25+
cachedEditors.Push(editor);
26+
OnBeginToolboxEditor?.Invoke(editor);
27+
}
28+
29+
private static void OnBreakEditor(Editor editor)
30+
{
31+
cachedEditors.Clear();
32+
OnBreakToolboxEditor?.Invoke(editor);
33+
}
34+
35+
private static void OnCloseEditor(Editor editor)
36+
{
37+
if (InToolboxEditor)
38+
{
39+
cachedEditors.Pop();
40+
}
41+
42+
OnCloseToolboxEditor?.Invoke(editor);
43+
ContextEditor = null;
44+
}
45+
946
public static void HandleToolboxEditor(IToolboxEditor editor)
1047
{
1148
try
1249
{
1350
ContextEditor = editor.ContextEditor;
14-
OnBeginToolboxEditor?.Invoke(ContextEditor);
51+
OnBeginEditor(ContextEditor);
1552
editor.DrawCustomInspector();
1653
}
1754
catch (Exception)
1855
{
1956
//make sure to catch all Exceptions (especially ExitGUIException),
2057
//it will allow us to safely dispose all layout-based controls, etc.
21-
OnBreakToolboxEditor?.Invoke(ContextEditor);
58+
OnBreakEditor(ContextEditor);
2259
throw;
2360
}
2461
finally
2562
{
26-
OnCloseToolboxEditor?.Invoke(ContextEditor);
27-
ContextEditor = null;
63+
OnCloseEditor(ContextEditor);
2864
}
2965
}
3066

67+
/// <summary>
68+
/// Event fired every time when <see cref="ToolboxEditor"/>s were re-created.
69+
/// </summary>
70+
internal static event Action OnEditorReload;
71+
72+
internal static event Action<Editor> OnBeginToolboxEditor;
73+
internal static event Action<Editor> OnBreakToolboxEditor;
74+
internal static event Action<Editor> OnCloseToolboxEditor;
3175

32-
public static event Action<Editor> OnBeginToolboxEditor;
33-
public static event Action<Editor> OnBreakToolboxEditor;
34-
public static event Action<Editor> OnCloseToolboxEditor;
76+
internal static bool InToolboxEditor
77+
{
78+
get => cachedEditors.Count > 0;
79+
}
80+
81+
/// <summary>
82+
/// Last cached targetObjects from the currently processed <see cref="ToolboxEditor"/>.
83+
/// </summary>
84+
internal static Object[] CurrentTargetObjects
85+
{
86+
get => cachedEditors.Count > 0 ? cachedEditors.Peek().targets : new Object[0];
87+
}
3588

3689
/// <summary>
3790
/// Currently maintained <see cref="Editor"/>.
3891
/// </summary>
39-
public static Editor ContextEditor { get; private set; }
92+
internal static Editor ContextEditor { get; private set; }
4093
}
4194
}

Assets/Editor Toolbox/Editor/ToolboxLayoutHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ static ToolboxLayoutHandler()
2323
ToolboxEditorHandler.OnCloseToolboxEditor += OnCloseEditor;
2424

2525
//we have to reset the current state between Editors to keep values independent
26-
InspectorUtility.OnEditorReload += ResetCache;
26+
ToolboxEditorHandler.OnEditorReload += ResetCache;
2727
}
2828

2929

0 commit comments

Comments
 (0)