Skip to content

Commit 70a5037

Browse files
committed
Main toolbar controls (unity 6.3 feature)
1 parent 9db79ce commit 70a5037

5 files changed

Lines changed: 153 additions & 0 deletions

File tree

Editor/MainToolbar.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#if UNITY_6000_3_OR_NEWER
2+
using System;
3+
using System.Reflection;
4+
using UnityEditor;
5+
using UnityEditor.Toolbars;
6+
using UnityEngine;
7+
8+
namespace UnityEssentialsEditor
9+
{
10+
public class ErrorPauseToolbarToggle
11+
{
12+
public static readonly Type consoleWindowType = typeof(EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow");
13+
14+
private const string MENU_PATH = "Editor Controls/Error Pause Toggle";
15+
16+
[MainToolbarElement(MENU_PATH, defaultDockPosition = MainToolbarDockPosition.Middle)]
17+
public static MainToolbarElement CreateSceneSelectorDropdown()
18+
{
19+
var icon = EditorGUIUtility.IconContent("d_console.erroricon.inactive.sml").image as Texture2D;
20+
var content = new MainToolbarContent("", icon, "Click to toggle error pause");
21+
var dropdown = new MainToolbarToggle(content, GetErrorPause(), SetErrorPause);
22+
return dropdown;
23+
}
24+
25+
private static bool GetErrorPause()
26+
{
27+
const BindingFlags PUBLIC_STATIC_FLAGS = BindingFlags.Static | BindingFlags.Public;
28+
var getMethod = consoleWindowType.GetMethod("GetConsoleErrorPause", PUBLIC_STATIC_FLAGS);
29+
return getMethod != null && (bool)getMethod.Invoke(null, null);
30+
}
31+
32+
private static void SetErrorPause(bool state)
33+
{
34+
const BindingFlags PUBLIC_STATIC_FLAGS = BindingFlags.Static | BindingFlags.Public;
35+
var setMethod = consoleWindowType.GetMethod("SetConsoleErrorPause", PUBLIC_STATIC_FLAGS);
36+
if (setMethod != null) setMethod.Invoke(null, new object[] { state });
37+
MainToolbar.Refresh(MENU_PATH);
38+
if (Resources.FindObjectsOfTypeAll(consoleWindowType).Length > 0)
39+
{
40+
EditorWindow.GetWindow(consoleWindowType, false, null, false)?.Repaint();
41+
}
42+
}
43+
}
44+
}
45+
#endif

Editor/MainToolbar/ErrorPauseToolbarToggle.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Editor/MainToolbar/SceneSelectionToolbarDropdown.cs.meta

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)