@@ -22,28 +22,32 @@ sealed class EditingContextManager : MonoBehaviour
2222
2323 const string k_LaunchOnExitPlaymode = "EditingContextManager.LaunchOnExitPlaymode" ;
2424
25+ IEditingContext m_CurrentContext ;
26+
2527 internal static EditingContextManager s_Instance ;
2628 static InputManager s_InputManager ;
2729 static List < IEditingContext > s_AvailableContexts ;
2830 static EditingContextManagerSettings s_Settings ;
2931 static UnityObject s_DefaultContext ;
30- static IEditingContext s_CurrentContext ;
3132
3233 string [ ] m_ContextNames ;
3334 int m_SelectedContextIndex ;
3435
36+ Rect m_EditingContextPopupRect = new Rect ( 0 , 0 , 150 , 20 ) ; // Y and X position will be set based on window size
37+
3538 readonly List < IEditingContext > m_PreviousContexts = new List < IEditingContext > ( ) ;
3639
3740 internal static IEditingContext defaultContext
3841 {
3942 get
4043 {
41- var context = s_AvailableContexts . Find ( c => c . Equals ( s_DefaultContext ) ) ?? s_AvailableContexts . First ( ) ;
44+ var availableContexts = GetAvailableEditingContexts ( ) ;
45+ var context = availableContexts . Find ( c => c . Equals ( s_DefaultContext ) ) ?? availableContexts . First ( ) ;
4246
43- var defaultContextName = s_Settings . defaultContextName ;
47+ var defaultContextName = settings . defaultContextName ;
4448 if ( ! string . IsNullOrEmpty ( defaultContextName ) )
4549 {
46- var foundContext = s_AvailableContexts . Find ( c => c . name == defaultContextName ) ;
50+ var foundContext = availableContexts . Find ( c => c . name == defaultContextName ) ;
4751 if ( foundContext != null )
4852 context = foundContext ;
4953 }
@@ -52,20 +56,30 @@ internal static IEditingContext defaultContext
5256 }
5357 set
5458 {
55- s_Settings . defaultContextName = value . name ;
59+ settings . defaultContextName = value . name ;
5660 }
5761 }
5862
5963 internal IEditingContext currentContext
6064 {
61- get { return s_CurrentContext ; }
65+ get { return m_CurrentContext ; }
66+ }
67+
68+ static EditingContextManagerSettings settings
69+ {
70+ get
71+ {
72+ if ( ! s_Settings )
73+ s_Settings = LoadUserSettings ( ) ;
74+
75+ return s_Settings ;
76+ }
6277 }
6378
6479 static EditingContextManager ( )
6580 {
6681 VRView . viewEnabled += OnVRViewEnabled ;
6782 VRView . viewDisabled += OnVRViewDisabled ;
68- VRView . cameraSetupStarted += OnCameraSetupStarted ;
6983
7084 EditorApplication . update += ReopenOnExitPlaymode ;
7185 }
@@ -82,11 +96,6 @@ static void OnVRViewDisabled()
8296 ObjectUtils . Destroy ( s_InputManager . gameObject ) ;
8397 }
8498
85- public static IEditingContext OnCameraSetupStarted ( )
86- {
87- return s_CurrentContext ;
88- }
89-
9099 [ MenuItem ( "Window/EditorXR %e" , false ) ]
91100 internal static void ShowEditorVR ( )
92101 {
@@ -138,33 +147,31 @@ static void OnPlayModeStateChanged(PlayModeStateChange stateChange)
138147 void Awake ( )
139148 {
140149 s_DefaultContext = m_DefaultContext ;
141- }
142-
143- void OnEnable ( )
144- {
145- s_Settings = LoadUserSettings ( ) ;
146-
147- ISetEditingContextMethods . getAvailableEditingContexts = GetAvailableEditingContexts ;
148- ISetEditingContextMethods . getPreviousEditingContexts = GetPreviousEditingContexts ;
149- ISetEditingContextMethods . setEditingContext = SetEditingContext ;
150- ISetEditingContextMethods . restorePreviousEditingContext = RestorePreviousContext ;
151150
152151 var availableContexts = GetAvailableEditingContexts ( ) ;
153152 m_ContextNames = availableContexts . Select ( c => c . name ) . ToArray ( ) ;
154153
155154 if ( s_AvailableContexts . Count == 0 )
156155 throw new Exception ( "You can't start EditorXR without at least one context. Try re-importing the package or use version control to restore the default context asset" ) ;
157156
158- SetEditingContext ( defaultContext ) ;
159-
160157 if ( s_AvailableContexts . Count > 1 )
161158 VRView . afterOnGUI += OnVRViewGUI ;
159+ }
160+
161+ void OnEnable ( )
162+ {
163+ ISetEditingContextMethods . getAvailableEditingContexts = GetAvailableEditingContexts ;
164+ ISetEditingContextMethods . getPreviousEditingContexts = GetPreviousEditingContexts ;
165+ ISetEditingContextMethods . setEditingContext = SetEditingContext ;
166+ ISetEditingContextMethods . restorePreviousEditingContext = RestorePreviousContext ;
162167
163168 // Force the window to repaint every tick, since we need live updating
164169 // This also allows scripts with [ExecuteInEditMode] to run
165170 EditorApplication . update += EditorApplication . QueuePlayerLoopUpdate ;
166171
167172 EditorApplication . playModeStateChanged += OnPlayModeStateChanged ;
173+
174+ SetEditingContext ( defaultContext ) ;
168175 }
169176
170177 void OnDisable ( )
@@ -175,46 +182,35 @@ void OnDisable()
175182
176183 VRView . afterOnGUI -= OnVRViewGUI ;
177184
178- if ( s_CurrentContext != null )
185+ if ( m_CurrentContext != null )
179186 {
180- defaultContext = s_CurrentContext ;
181- s_CurrentContext . Dispose ( ) ;
187+ defaultContext = m_CurrentContext ;
188+ m_CurrentContext . Dispose ( ) ;
182189 }
183190
184191 s_AvailableContexts = null ;
185192
193+ SetEditingContext ( null ) ;
194+
186195 ISetEditingContextMethods . getAvailableEditingContexts = null ;
187196 ISetEditingContextMethods . getPreviousEditingContexts = null ;
188197 ISetEditingContextMethods . setEditingContext = null ;
189198 ISetEditingContextMethods . restorePreviousEditingContext = null ;
190199
191- SaveUserSettings ( s_Settings ) ;
200+ SaveUserSettings ( settings ) ;
192201 }
193202#endif
194203
195- void OnVRViewGUI ( EditorWindow window )
204+ void OnVRViewGUI ( VRView view )
196205 {
197- var view = ( VRView ) window ;
198- GUILayout . BeginArea ( view . guiRect ) ;
199- {
200- GUILayout . FlexibleSpace ( ) ;
201- GUILayout . BeginHorizontal ( ) ;
202- {
203- DoGUI ( m_ContextNames , ref m_SelectedContextIndex , ( ) => SetEditingContext ( s_AvailableContexts [ m_SelectedContextIndex ] ) ) ;
204- GUILayout . FlexibleSpace ( ) ;
205- }
206- GUILayout . EndHorizontal ( ) ;
207- }
208- GUILayout . EndArea ( ) ;
209- }
206+ var position = view . position ;
207+ m_EditingContextPopupRect . y = position . height - m_EditingContextPopupRect . height ;
208+ m_EditingContextPopupRect . x = position . width - m_EditingContextPopupRect . width ;
210209
211- internal static void DoGUI ( string [ ] contextNames , ref int selectedContextIndex , Action callback = null )
212- {
213- selectedContextIndex = EditorGUILayout . Popup ( string . Empty , selectedContextIndex , contextNames ) ;
210+ m_SelectedContextIndex = EditorGUI . Popup ( m_EditingContextPopupRect , string . Empty , m_SelectedContextIndex , m_ContextNames ) ;
214211 if ( GUI . changed )
215212 {
216- if ( callback != null )
217- callback ( ) ;
213+ SetEditingContext ( s_AvailableContexts [ m_SelectedContextIndex ] ) ;
218214 GUIUtility . ExitGUI ( ) ;
219215 }
220216 }
@@ -224,16 +220,16 @@ internal void SetEditingContext(IEditingContext context)
224220 if ( context == null )
225221 return ;
226222
227- if ( s_CurrentContext != null )
223+ if ( m_CurrentContext != null )
228224 {
229- m_PreviousContexts . Insert ( 0 , s_CurrentContext ) ;
225+ m_PreviousContexts . Insert ( 0 , m_CurrentContext ) ;
230226
231- if ( s_CurrentContext . instanceExists )
232- s_CurrentContext . Dispose ( ) ;
227+ if ( m_CurrentContext . instanceExists )
228+ m_CurrentContext . Dispose ( ) ;
233229 }
234230
235231 context . Setup ( ) ;
236- s_CurrentContext = context ;
232+ m_CurrentContext = context ;
237233
238234 m_SelectedContextIndex = s_AvailableContexts . IndexOf ( context ) ;
239235 }
0 commit comments