1+ #if UNITY_6000_3_OR_NEWER
2+ using System . IO ;
3+ using UnityEditor ;
4+ using UnityEditor . SceneManagement ;
5+ using UnityEditor . Toolbars ;
6+ using UnityEngine ;
7+ using UnityEngine . SceneManagement ;
8+
9+ namespace UnityEssentialsEditor
10+ {
11+ public class SceneSelectionToolbarDropdown
12+ {
13+ private const string MENU_PATH = "Editor Controls/Scene Selector" ;
14+ private static string [ ] scenePaths ;
15+
16+ [ MainToolbarElement ( MENU_PATH , defaultDockPosition = MainToolbarDockPosition . Middle ) ]
17+ public static MainToolbarElement CreateSceneSelectorDropdown ( )
18+ {
19+ var icon = EditorGUIUtility . IconContent ( "UnityLogo" ) . image as Texture2D ;
20+ var content = new MainToolbarContent ( "Scenes" , icon , "Select active scene" ) ;
21+ var dropdown = new MainToolbarDropdown ( content , ShowDropdownMenu ) ;
22+ return dropdown ;
23+ }
24+
25+ static void ShowDropdownMenu ( Rect dropDownRect )
26+ {
27+ var menu = new GenericMenu ( ) ;
28+ if ( scenePaths . Length == 0 )
29+ {
30+ menu . AddDisabledItem ( new GUIContent ( "No Scenes in Project" ) ) ;
31+ }
32+ if ( EditorBuildSettings . scenes . Length > 0 )
33+ {
34+ for ( var i = 0 ; i < EditorBuildSettings . scenes . Length ; i ++ )
35+ {
36+ var scene = EditorBuildSettings . scenes [ i ] ;
37+ string sceneName = Path . GetFileNameWithoutExtension ( scene . path ) ;
38+ menu . AddItem ( new GUIContent ( "Build Scenes/ " + sceneName ) , false , ( ) => { SwitchScene ( scene . path ) ; } ) ;
39+ }
40+ menu . AddSeparator ( "" ) ;
41+ }
42+ foreach ( string scenePath in scenePaths )
43+ {
44+ string sceneName = Path . GetFileNameWithoutExtension ( scenePath ) ;
45+ menu . AddItem ( new GUIContent ( sceneName ) , false , ( ) => { SwitchScene ( scenePath ) ; } ) ;
46+ }
47+ menu . DropDown ( dropDownRect ) ;
48+ }
49+
50+ static void SwitchScene ( string scenePath )
51+ {
52+ if ( Application . isPlaying )
53+ {
54+ string sceneName = Path . GetFileNameWithoutExtension ( scenePath ) ;
55+ if ( Application . CanStreamedLevelBeLoaded ( sceneName ) )
56+ {
57+ Debug . Log ( $ "Switching to scene: { sceneName } ") ;
58+ SceneManager . LoadScene ( sceneName ) ;
59+ }
60+ else
61+ {
62+ Debug . LogError ( $ "Scene '{ sceneName } ' is not in the Build Settings.") ;
63+ }
64+ }
65+ else
66+ {
67+ if ( File . Exists ( scenePath ) )
68+ {
69+ if ( EditorSceneManager . SaveCurrentModifiedScenesIfUserWantsTo ( ) )
70+ {
71+ EditorSceneManager . OpenScene ( scenePath ) ;
72+ }
73+ }
74+ else
75+ {
76+ Debug . LogError ( $ "Scene at path '{ scenePath } ' does not exist.") ;
77+ }
78+ }
79+ }
80+
81+ static void RefreshSceneList ( )
82+ {
83+ scenePaths = Directory . GetFiles ( "Assets" , "*.unity" , SearchOption . AllDirectories ) ;
84+ }
85+
86+ static void SceneSwitched ( Scene oldScene , Scene newScene )
87+ {
88+ MainToolbar . Refresh ( MENU_PATH ) ;
89+ }
90+
91+ static SceneSelectionToolbarDropdown ( )
92+ {
93+ RefreshSceneList ( ) ;
94+ EditorApplication . projectChanged += RefreshSceneList ;
95+ SceneManager . activeSceneChanged += SceneSwitched ;
96+ EditorSceneManager . activeSceneChangedInEditMode += SceneSwitched ;
97+ }
98+ }
99+ }
100+ #endif
0 commit comments