- Home /
How can I allow a player to select from multiple scenes to load?
I'd like for players to be able to select from an arbitrary number of levels to load from some kind of hub (teleporter, map, etc) using a graphical menu. How can I achieve this effect?
Answer by burnumd · Nov 25, 2009 at 01:57 PM
In the simplest sense, you would set up an action to trigger the visibility of your menu (using OnTriggerEnter, OnMouseDown, or the like). The GUI code would look something like the following:
var availableLevels : String[]; var windowWidth : int = 200; var windowHeight : int = 200; private var showMenu : boolean = false;
function TriggerAction () { //Replace TriggerAction with your desired means of triggering the menu. showMenu = true; }
function OnGUI () { if (showMenu) { GUI.Window(0, Rect((Screen.width / 2) - (windowWidth / 2), (Screen.height / 2) - (windowHeight / 2), windowWidth, windowHeight), LevelSelect, "Select a level!"); //Creates a window with ID 0 in the center of the screen using the function LevelSelect with the title, "Select a level!" } }
function LevelSelect (id : int) { for (var levelName : String in availableLevels) { if (GUILayout.Button(levelName)) { Application.LoadLevel(levelName); } } if (GUILayout.Button("Cancel")) { //Gives the player the opportunity to back out of the menu. showMenu = false; } }
okay, i'm using this script, and i voted you up because.... you're awesome and this script works, but all i did was change if(GUILayout.Button(levelName)) to ...("Level1")) and made copies of that for my six levels (still working on levels) and it works, when i click on the button, it sends me to my levels, but it seems that it makes copies of the selectable levels, like a list of levels repeating 1,2,3,4,5,6,1,2,...etc. for ever. can you help me?
You shouldn't add copies of the Button. It automatically draws a button for every level name you put into availableLevels
(that's what the for-loop is for).
Answer by Jaap Kreijkamp · Nov 25, 2009 at 02:01 PM
Create C# class with name LoadLevelOnTrigger.cs and paste following code to it:
using UnityEngine; using System.Collections;
public class LoadLevelOnTrigger : MonoBehaviour {
public string levelName;
void OnTriggerEnter(Collider other) {
if (other.gameObject.tag == "Player") {
Application.LoadLevel(levelName);
}
}
}
Add script to object with collider set to trigger, set levelName property in inspector and walk into the collider (the player must have Tag "Player"). That's it.
Answer by Murcho · Nov 25, 2009 at 01:50 PM
Add each scene you wish to include in the build settings for your game. Then however you are setting up selection of these levels, via a menu list or other wise, you launch the selected level using :
Application.LoadLevel("NameOfLevel");
There are a couple of other options for loading levels, but they either require Pro or are for multi segmented levels. You can find all the available functions here.
Answer by SpiritWebb · Nov 25, 2009 at 03:09 PM
Not answering, just asking a question that involves the setup. burnumd wrote the code out, but what I am wanting to know with this question, is:
I have one jumpgate with locations (scenes): Vega & Alpha Centauri. Say I travel to A.C. now in that system, there is another gate, locations: Pegasus, Galnor & Bewlan. And in the previous gate: Sol.
Is this possible, and how would I set this up compared to the above code given by burnumd? Any help with this is greatly appreciated!
Add the script above to individual jumpgates in the various scenes. From the inspector, you can specify which scenes are available from which gates.
Thanks. I will try this when I get home from work! And let you know if it worked, or done something wrong!
This works, and it will be a big improvement, to allow other interactions as well...
I tried. It doesn't really give me options, except for the scene number and I cant specify which systems I want linked.
When I tried to run, it tells me: Level couldn't be loaded because its not added to the build settings.
I know it is, cause I checked and made sure. Something is wrong, and I don't know what it is.
Never$$anonymous$$d, I got it working.
I learned that the available levels, when you change the number, it gives Element 0,1,2,etc. I didn't look at the fact I could click in the open space to specify a level name to make it work.
I had a blonde moment...
So sad that I do not have enough rep for vote down yet... :(
Your answer
Follow this Question
Related Questions
Loading a scene in game with a GUI 1 Answer
iOS Scroll Effect - SceneGUI Objects 0 Answers
Change to scene based on current scene 1 Answer
Display multiple scenes? 1 Answer
fade between scenes? 1 Answer