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:
This source diff could not be displayed because it is too large. You can view the blob instead.
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
fileFormatVersion: 2
guid: 7d3de6a23d22fe64e815ebd43b20b2b7
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
/// <summary>
/// A ScriptableObject that groups scenes you frequently open together.
/// Create via: Assets > Create > Editor Tools > Scene Collection
/// </summary>
[CreateAssetMenu(fileName = "NewSceneCollection", menuName = "Editor Tools/Scene Collection")]
public class EditorSceneCollection : ScriptableObject
{
// ─── Inspector Fields ────────────────────────────────────────────────────
[Tooltip("Label shown in the toolbar menu. Falls back to the asset name if left empty.")]
[SerializeField] private string collectionName;
[Tooltip("Optional note shown as a disabled hint at the top of the menu.")]
[SerializeField] private string description;
[Tooltip("If enabled, this collection's scenes appear in the Main Scenes toolbar menu.\nOnly one collection should have this checked at a time.")]
[SerializeField] private bool showInMainMenu;
[Tooltip("Scenes in this collection. The first scene opens Single; the rest open Additive.")]
[SerializeField] private SceneAsset[] sceneAssets;
// ─── Public API ──────────────────────────────────────────────────────────
/// <summary>Display name used in the toolbar menu.</summary>
public string CollectionName =>
string.IsNullOrWhiteSpace(collectionName) ? name : collectionName;
/// <summary>Optional description shown as a disabled hint in the menu.</summary>
public string Description => description;
/// <summary>Whether this collection feeds the Main Scenes toolbar menu.</summary>
public bool ShowInMainMenu => showInMainMenu;
/// <summary>Read-only access to the scene assets in this collection.</summary>
public SceneAsset[] SceneAssets => sceneAssets;
/// <summary>
/// Opens all scenes in the collection.
/// The first scene replaces the current setup; the rest load additively.
/// Prompts the user to save any unsaved changes first.
/// </summary>
public void OpenAll()
{
if (sceneAssets == null || sceneAssets.Length == 0)
{
Debug.LogWarning($"[SceneCollection] '{CollectionName}' has no scenes assigned.");
return;
}
if (!EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo())
return;
for (int i = 0; i < sceneAssets.Length; i++)
{
if (sceneAssets[i] == null) continue;
string path = AssetDatabase.GetAssetPath(sceneAssets[i]);
var mode = (i == 0) ? OpenSceneMode.Single : OpenSceneMode.Additive;
EditorSceneManager.OpenScene(path, mode);
}
}
}
\ No newline at end of file
fileFormatVersion: 2
guid: 9d12c215cf29fa446abc76913d213425
\ No newline at end of file
m_EditorVersion: 6000.3.8f1
m_EditorVersionWithRevision: 6000.3.8f1 (1c7db571dde0)
m_EditorVersion: 6000.3.9f1
m_EditorVersionWithRevision: 6000.3.9f1 (7a9955a4f2fa)
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