x


How to get names of all available levels

I know that with Application.loadedLevelName I can get the name of the current level. But how do I get the names of all available levels? My interest in doing this is to create a level-select menu that lists all levels by name.

more ▼

asked Nov 08 '10 at 05:26 PM

Atlas gravatar image

Atlas
50 2 2 4

Where do you exactly attach that script ?

Nov 26 '12 at 10:13 AM PsionicTr
(comments are locked)
10|3000 characters needed characters left

2 answers: sort oldest

I think you'd have to do that manually, since there are no functions that return all level names.

more ▼

answered Nov 08 '10 at 06:38 PM

Eric5h5 gravatar image

Eric5h5
80.1k 41 132 519

That's a shame, it would be useful to enumerate available level names.

Apr 29 '12 at 02:23 AM numberkruncher

@kruncher: Yes it would be useful ;) I'm pretty sure they implement it the other day, but it's just a minor issue. Until then you could use the script i've just written.

Apr 29 '12 at 03:11 AM Bunny83

Unity crew, oh please implement a runtime method to get the names of the available scenes.

Oct 30 '12 at 08:58 PM CarlEmail

where do you attach that script ?

Nov 26 '12 at 10:12 AM PsionicTr

What about, you put all your scenes in one folder and using .Net framework IO functions read the content in that folder and listed as available levels.

Other idea would be the use assets files or XML with the description (text and image), location and configuration on each level, parse the info and display it.

But is mandatory you add all the scenes at the building configuration.

Feb 01 at 06:17 PM y0ux
(comments are locked)
10|3000 characters needed characters left

I've just written a little helper that makes it easier to get the names "manually".

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class ReadSceneNames : MonoBehaviour
{
    public string[] scenes;
    #if UNITY_EDITOR
    private static string[] ReadNames()
    {
        List<string> temp = new List<string>();
        foreach (UnityEditor.EditorBuildSettingsScene S in UnityEditor.EditorBuildSettings.scenes)
        {
            if (S.enabled)
            {
                string name = S.path.Substring(S.path.LastIndexOf('/')+1);
                name = name.Substring(0,name.Length-6);
                temp.Add(name);
            }
        }
        return temp.ToArray();
    }
    [UnityEditor.MenuItem("CONTEXT/ReadSceneNames/Update Scene Names")]
    private static void UpdateNames(UnityEditor.MenuCommand command)
    {
        ReadSceneNames context = (ReadSceneNames)command.context;
        context.scenes = ReadNames();
    }

    private void Reset()
    {
        scenes = ReadNames();
    }
    #endif
}

You just have to setup your build settings and add all scenes you want to include and then just press the "Update Scene Names" button. This will store the current active scene names in the public array of the script.

GetSceneNames

more ▼

answered Apr 29 '12 at 03:09 AM

Bunny83 gravatar image

Bunny83
45k 11 48 206

Where exactly do you attach that script ?

Nov 26 '12 at 10:12 AM PsionicTr

It doesn't matter where you attach it. Just attach it on an object in the scene. When you want to use the level names you can easily read the scenes array of that script by referencing this script instance in your script.

Keep in mind to press the Update Scene Names before you create a build / run the game.

You could also make the scenes array static so it can accessed more easily from other scripts.

Nov 26 '12 at 10:52 AM Bunny83

Well I though so also so I just attached it to one of the game objects but when I press update the array remains empty. Any ideas ?

Nov 26 '12 at 11:45 AM PsionicTr

I currently have 2 scenes in my assets folder though when I press the UpdateScene Names it does not update the array it still remains 0

Nov 26 '12 at 11:47 AM PsionicTr

@psionictr:

You have to add the scenes to your build array first. Only the scenes which are "ticked" in the build list are even included in your game. Just Press "File --> Build Settings" and drag and drop the scenes which should be part of your game into the "Scenes in build" list.

Nov 26 '12 at 10:41 PM Bunny83
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x322
x143

asked: Nov 08 '10 at 05:26 PM

Seen: 3699 times

Last Updated: Feb 01 at 06:17 PM