daily pastebin goal
19%
SHARE
TWEET

ScenelistWindow.cs

MaZy Nov 28th, 2015 10 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using System.Linq;
  4. using UnityEditor;
  5. using UnityEngine;
  6.  
  7. public struct SceneInfo
  8. {
  9.     public string FolderName;
  10.     public string SceneName;
  11.     public string ScenePath;
  12. }
  13.  
  14. public class ScenelistWindow : EditorWindow
  15. {
  16.     ScenenlistSerializeable instance;
  17.  
  18.     private Vector2 _scrollPosition;
  19.     private static readonly string RootFolder = "Assets";
  20.     private readonly List<SceneInfo> Scenes = new List<SceneInfo>();
  21.     List<string> OnlySceneNames = new List<string>();
  22.  
  23.     int QuickSwitch1SceneListID = 0;
  24.     int QuickSwitch2SceneListID = 0;
  25.     bool dropDownSceneList = false;
  26.  
  27.     string scriptFolderDataPath = "";
  28.  
  29.     [MenuItem("Window/ShowScenes")]
  30.     public static void Init()
  31.     {
  32.         GetWindow(typeof(ScenelistWindow), false, "Scenes");
  33.     }
  34.  
  35.     string m_ScriptFilePath;
  36.     string m_ScriptFolder;
  37.     string GetScriptAssetPath()
  38.     {
  39.         var tmpInstance = CreateInstance<ScenenlistSerializeable>();
  40.         MonoScript ms = MonoScript.FromScriptableObject(tmpInstance);
  41.         m_ScriptFilePath = AssetDatabase.GetAssetPath(ms);
  42.  
  43.         //FileInfo fi = new FileInfo(m_ScriptFilePath);
  44.         //m_ScriptFolder = fi.Directory.ToString();
  45.         //m_ScriptFolder.Replace('\\', '/');
  46.  
  47.         // Asset/.../Folder
  48.         return m_ScriptFilePath.Substring(0, m_ScriptFilePath.LastIndexOf("/"));
  49.     }
  50.  
  51.     void OnEnable()
  52.     {
  53.         LoadData();
  54.         SearchForScenes();
  55.     }
  56.  
  57.     void OnDisable()
  58.     {
  59.         SaveData();
  60.     }
  61.  
  62.     void LoadData()
  63.     {
  64.        
  65.         scriptFolderDataPath = GetScriptAssetPath() + "/ScenenlistData.asset";
  66.         instance = AssetDatabase.LoadAssetAtPath<ScenenlistSerializeable>(scriptFolderDataPath);
  67.         if(instance == null) {
  68.             instance = CreateInstance<ScenenlistSerializeable>();
  69.             EditorUtility.SetDirty(instance);
  70.             AssetDatabase.CreateAsset(instance, scriptFolderDataPath);
  71.             AssetDatabase.SaveAssets();
  72.             AssetDatabase.Refresh();
  73.         }
  74.         else {
  75.             QuickSwitch1SceneListID = instance.QuickSwitch1ID;
  76.             QuickSwitch2SceneListID = instance.QuickSwitch2ID;
  77.         }
  78.     }
  79.  
  80.     void SaveData()
  81.     {
  82.         if(instance) {
  83.             EditorUtility.SetDirty(instance);
  84.             instance.QuickSwitch1ID = QuickSwitch1SceneListID;
  85.             instance.QuickSwitch2ID = QuickSwitch2SceneListID;
  86.             AssetDatabase.SaveAssets();
  87.             AssetDatabase.Refresh();
  88.         }
  89.     }
  90.  
  91.     private void OnGUI()
  92.     {
  93.         if(GUILayout.Button("Research Scenes")) {
  94.             SearchForScenes();
  95.         }
  96.  
  97.         GUILayout.Space(10f);
  98.  
  99.         GUILayout.BeginHorizontal();
  100.         GUILayout.Label("Quick switch 1");
  101.         QuickSwitch1SceneListID = EditorGUILayout.Popup(QuickSwitch1SceneListID, OnlySceneNames.ToArray());
  102.  
  103.         if(GUILayout.Button("Switch", GUILayout.MaxHeight(14f), GUILayout.MaxWidth(100f)))
  104.             OpenScene(QuickSwitch1SceneListID);
  105.  
  106.         GUILayout.EndHorizontal();
  107.  
  108.         GUILayout.Space(2f);
  109.  
  110.         GUILayout.BeginHorizontal();
  111.         GUILayout.Label("Quick switch 2");
  112.         QuickSwitch2SceneListID = EditorGUILayout.Popup(QuickSwitch2SceneListID, OnlySceneNames.ToArray());
  113.  
  114.         if(GUILayout.Button("Switch", GUILayout.MaxHeight(14f), GUILayout.MaxWidth(100f)))
  115.             OpenScene(QuickSwitch2SceneListID);
  116.         GUILayout.EndHorizontal();
  117.  
  118.         GUILayout.Space(10f);
  119.  
  120.         if(!dropDownSceneList) {
  121.             if(GUILayout.Button("Show Scenes"))
  122.                 dropDownSceneList = true;
  123.         }
  124.         else {
  125.             if(GUILayout.Button("Hide Scenes"))
  126.                 dropDownSceneList = false;
  127.  
  128.             _scrollPosition = GUILayout.BeginScrollView(_scrollPosition, GUILayout.Height(position.height / 1.8f));
  129.  
  130.             string lastDir = "";
  131.             for(int i = 0; i < Scenes.Count; i++) {
  132.  
  133.                 var scene = Scenes[i];
  134.  
  135.                 if(lastDir != scene.FolderName) {
  136.                     lastDir = scene.FolderName;
  137.                     GUILayout.Space(10);
  138.                     GUILayout.Label(lastDir);
  139.                 }
  140.  
  141.                 if(GUILayout.Button(scene.SceneName))
  142.                     OpenScene(i);
  143.             }
  144.             GUILayout.EndScrollView();
  145.         }
  146.     }
  147.  
  148.     void OpenScene(int id)
  149.     {
  150.         if(EditorApplication.SaveCurrentSceneIfUserWantsTo())
  151.             EditorApplication.OpenScene(Scenes[id].ScenePath);
  152.     }
  153.  
  154.     void SearchForScenes()
  155.     {
  156.         OnlySceneNames.Clear();
  157.         Scenes.Clear();
  158.         ProcessDirectory(RootFolder);
  159.     }
  160.  
  161.     void ProcessDirectory(string targetDirectory)
  162.     {
  163.         // Process the list of files found in the directory.
  164.         string[] fileEntries = Directory.GetFiles(targetDirectory);
  165.         foreach(string fileName in fileEntries)
  166.             ProcessFile(fileName, targetDirectory);
  167.  
  168.         // Recurse into subdirectories of this directory.
  169.         string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
  170.         foreach(string subdirectory in subdirectoryEntries)
  171.             ProcessDirectory(subdirectory);
  172.     }
  173.  
  174.     // Insert logic for processing found files here.
  175.     void ProcessFile(string fileName, string folderName)
  176.     {
  177.         if(!fileName.Contains(".unity") || fileName.Contains(".meta")) return;
  178.         var name = fileName.Substring(fileName.LastIndexOf('\\') + 1);
  179.         name = name.Substring(0, name.Length - 6);
  180.         var sceneInfo = new SceneInfo {
  181.             SceneName = name,
  182.             ScenePath = fileName,
  183.             FolderName = folderName
  184.         };
  185.         Scenes.Add(sceneInfo);
  186.         OnlySceneNames.Add(sceneInfo.SceneName);
  187.     }
  188. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top