hey guys, I am trying to make a editor window that shows me all the scenes that are in the current build. And if i build a new scene and i add it to the build. That it will show up automatically. i want this window to have a button behind the scenes that say: "load", and "save". for example: Scenes: level01 load save level02 load save level03 load save But i have no idea how to begin, if you guys could help me that would be great. And if possible in c# please. ps sorry for my english -joey
Here you go. You will find the button in the Windows Menu, called LevelSelector. Save the file named LevelSelector.cs in the Editor folder of your project. Updated: Contains Level load buttons array with level names. Also asks to save upon change. Contains Save level button to save currently opened scene. Refresh button just in case. Code (CSharp): using UnityEditor; using UnityEngine; using System.Linq; using System.Collections.Generic; public class LevelSelector : EditorWindow { public Vector2 scrollPosition; [MenuItem("Window/LevelSelector")] public static void ShowWindow() { EditorWindow.GetWindow(typeof(LevelSelector)); } void OnGUI() { GUILayout.BeginHorizontal("box"); GUILayout.Label("List of enabled scenes in the project"); if (GUILayout.Button("Refresh", GUILayout.Width(100))) { Repaint(); } GUILayout.EndHorizontal(); scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(250)); for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) { string sName = FillLevels()[i].Substring(FillLevels()[i].LastIndexOf('/') + 1); sName = sName.Substring(0, sName.Length - 6); GUILayout.BeginHorizontal("box"); if (GUILayout.Button(sName, GUILayout.Width(150))) { EditorApplication.SaveCurrentSceneIfUserWantsTo(); EditorApplication.OpenScene(FillLevels()[i]); } if (FillLevels()[i] == EditorApplication.currentScene) { if (GUILayout.Button("Save", GUILayout.Width(60))) { EditorApplication.SaveScene(FillLevels()[i]); } } GUILayout.EndHorizontal(); } GUILayout.EndScrollView(); } private string[] FillLevels() { List<string> temp = new List<string>(); foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes) { if (S.enabled) { string path = S.path; temp.Add(path); } } return temp.ToArray(); } }
updated the script to show only the scene names instead of the path. Note: it is able only to load levels currently. When I'll get home, I'll extend it with a Save button also. Unless someone else does it before me the script should work perfectly. I'm using Unity 4.5.2 btw. If you open the script with MonoDevelop, do you get any errors in the code?
i get an array is out of index error. on line 32. its this line: "string sName = FillLevels().Substring(FillLevels().LastIndexOf('/') + 1);"
I just updated the code above. Please check that one out you are missing FillLevels() in the snippet you posted. Just try the whole code that I posted now. I also made the Save button. Save button will appear only for the currently opened scene. Otherwise you would save the current scene onto another scene
i copy paste your whole code inside a script called LevelSelector. thats inside a folder called Editor and i keep getting the same error array index is out of range
make sure this line: string sName = FillLevels().Substring(FillLevels().LastIndexOf('/')+1); is exactly the same in your script. in the code you posted above, you were missing the part in 2 places. Also, the script should be placed in the Editor folder of your project.
its so weird i have no idea why it doesnt work its inside my Editor folder. and this is the line where it gives an error: Code (CSharp): string sName = FillLevels()[i].Substring(FillLevels()[i].LastIndexOf('/') + 1);
new unity same problem, i made a folder called Editor in my assets and placed the script in there but still same error
alright, check the updated code now (refresh the page) I changed the way I get the path of the scenes. Don't forget to include all the "using" parts. Let me know if it works out for you.
i copied the script exactly heres the code: Code (CSharp): using UnityEditor; using UnityEngine; using System.Linq; public class LevelSelector : EditorWindow { public Vector2 scrollPosition; [MenuItem("Window/LevelSelector")] public static void ShowWindow() { EditorWindow.GetWindow(typeof(LevelSelector)); } void OnGUI() { GUILayout.Label("List of enabled scenes in the project"); scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(250)); for (int i = 0; i < EditorBuildSettings.scenes.Length; i++) { string sName = FillLevels()[i].Substring(FillLevels()[i].LastIndexOf("/") + 1); sName = sName.Substring(0, sName.Length - 6); GUILayout.BeginHorizontal("box"); if (GUILayout.Button(sName, GUILayout.Width(150))) { EditorApplication.OpenScene(FillLevels()[i]); } if (FillLevels()[i] == EditorApplication.currentScene) { if (GUILayout.Button("Save", GUILayout.Width(60))) { EditorApplication.SaveScene(FillLevels()[i]); } } GUILayout.EndHorizontal(); } GUILayout.EndScrollView(); } private string[] FillLevels() { return (from scene in EditorBuildSettings.scenes where scene.enabled select scene.path).ToArray(); } }
glad to hear not sure why it didn't work for you with the other code, cause it works for me both ways )