Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Scene jumper

Discussion in 'Scripting' started by v3n0mou5, Jul 29, 2014.

  1. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    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
     
  2. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    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):
    1.  
    2. using UnityEditor;
    3. using UnityEngine;
    4. using System.Linq;
    5. using System.Collections.Generic;
    6.  
    7. public class LevelSelector : EditorWindow
    8. {
    9.     public Vector2 scrollPosition;
    10.  
    11.     [MenuItem("Window/LevelSelector")]
    12.     public static void ShowWindow()
    13.     {
    14.         EditorWindow.GetWindow(typeof(LevelSelector));
    15.     }
    16.  
    17.     void OnGUI()
    18.     {
    19.         GUILayout.BeginHorizontal("box");
    20.         GUILayout.Label("List of enabled scenes in the project");
    21.         if (GUILayout.Button("Refresh", GUILayout.Width(100)))
    22.         {
    23.             Repaint();
    24.         }
    25.         GUILayout.EndHorizontal();
    26.  
    27.         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(250));
    28.         for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
    29.         {
    30.             string sName = FillLevels()[i].Substring(FillLevels()[i].LastIndexOf('/') + 1);
    31.             sName = sName.Substring(0, sName.Length - 6);
    32.  
    33.             GUILayout.BeginHorizontal("box");
    34.             if (GUILayout.Button(sName, GUILayout.Width(150)))
    35.             {
    36.                 EditorApplication.SaveCurrentSceneIfUserWantsTo();
    37.                 EditorApplication.OpenScene(FillLevels()[i]);
    38.             }
    39.  
    40.             if (FillLevels()[i] == EditorApplication.currentScene)
    41.             {
    42.                 if (GUILayout.Button("Save", GUILayout.Width(60)))
    43.                 {
    44.                     EditorApplication.SaveScene(FillLevels()[i]);
    45.                 }
    46.             }
    47.             GUILayout.EndHorizontal();
    48.         }
    49.         GUILayout.EndScrollView();
    50.     }
    51.  
    52.     private string[] FillLevels()
    53.     {
    54.         List<string> temp = new List<string>();
    55.         foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes)
    56.         {
    57.             if (S.enabled)
    58.             {
    59.                 string path = S.path;
    60.                 temp.Add(path);
    61.             }
    62.         }
    63.         return temp.ToArray();
    64.     }
    65. }
    66.  
     
    Last edited: Jul 29, 2014
  3. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    it doesnt work man, get an error that crashes my unity
     
  4. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    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 :D
    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?
     
  5. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    i get an array is out of index error. on line 32.
    its this line:

    "string sName = FillLevels().Substring(FillLevels().LastIndexOf('/') + 1);"
     
  6. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    I just updated the code above.
    Please check that one out :D

    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 :)
     
  7. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    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
     
  8. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    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.
     
  9. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    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):
    1. string sName = FillLevels()[i].Substring(FillLevels()[i].LastIndexOf('/') + 1);
     
    Last edited: Jul 29, 2014
  10. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    no clue man...
    this is how it looks for me with the updated code

    upload_2014-7-29_13-7-14.png
     

    Attached Files:

  11. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    do you use the latest Unity 4.5.2 ?
     
  12. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    i thought i was but i was using 4.5.1, im updating as we speak
     
  13. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    new unity same problem, i made a folder called Editor in my assets and placed the script in there but still same error
     
  14. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    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.
     
  15. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    i copied the script exactly
    heres the code:

    Code (CSharp):
    1. using UnityEditor;
    2. using UnityEngine;
    3. using System.Linq;
    4. public class LevelSelector : EditorWindow
    5. {
    6.     public Vector2 scrollPosition;
    7.     [MenuItem("Window/LevelSelector")]
    8.     public static void ShowWindow()
    9.     {
    10.         EditorWindow.GetWindow(typeof(LevelSelector));
    11.     }
    12.  
    13.     void OnGUI()
    14.     {
    15.         GUILayout.Label("List of enabled scenes in the project");
    16.         scrollPosition = GUILayout.BeginScrollView(scrollPosition, GUILayout.Height(250));
    17.         for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
    18.         {
    19.             string sName = FillLevels()[i].Substring(FillLevels()[i].LastIndexOf("/") + 1);
    20.             sName = sName.Substring(0, sName.Length - 6);
    21.             GUILayout.BeginHorizontal("box");
    22.             if (GUILayout.Button(sName, GUILayout.Width(150)))
    23.             {
    24.                 EditorApplication.OpenScene(FillLevels()[i]);
    25.             }
    26.             if (FillLevels()[i] == EditorApplication.currentScene)
    27.             {
    28.                 if (GUILayout.Button("Save", GUILayout.Width(60)))
    29.                 {
    30.                     EditorApplication.SaveScene(FillLevels()[i]);
    31.                 }
    32.             }
    33.             GUILayout.EndHorizontal();
    34.         }
    35.         GUILayout.EndScrollView();
    36.     }
    37.     private string[] FillLevels()
    38.     {
    39.         return (from scene in EditorBuildSettings.scenes where scene.enabled select scene.path).ToArray();
    40.     }
    41. }
     
  16. v3n0mou5

    v3n0mou5

    Joined:
    Jan 8, 2014
    Posts:
    14
    hey man with the new code it works, thx mate :D
     
  17. johnnydj

    johnnydj

    Joined:
    Apr 20, 2012
    Posts:
    211
    glad to hear :)
    not sure why it didn't work for you with the other code, cause it works for me both ways :))