@@ -16,37 +16,47 @@ internal static partial class InspectorUtility
1616 {
1717 static InspectorUtility ( )
1818 {
19- //we can use 'OnBeginToolboxEditor' and 'OnCloseToolboxEditor' to cache
20- //processed Editors and determine the real context of the serialization
21- ToolboxEditor . OnBeginToolboxEditor += CacheTargets ;
22- ToolboxEditor . OnCloseToolboxEditor += ClearTargets ;
23-
2419 //we can use each new Editor to check if 'OnEditorReload' should be called
2520 ToolboxEditor . OnBeginToolboxEditor += CheckReloads ;
21+
22+ //we can use 'OnBeginToolboxEditor' and 'OnCloseToolboxEditor' to cache
23+ //processed Editors and determine the real context of the serialization
24+ ToolboxEditor . OnBeginToolboxEditor += OnBeginEditor ;
25+ ToolboxEditor . OnBreakToolboxEditor += OnBreakEditor ;
26+ ToolboxEditor . OnCloseToolboxEditor += OnCloseEditor ;
2627 }
2728
29+
2830 private static Editor lastCachedEditor ;
31+ private static readonly Stack < Editor > cachedEditors = new Stack < Editor > ( ) ;
2932
3033
31- private static void CacheTargets ( Editor editor )
34+ private static void OnBeginEditor ( Editor editor )
3235 {
33- CurrentTargetObjects = editor . targets ;
36+ lastCachedEditor = editor ;
37+ cachedEditors . Push ( editor ) ;
3438 }
3539
36- private static void ClearTargets ( Editor editor )
40+ private static void OnBreakEditor ( Editor editor )
3741 {
38- CurrentTargetObjects = null ;
42+ cachedEditors . Clear ( ) ;
43+ }
44+
45+ private static void OnCloseEditor ( Editor editor )
46+ {
47+ if ( InToolboxEditor )
48+ {
49+ cachedEditors . Pop ( ) ;
50+ }
3951 }
4052
4153 private static void CheckReloads ( Editor editor )
4254 {
55+ //NOTE: it means that last Editor was null or disposed, anyway we probably want to reload drawers-related cache
4356 if ( lastCachedEditor == null )
4457 {
4558 OnEditorReload ? . Invoke ( ) ;
4659 }
47-
48- //this Editor will be destroyed every time when object is deselected
49- lastCachedEditor = editor ;
5060 }
5161
5262
@@ -112,7 +122,7 @@ internal static void SimulateOnValidate(Object target)
112122 throw new ArgumentNullException ( nameof ( target ) ) ;
113123 }
114124
115- var methodInfo = target . GetType ( ) . GetMethod ( "OnValidate" ,
125+ var methodInfo = target . GetType ( ) . GetMethod ( "OnValidate" ,
116126 BindingFlags . Public | BindingFlags . NonPublic | BindingFlags . Instance ,
117127 null , CallingConventions . Any , new Type [ 0 ] , null ) ;
118128 if ( methodInfo != null )
@@ -131,9 +141,15 @@ internal static void SimulateOnValidate(Object target)
131141 /// <summary>
132142 /// Last cached targetObjects from the currently processed <see cref="ToolboxEditor"/>.
133143 /// </summary>
134- internal static Object [ ] CurrentTargetObjects { get ; private set ; }
144+ internal static Object [ ] CurrentTargetObjects
145+ {
146+ get => cachedEditors . Count > 0 ? cachedEditors . Peek ( ) . targets : new Object [ 0 ] ;
147+ }
135148
136- internal static bool InToolboxEditor => lastCachedEditor ;
149+ internal static bool InToolboxEditor
150+ {
151+ get => cachedEditors . Count > 0 ;
152+ }
137153 }
138154
139155 internal static partial class InspectorUtility
0 commit comments