-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathToolboxEditorHandler.cs
More file actions
111 lines (98 loc) · 3.42 KB
/
ToolboxEditorHandler.cs
File metadata and controls
111 lines (98 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Collections.Generic;
// NOTE: Needed for EntityId
#if UNITY_6000_4_OR_NEWER
using UnityEngine;
#endif
namespace Toolbox.Editor
{
using Editor = UnityEditor.Editor;
using Object = UnityEngine.Object;
public static class ToolboxEditorHandler
{
#if UNITY_6000_4_OR_NEWER
private static EntityId lastCachedEditorId;
#else
private static int lastCachedEditorId;
#endif
private static Editor lastCachedEditor;
private static readonly Stack<Editor> cachedEditors = new Stack<Editor>();
private static void OnBeginEditor(Editor editor)
{
#if UNITY_6000_4_OR_NEWER
var lastId = lastCachedEditor != null ? lastCachedEditor.GetEntityId() : default;
#else
var lastId = lastCachedEditor != null ? lastCachedEditor.GetInstanceID() : default;
#endif
//NOTE: it means that last Editor was null or disposed, anyway we probably want to reload drawers-related cache
if (lastCachedEditor == null || lastCachedEditorId != lastId)
{
lastCachedEditor = editor;
#if UNITY_6000_4_OR_NEWER
lastCachedEditorId = editor.GetEntityId();
#else
lastCachedEditorId = editor.GetInstanceID();
#endif
OnEditorReload?.Invoke();
}
cachedEditors.Push(editor);
OnBeginToolboxEditor?.Invoke(editor);
}
private static void OnBreakEditor(Editor editor)
{
cachedEditors.Clear();
OnBreakToolboxEditor?.Invoke(editor);
}
private static void OnCloseEditor(Editor editor)
{
if (InToolboxEditor)
{
cachedEditors.Pop();
}
OnCloseToolboxEditor?.Invoke(editor);
ContextEditor = null;
}
public static void HandleToolboxEditor(IToolboxEditor editor)
{
try
{
ContextEditor = editor.ContextEditor;
OnBeginEditor(ContextEditor);
editor.DrawCustomInspector();
}
catch (Exception)
{
//make sure to catch all Exceptions (especially ExitGUIException),
//it will allow us to safely dispose all layout-based controls, etc.
OnBreakEditor(ContextEditor);
throw;
}
finally
{
OnCloseEditor(ContextEditor);
}
}
/// <summary>
/// Event fired every time when <see cref="ToolboxEditor"/>s were re-created.
/// </summary>
internal static event Action OnEditorReload;
internal static event Action<Editor> OnBeginToolboxEditor;
internal static event Action<Editor> OnBreakToolboxEditor;
internal static event Action<Editor> OnCloseToolboxEditor;
internal static bool InToolboxEditor
{
get => cachedEditors.Count > 0;
}
/// <summary>
/// Last cached targetObjects from the currently processed <see cref="ToolboxEditor"/>.
/// </summary>
internal static Object[] CurrentTargetObjects
{
get => cachedEditors.Count > 0 ? cachedEditors.Peek().targets : new Object[0];
}
/// <summary>
/// Currently maintained <see cref="Editor"/>.
/// </summary>
internal static Editor ContextEditor { get; private set; }
}
}