Skip to content

Instantly share code, notes, and snippets.

@nir1082
Created August 23, 2017 09:34
Show Gist options
  • Save nir1082/9c79cea8ae2b73ded3babe845af466a8 to your computer and use it in GitHub Desktop.
Save nir1082/9c79cea8ae2b73ded3babe845af466a8 to your computer and use it in GitHub Desktop.
using System.Linq;
using UnityEngine;
using UnityEditor;
using UnityEngine.SceneManagement;
using UnityEditor.SceneManagement;
public class LoadSceneWindow : EditorWindow
{
[MenuItem ("Custom/Scene")]
static public void Open ()
{
LoadSceneWindow window = GetWindow<LoadSceneWindow> (false);
window.minSize = new Vector2 (200.0f, 22.5f);
window.maxSize = new Vector2 (400.0f, 22.5f);
}
SceneAsset[] m_Scenes;
SceneAsset m_CurrentScene;
bool m_WasInitialized = false;
void Initialize ()
{
var sceneGUIDs = AssetDatabase.FindAssets ("t:scene");
m_Scenes = sceneGUIDs.Select (arg => AssetDatabase.LoadAssetAtPath<SceneAsset> (AssetDatabase.GUIDToAssetPath (arg))).ToArray ();
m_CurrentScene = m_Scenes.Where (arg => arg.name.Equals (SceneManager.GetActiveScene ().name)).FirstOrDefault ();
m_WasInitialized = true;
}
void OnGUI ()
{
if (!m_WasInitialized) {
Initialize ();
return;
}
EditorGUI.BeginDisabledGroup (EditorApplication.isPlayingOrWillChangePlaymode);
EditorGUILayout.BeginHorizontal ();
var prevIdx = ArrayUtility.IndexOf <SceneAsset> (m_Scenes, m_CurrentScene);
var nextIdx = EditorGUILayout.Popup (prevIdx, m_Scenes.Select (arg => arg.name).ToArray ());
if (prevIdx != nextIdx) {
if (EditorSceneManager.SaveCurrentModifiedScenesIfUserWantsTo ()) {
m_CurrentScene = m_Scenes [nextIdx];
EditorSceneManager.OpenScene (AssetDatabase.GetAssetPath (m_CurrentScene));
}
}
if (GUILayout.Button ("Select")) {
Selection.activeObject = m_CurrentScene;
}
EditorGUILayout.EndHorizontal ();
EditorGUI.EndDisabledGroup ();
}
void Update ()
{
if (!m_WasInitialized) {
Initialize ();
return;
}
if (m_CurrentScene != null && !m_CurrentScene.name.Equals (SceneManager.GetActiveScene ().name)) {
m_CurrentScene = AssetDatabase.LoadAssetAtPath<SceneAsset> (SceneManager.GetActiveScene ().path);
Repaint ();
}
}
void OnEnable ()
{
Initialize ();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment