@@ -94,6 +94,8 @@ sealed class AudioContainerWindow : EditorWindow
94
94
Texture2D m_DiceIconOff ;
95
95
Texture2D m_DiceIconOn ;
96
96
97
+ bool m_IsVisible ;
98
+ bool m_IsSubscribedToGUICallbacksAndEvents ;
97
99
bool m_IsInitializing ;
98
100
bool m_Day0ElementsInitialized ;
99
101
bool m_ContainerElementsInitialized ;
@@ -120,10 +122,6 @@ void OnEnable()
120
122
{
121
123
Instance = this ;
122
124
123
- State . TargetChanged += OnTargetChanged ;
124
- State . TransportStateChanged += OnTransportStateChanged ;
125
- State . EditorPauseStateChanged += EditorPauseStateChanged ;
126
-
127
125
m_DiceIconOff = EditorGUIUtility . IconContent ( "AudioRandomContainer On Icon" ) . image as Texture2D ;
128
126
m_DiceIconOn = EditorGUIUtility . IconContent ( "AudioRandomContainer Icon" ) . image as Texture2D ;
129
127
@@ -137,27 +135,16 @@ void OnEnable()
137
135
void OnDisable ( )
138
136
{
139
137
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
-
153
138
State . OnDestroy ( ) ;
154
-
155
139
m_CachedElements . Clear ( ) ;
156
140
m_AddedElements . Clear ( ) ;
157
141
}
158
142
159
143
void Update ( )
160
144
{
145
+ if ( ! m_IsVisible )
146
+ return ;
147
+
161
148
if ( State . IsPlayingOrPaused ( ) ) { UpdateClipFieldProgressBars ( ) ; }
162
149
else if ( ! m_ClipFieldProgressBarsAreCleared ) { ClearClipFieldProgressBars ( ) ; }
163
150
@@ -175,12 +162,6 @@ void Update()
175
162
}
176
163
}
177
164
178
- void OnBecameInvisible ( )
179
- {
180
- State . Stop ( ) ;
181
- ClearClipFieldProgressBars ( ) ;
182
- }
183
-
184
165
void SetTitle ( )
185
166
{
186
167
var titleString = "Audio Random Container" ;
@@ -222,6 +203,9 @@ void CreateGUI()
222
203
Assert . IsNotNull ( m_Day0RootVisualElement ) ;
223
204
}
224
205
206
+ if ( m_ContainerElementsInitialized )
207
+ root . Unbind ( ) ;
208
+
225
209
if ( State . AudioContainer == null )
226
210
{
227
211
if ( ! m_Day0ElementsInitialized )
@@ -235,15 +219,16 @@ void CreateGUI()
235
219
}
236
220
else
237
221
{
238
- if ( m_ContainerElementsInitialized )
239
- root . Unbind ( ) ;
240
- else
222
+ if ( ! m_ContainerElementsInitialized )
241
223
{
242
- InitializeElements ( ) ;
243
- SubscribeToCallbacksAndEvents ( ) ;
224
+ InitializeContainerElements ( ) ;
244
225
m_ContainerElementsInitialized = true ;
226
+ EditorApplication . update += OneTimeEditorApplicationUpdate ;
245
227
}
246
228
229
+ if ( ! m_IsSubscribedToGUICallbacksAndEvents )
230
+ SubscribeToGUICallbacksAndEvents ( ) ;
231
+
247
232
BindAndTrackObjectAndProperties ( ) ;
248
233
249
234
m_Day0RootVisualElement . style . display = DisplayStyle . None ;
@@ -257,6 +242,15 @@ void CreateGUI()
257
242
}
258
243
}
259
244
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
+
260
254
void InitializeDay0Elements ( )
261
255
{
262
256
var createButtonLabel = UIToolkitUtilities . GetChildByName < Label > ( m_Day0RootVisualElement , "CreateButtonLabel" ) ;
@@ -265,7 +259,7 @@ void InitializeDay0Elements()
265
259
createButtonLabel . text = "Select an existing Audio Random Container asset in the project browser or create a new one using the button below." ;
266
260
}
267
261
268
- void InitializeElements ( )
262
+ void InitializeContainerElements ( )
269
263
{
270
264
InitializePreviewElements ( ) ;
271
265
InitializeVolumeElements ( ) ;
@@ -275,6 +269,26 @@ void InitializeElements()
275
269
InitializeAutomaticTriggerElements ( ) ;
276
270
}
277
271
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
+
278
292
void BindAndTrackObjectAndProperties ( )
279
293
{
280
294
m_ContainerRootVisualElement . TrackSerializedObjectValue ( State . SerializedObject , OnSerializedObjectChanged ) ;
@@ -287,23 +301,17 @@ void BindAndTrackObjectAndProperties()
287
301
BindAndTrackAutomaticTriggerProperties ( ) ;
288
302
}
289
303
290
- void SubscribeToCallbacksAndEvents ( )
291
- {
292
- SubscribeToPreviewCallbacksAndEvents ( ) ;
293
- SubscribeToVolumeCallbacksAndEvents ( ) ;
294
- SubscribeToPitchCallbacksAndEvents ( ) ;
295
- SubscribeToClipListCallbacksAndEvents ( ) ;
296
- SubscribeToAutomaticTriggerCallbacksAndEvents ( ) ;
297
- EditorApplication . update += OneTimeEditorApplicationUpdate ;
298
- }
299
-
300
304
void OnTargetChanged ( object sender , EventArgs e )
301
305
{
302
306
SetTitle ( ) ;
303
307
CreateGUI ( ) ;
304
308
305
- if ( State . AudioContainer != null )
309
+ if ( State . AudioContainer == null )
310
+ m_CachedElements . Clear ( ) ;
311
+ else
306
312
m_CachedElements = State . AudioContainer . elements . ToList ( ) ;
313
+
314
+ m_AddedElements . Clear ( ) ;
307
315
}
308
316
309
317
void OnSerializedObjectChanged ( SerializedObject obj )
@@ -1196,15 +1204,49 @@ void OnCountRandomizationButtonClicked()
1196
1204
1197
1205
#region GlobalEditorCallbackHandlers
1198
1206
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
+
1199
1242
void OnWillSaveAssets ( IEnumerable < string > paths )
1200
1243
{
1244
+ // If there is no target we are in day 0 state.
1201
1245
if ( State . AudioContainer == null )
1202
1246
return ;
1203
1247
1204
- var currentSelectionPath = AssetDatabase . GetAssetPath ( State . AudioContainer ) ;
1205
-
1206
1248
foreach ( var path in paths )
1207
- if ( path == currentSelectionPath )
1249
+ if ( path == State . TargetPath )
1208
1250
{
1209
1251
SetTitle ( ) ;
1210
1252
return ;
@@ -1213,27 +1255,32 @@ void OnWillSaveAssets(IEnumerable<string> paths)
1213
1255
1214
1256
void OnAssetsImported ( IEnumerable < string > paths )
1215
1257
{
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 ;
1217
1261
1218
1262
foreach ( var path in paths )
1219
- if ( AssetDatabase . GetMainAssetTypeAtPath ( path ) == typeof ( AudioRandomContainer ) &&
1220
- AssetDatabase . GetMainAssetInstanceID ( path ) == State . AudioContainer . GetInstanceID ( ) )
1263
+ if ( path == State . TargetPath )
1221
1264
{
1222
1265
State . SerializedObject . Update ( ) ;
1223
1266
OnTargetChanged ( this , EventArgs . Empty ) ;
1267
+ return ;
1224
1268
}
1225
1269
}
1226
1270
1227
1271
void OnAssetsDeleted ( IEnumerable < string > paths )
1228
1272
{
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.
1229
1278
foreach ( var path in paths )
1230
1279
if ( path == State . TargetPath )
1231
1280
{
1232
1281
State . Reset ( ) ;
1233
- SetTitle ( ) ;
1234
- CreateGUI ( ) ;
1235
- m_CachedElements . Clear ( ) ;
1236
- break ;
1282
+ OnTargetChanged ( this , EventArgs . Empty ) ;
1283
+ return ;
1237
1284
}
1238
1285
}
1239
1286
0 commit comments