@@ -20,6 +20,15 @@ namespace UnityEditor;
2020
2121sealed class AudioContainerWindow : EditorWindow
2222{
23+ enum Icons
24+ {
25+ Play = 0 ,
26+ Stop = 1 ,
27+ Skip = 2 ,
28+ DiceOff = 3 ,
29+ DiceOn = 4
30+ }
31+
2332 /// <summary>
2433 /// The cached instance of the window, if it is open.
2534 /// </summary>
@@ -32,8 +41,8 @@ sealed class AudioContainerWindow : EditorWindow
3241 /// Only used locally in these methods, but it's a global member to avoid GC.
3342 /// </summary>
3443 readonly List < AudioContainerElement > m_AddedElements = new ( ) ;
35-
3644 readonly string k_EmptyGuidString = Guid . Empty . ToString ( "N" ) ;
45+ readonly Texture2D [ ] k_IconTextureCache = new Texture2D [ Enum . GetNames ( typeof ( Icons ) ) . Length ] ;
3746
3847 VisualElement m_ContainerRootVisualElement ;
3948 VisualElement m_Day0RootVisualElement ;
@@ -91,15 +100,11 @@ sealed class AudioContainerWindow : EditorWindow
91100 Label m_AutomaticTriggerModeLabel ;
92101 Label m_LoopLabel ;
93102
94- // Shared icon references
95- Texture2D m_DiceIconOff ;
96- Texture2D m_DiceIconOn ;
97-
98103 bool m_IsVisible ;
99- bool m_IsSubscribedToGUICallbacksAndEvents ;
100104 bool m_IsInitializing ;
101105 bool m_Day0ElementsInitialized ;
102106 bool m_ContainerElementsInitialized ;
107+ bool m_IsSubscribedToGUICallbacksAndEvents ;
103108 bool m_ClipFieldProgressBarsAreCleared = true ;
104109
105110 /// <summary>
@@ -114,6 +119,11 @@ internal static void CreateAudioRandomContainerWindow()
114119 window . Show ( ) ;
115120 }
116121
122+ internal bool IsInitializedForTargetDisplay ( )
123+ {
124+ return m_ContainerElementsInitialized && m_IsSubscribedToGUICallbacksAndEvents ;
125+ }
126+
117127 static void OnCreateButtonClicked ( )
118128 {
119129 ProjectWindowUtil . CreateAudioRandomContainer ( ) ;
@@ -126,8 +136,6 @@ void OnEnable()
126136 Instance = this ;
127137 }
128138
129- m_DiceIconOff = EditorGUIUtility . IconContent ( "AudioRandomContainer On Icon" ) . image as Texture2D ;
130- m_DiceIconOn = EditorGUIUtility . IconContent ( "AudioRandomContainer Icon" ) . image as Texture2D ;
131139 SetTitle ( ) ;
132140 }
133141
@@ -186,7 +194,7 @@ void SetTitle()
186194
187195 titleContent = new GUIContent ( titleString )
188196 {
189- image = m_DiceIconOff
197+ image = GetIconTexture ( Icons . DiceOff )
190198 } ;
191199 }
192200
@@ -378,9 +386,7 @@ void InitializePreviewElements()
378386 m_PlayStopButtonImage = UIToolkitUtilities . GetChildByName < VisualElement > ( m_ContainerRootVisualElement , "play-button-image" ) ;
379387 m_SkipButton = UIToolkitUtilities . GetChildByName < Button > ( m_ContainerRootVisualElement , "skip-button" ) ;
380388 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 ) ;
384390 }
385391
386392 void SubscribeToPreviewCallbacksAndEvents ( )
@@ -429,13 +435,27 @@ void UpdateTransportButtonStates()
429435
430436 m_PlayStopButton ? . SetEnabled ( State . IsReadyToPlay ( ) && ! editorIsPaused && ! EditorUtility . audioMasterMute ) ;
431437 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+ }
432440
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+ }
437457
438- m_PlayStopButtonImage . style . backgroundImage = new StyleBackground ( image ) ;
458+ return k_IconTextureCache [ cacheIndex ] ;
439459 }
440460
441461 void OnTransportStateChanged ( object sender , EventArgs e )
@@ -535,14 +555,14 @@ void OnVolumeRandomizationEnabledChanged(SerializedProperty property)
535555 {
536556 if ( property . boolValue )
537557 {
538- m_VolumeRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOn ) ;
558+ m_VolumeRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOn ) ;
539559 m_VolumeRandomizationRangeSlider . SetEnabled ( true ) ;
540560 m_VolumeRandomizationRangeField . SetEnabled ( true ) ;
541561 m_VolumeRandomRangeTracker . SetEnabled ( true ) ;
542562 }
543563 else
544564 {
545- m_VolumeRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOff ) ;
565+ m_VolumeRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOff ) ;
546566 m_VolumeRandomizationRangeSlider . SetEnabled ( false ) ;
547567 m_VolumeRandomizationRangeField . SetEnabled ( false ) ;
548568 m_VolumeRandomRangeTracker . SetEnabled ( false ) ;
@@ -642,14 +662,14 @@ void OnPitchRandomizationEnabledChanged(SerializedProperty property)
642662 {
643663 if ( property . boolValue )
644664 {
645- m_PitchRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOn ) ;
665+ m_PitchRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOn ) ;
646666 m_PitchRandomizationRangeSlider . SetEnabled ( true ) ;
647667 m_PitchRandomizationRangeField . SetEnabled ( true ) ;
648668 m_PitchRandomRangeTracker . SetEnabled ( true ) ;
649669 }
650670 else
651671 {
652- m_PitchRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOff ) ;
672+ m_PitchRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOff ) ;
653673 m_PitchRandomizationRangeSlider . SetEnabled ( false ) ;
654674 m_PitchRandomizationRangeField . SetEnabled ( false ) ;
655675 m_PitchRandomRangeTracker . SetEnabled ( false ) ;
@@ -1227,14 +1247,14 @@ void OnTimeRandomizationEnabledChanged(SerializedProperty property)
12271247 if ( property . boolValue
12281248 && State . AudioContainer . triggerMode == AudioRandomContainerTriggerMode . Automatic )
12291249 {
1230- m_TimeRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOn ) ;
1250+ m_TimeRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOn ) ;
12311251 m_TimeRandomizationRangeSlider . SetEnabled ( true ) ;
12321252 m_TimeRandomizationRangeField . SetEnabled ( true ) ;
12331253 m_TimeRandomRangeTracker . SetEnabled ( true ) ;
12341254 }
12351255 else
12361256 {
1237- m_TimeRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOff ) ;
1257+ m_TimeRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOff ) ;
12381258 m_TimeRandomizationRangeSlider . SetEnabled ( false ) ;
12391259 m_TimeRandomizationRangeField . SetEnabled ( false ) ;
12401260 m_TimeRandomRangeTracker . SetEnabled ( false ) ;
@@ -1267,13 +1287,13 @@ void OnCountRandomizationEnabledChanged(SerializedProperty property)
12671287 && State . AudioContainer . loopMode != AudioRandomContainerLoopMode . Infinite
12681288 && State . AudioContainer . triggerMode == AudioRandomContainerTriggerMode . Automatic )
12691289 {
1270- m_CountRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOn ) ;
1290+ m_CountRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOn ) ;
12711291 m_CountRandomizationRangeSlider . SetEnabled ( true ) ;
12721292 m_CountRandomizationRangeField . SetEnabled ( true ) ;
12731293 }
12741294 else
12751295 {
1276- m_CountRandomizationButtonImage . style . backgroundImage = new StyleBackground ( m_DiceIconOff ) ;
1296+ m_CountRandomizationButtonImage . style . backgroundImage = GetIconTexture ( Icons . DiceOff ) ;
12771297 m_CountRandomizationRangeSlider . SetEnabled ( false ) ;
12781298 m_CountRandomizationRangeField . SetEnabled ( false ) ;
12791299 }
0 commit comments