Skip to content

Instantly share code, notes, and snippets.

@tyfkda
Last active July 15, 2021 12:36
Show Gist options
  • Save tyfkda/6fd2a4de6de7d3ea3f2766c705eae61b to your computer and use it in GitHub Desktop.
Save tyfkda/6fd2a4de6de7d3ea3f2766c705eae61b to your computer and use it in GitHub Desktop.
SceneSwitcher for Unity
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditor.SceneManagement;
using System.IO;
public class SceneSwitcherWindow : EditorWindow {
private List<SceneAsset> scenes;
private Vector2 scrollPos = Vector2.zero;
[MenuItem("Tools/Foo/Scene Switcher")]
static void Open() {
GetWindow<SceneSwitcherWindow>("SceneSwitcher");
}
private void OnEnable() {
if (scenes == null) {
scenes = new List<SceneAsset>();
Load();
}
}
private static string FilePath => $"{Application.persistentDataPath}/_sceneLauncher.sav";
private void OnGUI() {
EditorGUILayout.BeginHorizontal();
var sceneAsset = EditorGUILayout.ObjectField(null, typeof(SceneAsset), false) as SceneAsset;
if (sceneAsset != null && scenes.IndexOf(sceneAsset) < 0) {
scenes.Add(sceneAsset);
Save();
}
if (GUILayout.Button("Add current scene")) {
var scene = EditorSceneManager.GetActiveScene();
if (scene != null && scene.path != null &&
scenes.Find(s => AssetDatabase.GetAssetPath(s) == scene.path) == null)
{
var asset = AssetDatabase.LoadAssetAtPath<SceneAsset>(scene.path);
if (asset != null && scenes.IndexOf(asset) < 0) {
scenes.Add(asset);
Save();
}
}
}
EditorGUILayout.EndHorizontal();
GuiLine();
this.scrollPos = EditorGUILayout.BeginScrollView(this.scrollPos);
for (var i = 0; i < scenes.Count; ++i) {
var scene = scenes[i];
EditorGUILayout.BeginHorizontal();
var path = AssetDatabase.GetAssetPath(scene);
if (GUILayout.Button("X", GUILayout.Width(20))) {
scenes.Remove(scene);
Save();
--i;
} else {
if (GUILayout.Button("O", GUILayout.Width(20))) {
EditorGUIUtility.PingObject(scene);
}
if (GUILayout.Button(i > 0 ? "↑" : " ", GUILayout.Width(20)) && i > 0) {
scenes[i] = scenes[i - 1];
scenes[i - 1] = scene;
Save();
}
EditorGUI.BeginDisabledGroup(EditorApplication.isPlaying);
if (GUILayout.Button(Path.GetFileNameWithoutExtension(path))) {
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo()) {
EditorSceneManager.OpenScene(path);
}
}
EditorGUI.EndDisabledGroup();
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndScrollView();
}
private void Save() {
var guids = new List<string>();
foreach (var scene in scenes) {
string guid;
long localId;
if (AssetDatabase.TryGetGUIDAndLocalFileIdentifier(scene, out guid, out localId)) {
guids.Add(guid);
}
}
var content = string.Join("\n", guids.ToArray());
File.WriteAllText(FilePath, content);
}
private void Load() {
scenes.Clear();
if (File.Exists(FilePath)) {
string content = File.ReadAllText(FilePath);
foreach (var guid in content.Split(new char[]{'\n'})) {
var path = AssetDatabase.GUIDToAssetPath(guid);
var scene = AssetDatabase.LoadAssetAtPath<SceneAsset>(path);
if (scene != null)
scenes.Add(scene);
}
}
}
private void GuiLine(int height = 1) {
GUILayout.Box("", new GUILayoutOption[]{GUILayout.ExpandWidth(true), GUILayout.Height(height)});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment