Commit f8d17eae authored by saad's avatar saad

add scene toolbar tool

parent ad185f7b
fileFormatVersion: 2
guid: aa7b3186c161cb146a4d7e1ef3adf89c
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: e7ce1812f530d4b4ebb33e3f2bc608b4
PrefabImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: f221bf2bdc917c146a8049f4dbdfc2ba
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
%YAML 1.1
%TAG !u! tag:unity3d.com,2011:
--- !u!114 &11400000
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 0}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 9d12c215cf29fa446abc76913d213425, type: 3}
m_Name: NewSceneCollection
m_EditorClassIdentifier: Assembly-CSharp::EditorSceneCollection
collectionName: MainScenes
description:
showInMainMenu: 1
sceneAssets:
- {fileID: 102900000, guid: 70e093a0ef85a3e418cc77c732bc9e48, type: 3}
- {fileID: 102900000, guid: c2f60049cef0f6b44b9445afcf550aff, type: 3}
- {fileID: 102900000, guid: 89a5ca01ddc649a498cda8dc02cf28e3, type: 3}
fileFormatVersion: 2
guid: c3433eacd2c659e41b5f17a3d10876ef
NativeFormatImporter:
externalObjects: {}
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:
fileFormatVersion: 2
guid: 852beafb6193f3b428c62d435119ed37
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEditor;
using UnityEditor.Toolbars;
using UnityEditor.SceneManagement;
using UnityEngine;
/// <summary>
/// Adds a "Scenes" button to the Unity main toolbar.
/// Clicking it opens a flat "Main Scenes" menu populated from whichever
/// EditorSceneCollection has "Show In Main Menu" enabled.
/// </summary>
public static class EditorToolbarSceneButton
{
// ─── Toolbar Registration ────────────────────────────────────────────────
[MainToolbarElement("Scenes Button", defaultDockPosition = MainToolbarDockPosition.Right)]
public static MainToolbarElement GetScenesButton()
{
Texture2D icon = EditorGUIUtility.IconContent("SceneAsset Icon").image as Texture2D;
return new MainToolbarButton(
new MainToolbarContent("Scenes", icon, "Open a scene from Main Scenes."),
OnOpenScenesMenu
);
}
// ─── Menu Construction ───────────────────────────────────────────────────
private static void OnOpenScenesMenu()
{
GenericMenu menu = new GenericMenu();
EditorSceneCollection mainCollection = FindMainCollection();
if (mainCollection == null)
{
menu.AddDisabledItem(new GUIContent("No collection has 'Show In Main Menu' enabled."));
menu.ShowAsContext();
return;
}
// ── Header ──────────────────────────────────────────────────────────
menu.AddDisabledItem(new GUIContent("Main Scenes"));
menu.AddSeparator("");
// ── Optional description ─────────────────────────────────────────────
if (!string.IsNullOrWhiteSpace(mainCollection.Description))
{
menu.AddDisabledItem(new GUIContent(mainCollection.Description));
menu.AddSeparator("");
}
// ── Individual scenes ────────────────────────────────────────────────
bool anyScenesAdded = false;
if (mainCollection.SceneAssets != null)
{
foreach (SceneAsset scene in mainCollection.SceneAssets)
{
if (scene == null) continue;
string scenePath = AssetDatabase.GetAssetPath(scene);
string sceneName = scene.name;
menu.AddItem(new GUIContent(sceneName), false, () => OpenScene(scenePath));
anyScenesAdded = true;
}
}
if (!anyScenesAdded)
menu.AddDisabledItem(new GUIContent("No scenes assigned to this collection."));
// ── Open All shortcut ────────────────────────────────────────────────
menu.AddSeparator("");
menu.AddItem(new GUIContent("Open All"), false, mainCollection.OpenAll);
menu.ShowAsContext();
}
// ─── Helpers ─────────────────────────────────────────────────────────────
/// <summary>
/// Finds the first EditorSceneCollection with ShowInMainMenu enabled.
/// Logs a warning if more than one is found.
/// </summary>
private static EditorSceneCollection FindMainCollection()
{
string[] guids = AssetDatabase.FindAssets("t:EditorSceneCollection");
EditorSceneCollection result = null;
int mainCount = 0;
foreach (string guid in guids)
{
string path = AssetDatabase.GUIDToAssetPath(guid);
var collection = AssetDatabase.LoadAssetAtPath<EditorSceneCollection>(path);
if (collection == null || !collection.ShowInMainMenu) continue;
result ??= collection;
mainCount++;
}
if (mainCount > 1)
Debug.LogWarning("[EditorToolbarSceneButton] More than one EditorSceneCollection has 'Show In Main Menu' enabled. Only the first one found will be used.");
return result;
}
/// <summary>
/// Opens a single scene. Prompts the user to save unsaved changes first.
/// </summary>
private static void OpenScene(string path)
{
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
EditorSceneManager.OpenScene(path, OpenSceneMode.Single);
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: d4dab859724741b4dae4a37ec651b647
\ No newline at end of file
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment