开发者

open and play a scene in Unity3d

开发者 https://www.devze.com 2023-03-22 03:43 出处:网络
I have done a project with many scenes in Unity3D. In the first scene there are buttons, each of them will play a scene when clicked.

I have done a project with many scenes in Unity3D. In the first scene there are buttons, each of them will play a scene when clicked. For exam开发者_JAVA技巧ple, if the player clicks the button “Show the balloon”, then the scene called Balloon (which contains a balloon object and its animation) will be opened.

How can I do it using JavaScript code?


See Application.LoadLevel(...).

From the documentation:

[...]. Before you can load a level you have to add it to the list of levels used in the game. [...]

// Loads the level with index 0
Application.LoadLevel (0);

// Load the level named "HighScore".
Application.LoadLevel ("HighScore");


First thing you have to do is add all the levels you want into the Build Settings Property. Go to Unity or File Menu, Build Settings, and drag and drop your scenes into the scroll window. Then it'll assign them a logical number (or you can reference by name).

Application.LoadLevel(0); // this loads the first level in the list
Application.LoadLevel("nameoflevel"); //does the same thing numerically except by name
Application.LoadLevel(Application.loadedLevel); //reloads current level
Application.LoadLevel(Application.loadedLevel + 1); //loads the next level in order
Application.LoadLevel(Application.loadedLevel - 1); //loads the prior level in order
Application.LoadLevel(Application.levelCount - 1); //loads the last level in list


The int version of LoadLevel takes the id from build settings. You can also call the string version, which takes the scene name from the project view (Though it still has to be in build settings for it to be found)

Go to File->Build Settings and drag your scenes there then use following.

Application.LoadLevel("Ballon");

If you want to reload the current level, you can use

Application.LoadLevel (Application.loadedLevel);

You'll, of course, need to make sure to manually reset or destroy anything that was created by the scene and marked as DontDestroyOnLoad.

And yes, the only way to put levels in any order in your build is from the build settings menu. You can jump around to which level is loaded by specifying its name or build order, but if you wanted to insert your levels in order in the build sequence and just want to jump from one level to the next (ie, Menu->Level1->Level2->EndGame), you can use

Application.LoadLevel (Application.loadedLevel + 1);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号