@@ -94,6 +94,8 @@ sealed class AudioContainerWindow : EditorWindow
9494 Texture2D m_DiceIconOff ;
9595 Texture2D m_DiceIconOn ;
9696
97+ bool m_IsVisible ;
98+ bool m_IsSubscribedToGUICallbacksAndEvents ;
9799 bool m_IsInitializing ;
98100 bool m_Day0ElementsInitialized ;
99101 bool m_ContainerElementsInitialized ;
@@ -120,10 +122,6 @@ void OnEnable()
120122 {
121123 Instance = this ;
122124
123- State . TargetChanged += OnTargetChanged ;
124- State . TransportStateChanged += OnTransportStateChanged ;
125- State . EditorPauseStateChanged += EditorPauseStateChanged ;
126-
127125 m_DiceIconOff = EditorGUIUtility . IconContent ( "AudioRandomContainer On Icon" ) . image as Texture2D ;
128126 m_DiceIconOn = EditorGUIUtility . IconContent ( "AudioRandomContainer Icon" ) . image as Texture2D ;
129127
@@ -137,27 +135,16 @@ void OnEnable()
137135 void OnDisable ( )
138136 {
139137 Instance = null ;
140-
141- UnsubscribeFromPreviewCallbacksAndEvents ( ) ;
142- UnsubscribeFromVolumeCallbacksAndEvents ( ) ;
143- UnsubscribeFromPitchCallbacksAndEvents ( ) ;
144- UnsubscribeFromClipListCallbacksAndEvents ( ) ;
145- UnsubscribeFromAutomaticTriggerCallbacksAndEvents ( ) ;
146-
147- EditorApplication . update -= OneTimeEditorApplicationUpdate ;
148-
149- State . TargetChanged -= OnTargetChanged ;
150- State . TransportStateChanged -= OnTransportStateChanged ;
151- State . EditorPauseStateChanged -= EditorPauseStateChanged ;
152-
153138 State . OnDestroy ( ) ;
154-
155139 m_CachedElements . Clear ( ) ;
156140 m_AddedElements . Clear ( ) ;
157141 }
158142
159143 void Update ( )
160144 {
145+ if ( ! m_IsVisible )
146+ return ;
147+
161148 if ( State . IsPlayingOrPaused ( ) ) { UpdateClipFieldProgressBars ( ) ; }
162149 else if ( ! m_ClipFieldProgressBarsAreCleared ) { ClearClipFieldProgressBars ( ) ; }
163150
@@ -175,12 +162,6 @@ void Update()
175162 }
176163 }
177164
178- void OnBecameInvisible ( )
179- {
180- State . Stop ( ) ;
181- ClearClipFieldProgressBars ( ) ;
182- }
183-
184165 void SetTitle ( )
185166 {
186167 var titleString = "Audio Random Container" ;
@@ -222,6 +203,9 @@ void CreateGUI()
222203 Assert . IsNotNull ( m_Day0RootVisualElement ) ;
223204 }
224205
206+ if ( m_ContainerElementsInitialized )
207+ root . Unbind ( ) ;
208+
225209 if ( State . AudioContainer == null )
226210 {
227211 if ( ! m_Day0ElementsInitialized )
@@ -235,15 +219,16 @@ void CreateGUI()
235219 }
236220 else
237221 {
238- if ( m_ContainerElementsInitialized )
239- root . Unbind ( ) ;
240- else
222+ if ( ! m_ContainerElementsInitialized )
241223 {
242- InitializeElements ( ) ;
243- SubscribeToCallbacksAndEvents ( ) ;
224+ InitializeContainerElements ( ) ;
244225 m_ContainerElementsInitialized = true ;
226+ EditorApplication . update += OneTimeEditorApplicationUpdate ;
245227 }
246228
229+ if ( ! m_IsSubscribedToGUICallbacksAndEvents )
230+ SubscribeToGUICallbacksAndEvents ( ) ;
231+
247232 BindAndTrackObjectAndProperties ( ) ;
248233
249234 m_Day0RootVisualElement . style . display = DisplayStyle . None ;
@@ -257,6 +242,15 @@ void CreateGUI()
257242 }
258243 }
259244
245+ bool IsDisplayingTarget ( )
246+ {
247+ return
248+ m_Day0RootVisualElement != null
249+ && m_Day0RootVisualElement . style . display == DisplayStyle . None
250+ && m_ContainerRootVisualElement != null
251+ && m_ContainerRootVisualElement . style . display == DisplayStyle . Flex ;
252+ }
253+
260254 void InitializeDay0Elements ( )
261255 {
262256 var createButtonLabel = UIToolkitUtilities . GetChildByName < Label > ( m_Day0RootVisualElement , "CreateButtonLabel" ) ;
@@ -265,7 +259,7 @@ void InitializeDay0Elements()
265259 createButtonLabel . text = "Select an existing Audio Random Container asset in the project browser or create a new one using the button below." ;
266260 }
267261
268- void InitializeElements ( )
262+ void InitializeContainerElements ( )
269263 {
270264 InitializePreviewElements ( ) ;
271265 InitializeVolumeElements ( ) ;
@@ -275,6 +269,26 @@ void InitializeElements()
275269 InitializeAutomaticTriggerElements ( ) ;
276270 }
277271
272+ void SubscribeToGUICallbacksAndEvents ( )
273+ {
274+ SubscribeToPreviewCallbacksAndEvents ( ) ;
275+ SubscribeToVolumeCallbacksAndEvents ( ) ;
276+ SubscribeToPitchCallbacksAndEvents ( ) ;
277+ SubscribeToClipListCallbacksAndEvents ( ) ;
278+ SubscribeToAutomaticTriggerCallbacksAndEvents ( ) ;
279+ m_IsSubscribedToGUICallbacksAndEvents = true ;
280+ }
281+
282+ void UnsubscribeFromGUICallbacksAndEvents ( )
283+ {
284+ UnsubscribeFromPreviewCallbacksAndEvents ( ) ;
285+ UnsubscribeFromVolumeCallbacksAndEvents ( ) ;
286+ UnsubscribeFromPitchCallbacksAndEvents ( ) ;
287+ UnsubscribeFromClipListCallbacksAndEvents ( ) ;
288+ UnsubscribeFromAutomaticTriggerCallbacksAndEvents ( ) ;
289+ m_IsSubscribedToGUICallbacksAndEvents = false ;
290+ }
291+
278292 void BindAndTrackObjectAndProperties ( )
279293 {
280294 m_ContainerRootVisualElement . TrackSerializedObjectValue ( State . SerializedObject , OnSerializedObjectChanged ) ;
@@ -287,23 +301,17 @@ void BindAndTrackObjectAndProperties()
287301 BindAndTrackAutomaticTriggerProperties ( ) ;
288302 }
289303
290- void SubscribeToCallbacksAndEvents ( )
291- {
292- SubscribeToPreviewCallbacksAndEvents ( ) ;
293- SubscribeToVolumeCallbacksAndEvents ( ) ;
294- SubscribeToPitchCallbacksAndEvents ( ) ;
295- SubscribeToClipListCallbacksAndEvents ( ) ;
296- SubscribeToAutomaticTriggerCallbacksAndEvents ( ) ;
297- EditorApplication . update += OneTimeEditorApplicationUpdate ;
298- }
299-
300304 void OnTargetChanged ( object sender , EventArgs e )
301305 {
302306 SetTitle ( ) ;
303307 CreateGUI ( ) ;
304308
305- if ( State . AudioContainer != null )
309+ if ( State . AudioContainer == null )
310+ m_CachedElements . Clear ( ) ;
311+ else
306312 m_CachedElements = State . AudioContainer . elements . ToList ( ) ;
313+
314+ m_AddedElements . Clear ( ) ;
307315 }
308316
309317 void OnSerializedObjectChanged ( SerializedObject obj )
@@ -1196,15 +1204,49 @@ void OnCountRandomizationButtonClicked()
11961204
11971205 #region GlobalEditorCallbackHandlers
11981206
1207+ void OnBecameVisible ( )
1208+ {
1209+ m_IsVisible = true ;
1210+ State . TargetChanged += OnTargetChanged ;
1211+ State . TransportStateChanged += OnTransportStateChanged ;
1212+ State . EditorPauseStateChanged += EditorPauseStateChanged ;
1213+ State . Resume ( ) ;
1214+
1215+ if ( ! m_IsSubscribedToGUICallbacksAndEvents
1216+ && m_ContainerElementsInitialized
1217+ && IsDisplayingTarget ( ) )
1218+ {
1219+ SubscribeToGUICallbacksAndEvents ( ) ;
1220+ }
1221+ }
1222+
1223+ void OnBecameInvisible ( )
1224+ {
1225+ m_IsVisible = false ;
1226+ State . TargetChanged -= OnTargetChanged ;
1227+ State . TransportStateChanged -= OnTransportStateChanged ;
1228+ State . EditorPauseStateChanged -= EditorPauseStateChanged ;
1229+ State . Suspend ( ) ;
1230+
1231+ if ( m_IsSubscribedToGUICallbacksAndEvents
1232+ && m_ContainerElementsInitialized
1233+ && IsDisplayingTarget ( ) )
1234+ {
1235+ UnsubscribeFromGUICallbacksAndEvents ( ) ;
1236+ }
1237+
1238+ EditorApplication . update -= OneTimeEditorApplicationUpdate ;
1239+ ClearClipFieldProgressBars ( ) ;
1240+ }
1241+
11991242 void OnWillSaveAssets ( IEnumerable < string > paths )
12001243 {
1244+ // If there is no target we are in day 0 state.
12011245 if ( State . AudioContainer == null )
12021246 return ;
12031247
1204- var currentSelectionPath = AssetDatabase . GetAssetPath ( State . AudioContainer ) ;
1205-
12061248 foreach ( var path in paths )
1207- if ( path == currentSelectionPath )
1249+ if ( path == State . TargetPath )
12081250 {
12091251 SetTitle ( ) ;
12101252 return ;
@@ -1213,27 +1255,32 @@ void OnWillSaveAssets(IEnumerable<string> paths)
12131255
12141256 void OnAssetsImported ( IEnumerable < string > paths )
12151257 {
1216- if ( State . AudioContainer == null ) return ;
1258+ // If there is no target we are in day 0 state.
1259+ if ( State . AudioContainer == null )
1260+ return ;
12171261
12181262 foreach ( var path in paths )
1219- if ( AssetDatabase . GetMainAssetTypeAtPath ( path ) == typeof ( AudioRandomContainer ) &&
1220- AssetDatabase . GetMainAssetInstanceID ( path ) == State . AudioContainer . GetInstanceID ( ) )
1263+ if ( path == State . TargetPath )
12211264 {
12221265 State . SerializedObject . Update ( ) ;
12231266 OnTargetChanged ( this , EventArgs . Empty ) ;
1267+ return ;
12241268 }
12251269 }
12261270
12271271 void OnAssetsDeleted ( IEnumerable < string > paths )
12281272 {
1273+ // The target reference will already be invalid at this point if it's been deleted.
1274+ if ( State . AudioContainer != null )
1275+ return ;
1276+
1277+ // ...but we still have the target path available for the check.
12291278 foreach ( var path in paths )
12301279 if ( path == State . TargetPath )
12311280 {
12321281 State . Reset ( ) ;
1233- SetTitle ( ) ;
1234- CreateGUI ( ) ;
1235- m_CachedElements . Clear ( ) ;
1236- break ;
1282+ OnTargetChanged ( this , EventArgs . Empty ) ;
1283+ return ;
12371284 }
12381285 }
12391286
0 commit comments