@@ -20,6 +20,15 @@ namespace UnityEditor;
20
20
21
21
sealed class AudioContainerWindow : EditorWindow
22
22
{
23
+ enum Icons
24
+ {
25
+ Play = 0 ,
26
+ Stop = 1 ,
27
+ Skip = 2 ,
28
+ DiceOff = 3 ,
29
+ DiceOn = 4
30
+ }
31
+
23
32
/// <summary>
24
33
/// The cached instance of the window, if it is open.
25
34
/// </summary>
@@ -32,8 +41,8 @@ sealed class AudioContainerWindow : EditorWindow
32
41
/// Only used locally in these methods, but it's a global member to avoid GC.
33
42
/// </summary>
34
43
readonly List < AudioContainerElement > m_AddedElements = new ( ) ;
35
-
36
44
readonly string k_EmptyGuidString = Guid . Empty . ToString ( "N" ) ;
45
+ readonly Texture2D [ ] k_IconTextureCache = new Texture2D [ Enum . GetNames ( typeof ( Icons ) ) . Length ] ;
37
46
38
47
VisualElement m_ContainerRootVisualElement ;
39
48
VisualElement m_Day0RootVisualElement ;
@@ -91,15 +100,11 @@ sealed class AudioContainerWindow : EditorWindow
91
100
Label m_AutomaticTriggerModeLabel ;
92
101
Label m_LoopLabel ;
93
102
94
- // Shared icon references
95
- Texture2D m_DiceIconOff ;
96
- Texture2D m_DiceIconOn ;
97
-
98
103
bool m_IsVisible ;
99
- bool m_IsSubscribedToGUICallbacksAndEvents ;
100
104
bool m_IsInitializing ;
101
105
bool m_Day0ElementsInitialized ;
102
106
bool m_ContainerElementsInitialized ;
107
+ bool m_IsSubscribedToGUICallbacksAndEvents ;
103
108
bool m_ClipFieldProgressBarsAreCleared = true ;
104
109
105
110
/// <summary>
@@ -114,6 +119,11 @@ internal static void CreateAudioRandomContainerWindow()
114
119
window . Show ( ) ;
115
120
}
116
121
122
+ internal bool IsInitializedForTargetDisplay ( )
123
+ {
124
+ return m_ContainerElementsInitialized && m_IsSubscribedToGUICallbacksAndEvents ;
125
+ }
126
+
117
127
static void OnCreateButtonClicked ( )
118
128
{
119
129
ProjectWindowUtil . CreateAudioRandomContainer ( ) ;
@@ -126,8 +136,6 @@ void OnEnable()
126
136
Instance = this ;
127
137
}
128
138
129
- m_DiceIconOff = EditorGUIUtility . IconContent ( "AudioRandomContainer On Icon" ) . image as Texture2D ;
130
- m_DiceIconOn = EditorGUIUtility . IconContent ( "AudioRandomContainer Icon" ) . image as Texture2D ;
131
139
SetTitle ( ) ;
132
140
}
133
141
@@ -186,7 +194,7 @@ void SetTitle()
186
194
187
195
titleContent = new GUIContent ( titleString )
188
196
{
189
- image = m_DiceIconOff
197
+ image = GetIconTexture ( Icons . DiceOff )
190
198
} ;
191
199
}
192
200
@@ -378,9 +386,7 @@ void InitializePreviewElements()
378
386
m_PlayStopButtonImage = UIToolkitUtilities . GetChildByName < VisualElement > ( m_ContainerRootVisualElement , "play-button-image" ) ;
379
387
m_SkipButton = UIToolkitUtilities . GetChildByName < Button > ( m_ContainerRootVisualElement , "skip-button" ) ;
380
388
m_SkipButtonImage = UIToolkitUtilities . GetChildByName < VisualElement > ( m_ContainerRootVisualElement , "skip-button-image" ) ;
381
-
382
- var skipIcon = UIToolkitUtilities . LoadIcon ( "Skip" ) ;
383
- m_SkipButtonImage . style . backgroundImage = new StyleBackground ( skipIcon ) ;
389
+ m_SkipButtonImage . style . backgroundImage = GetIconTexture ( Icons . Skip ) ;
384
390
}
385
391
386
392
void SubscribeToPreviewCallbacksAndEvents ( )
@@ -429,13 +435,27 @@ void UpdateTransportButtonStates()
429
435
430
436
m_PlayStopButton ? . SetEnabled ( State . IsReadyToPlay ( ) && ! editorIsPaused && ! EditorUtility . audioMasterMute ) ;
431
437
m_SkipButton ? . SetEnabled ( State . IsPlayingOrPaused ( ) && State . AudioContainer . triggerMode == AudioRandomContainerTriggerMode . Automatic && ! editorIsPaused && ! EditorUtility . audioMasterMute ) ;
438
+ m_PlayStopButtonImage . style . backgroundImage = State . IsPlayingOrPaused ( ) ? GetIconTexture ( Icons . Stop ) : GetIconTexture ( Icons . Play ) ;
439
+ }
432
440
433
- var image =
434
- State . IsPlayingOrPaused ( )
435
- ? UIToolkitUtilities . LoadIcon ( "Stop" )
436
- : UIToolkitUtilities . LoadIcon ( "Play" ) ;
441
+ Texture2D GetIconTexture ( Icons icon )
442
+ {
443
+ var cacheIndex = ( int ) icon ;
444
+
445
+ var name = icon switch
446
+ {
447
+ Icons . Play or Icons . Stop or Icons . Skip => icon . ToString ( ) ,
448
+ Icons . DiceOff => "AudioRandomContainer On Icon" ,
449
+ Icons . DiceOn => "AudioRandomContainer Icon" ,
450
+ _ => throw new ArgumentOutOfRangeException ( nameof ( icon ) , icon , null )
451
+ } ;
452
+
453
+ if ( k_IconTextureCache [ cacheIndex ] == null )
454
+ {
455
+ k_IconTextureCache [ cacheIndex ] = EditorGUIUtility . IconContent ( name ) . image as Texture2D ;
456
+ }
437
457
438
- m_PlayStopButtonImage . style . backgroundImage = new StyleBackground ( image ) ;
458
+ return k_IconTextureCache [ cacheIndex ] ;
439
459
}
440
460
441
461
void OnTransportStateChanged ( object sender , EventArgs e )
@@ -535,14 +555,14 @@ void OnVolumeRandomizationEnabledChanged(SerializedProperty property)
535
555
{
536
556
if ( property . boolValue )
537
557
{
538
- m_VolumeRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOn ) ;
558
+ m_VolumeRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOn ) ;
539
559
m_VolumeRandomizationRangeSlider . SetEnabled ( true ) ;
540
560
m_VolumeRandomizationRangeField . SetEnabled ( true ) ;
541
561
m_VolumeRandomRangeTracker . SetEnabled ( true ) ;
542
562
}
543
563
else
544
564
{
545
- m_VolumeRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOff ) ;
565
+ m_VolumeRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOff ) ;
546
566
m_VolumeRandomizationRangeSlider . SetEnabled ( false ) ;
547
567
m_VolumeRandomizationRangeField . SetEnabled ( false ) ;
548
568
m_VolumeRandomRangeTracker . SetEnabled ( false ) ;
@@ -642,14 +662,14 @@ void OnPitchRandomizationEnabledChanged(SerializedProperty property)
642
662
{
643
663
if ( property . boolValue )
644
664
{
645
- m_PitchRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOn ) ;
665
+ m_PitchRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOn ) ;
646
666
m_PitchRandomizationRangeSlider . SetEnabled ( true ) ;
647
667
m_PitchRandomizationRangeField . SetEnabled ( true ) ;
648
668
m_PitchRandomRangeTracker . SetEnabled ( true ) ;
649
669
}
650
670
else
651
671
{
652
- m_PitchRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOff ) ;
672
+ m_PitchRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOff ) ;
653
673
m_PitchRandomizationRangeSlider . SetEnabled ( false ) ;
654
674
m_PitchRandomizationRangeField . SetEnabled ( false ) ;
655
675
m_PitchRandomRangeTracker . SetEnabled ( false ) ;
@@ -1227,14 +1247,14 @@ void OnTimeRandomizationEnabledChanged(SerializedProperty property)
1227
1247
if ( property . boolValue
1228
1248
&& State . AudioContainer . triggerMode == AudioRandomContainerTriggerMode . Automatic )
1229
1249
{
1230
- m_TimeRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOn ) ;
1250
+ m_TimeRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOn ) ;
1231
1251
m_TimeRandomizationRangeSlider . SetEnabled ( true ) ;
1232
1252
m_TimeRandomizationRangeField . SetEnabled ( true ) ;
1233
1253
m_TimeRandomRangeTracker . SetEnabled ( true ) ;
1234
1254
}
1235
1255
else
1236
1256
{
1237
- m_TimeRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOff ) ;
1257
+ m_TimeRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOff ) ;
1238
1258
m_TimeRandomizationRangeSlider . SetEnabled ( false ) ;
1239
1259
m_TimeRandomizationRangeField . SetEnabled ( false ) ;
1240
1260
m_TimeRandomRangeTracker . SetEnabled ( false ) ;
@@ -1267,13 +1287,13 @@ void OnCountRandomizationEnabledChanged(SerializedProperty property)
1267
1287
&& State . AudioContainer . loopMode != AudioRandomContainerLoopMode . Infinite
1268
1288
&& State . AudioContainer . triggerMode == AudioRandomContainerTriggerMode . Automatic )
1269
1289
{
1270
- m_CountRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOn ) ;
1290
+ m_CountRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOn ) ;
1271
1291
m_CountRandomizationRangeSlider . SetEnabled ( true ) ;
1272
1292
m_CountRandomizationRangeField . SetEnabled ( true ) ;
1273
1293
}
1274
1294
else
1275
1295
{
1276
- m_CountRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOff ) ;
1296
+ m_CountRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOff ) ;
1277
1297
m_CountRandomizationRangeSlider . SetEnabled ( false ) ;
1278
1298
m_CountRandomizationRangeField . SetEnabled ( false ) ;
1279
1299
}
0 commit comments