Unity 5.3 Load Scene later

Source: Internet
Author: User
Tags unique id unity 5

Document the method of loading scenarios that are officially recommended:

Startcoroutine (Loadyourasyncscene ()); IEnumerator Loadyourasyncscene () {//The application loads the scene in the background at the same time as the current scene. //This is particularly good for creating loading screens. You could also load the Scene by build//Number .AsyncOperation asyncload =Scenemanager.loadsceneasync (scene_name); //Wait until the last operation fully loads to return anything         while(!asyncload.isdone) {yield return NULL; }    }

This article describes how to use the new API introduced by Unity5.3 UnityEngine.SceneManagement to manage scenarios and levels.

Introduced

After 5 versions of the development, Unity5.3 released a unified and reasonable scene management API. In previous versions, the scene was the code level ( Level ). But using the unity process quickly found that it was not necessary to use a scene to define different levels , so the Scene API was introduced.
The levels in the previous version indicated the build index number of the level (that is, the sequence number of the level in build Settins ) at a logical point, and the scene indicates the name of the resource that contains the scene. Neither of these concepts, however, is a reliable unique ID that identifies a real scene . Build Settings moves the scene to change their index. Similarly, there may be two scenes of the same name under different folders. Unity5.3 tried to put an end to these chaotic logic.

New Scenario API in version 5.3

Using the Build index and scene resource names before Unity5.3 can point to a scene. Unity5.3 introduced a new Scene structure to indicate a scene. It encapsulates some of the scenarios commonly used by methods and variables such as buildIndex , name and path .

Access SceneManagerClass

To use the new scene api,c# code needs to reference the SceneManagement namespace

1
Using Unityengine.scenemanagement;

This allows you to SceneManager replace the Application deprecated related scene function with a class.

Get current scene

One of the main reasons unity has changed to "scene" instead of "level" is that you can load multiple scenes at the same time. This destroys the concept of "current level", which is now replaced by the "Activate scene". In most cases, the active scene is the most recently loaded scene. We can use it at any time to GetActiveScene get the activation scene.

1
2
3
4
5
6
7
8
5.3
Scene scene = Scenemanager.getactivescene ();
Debug.Log (Scene.name);
Debug.Log (Scene.buildindex);

5.2
Debug.Log (Application.loadedlevelname);
Debug.Log (Application.loadedlevel);

Detects if the active scene is a specified scene name:

1
2
3
4
"Scenename")
{
// ...
}

You can also use the overloaded == operator to detect:

1
2
3
4
if (scenemanager.getactivescene () = = Scene)
{
// ...
}

Load scene

Like the previous version, we can load the scene by building the index and the scene name, and we recommend using a path to load the specified scene with two scene names but different paths, or unity will load the first scene that matches the name.

Loading a single scene

The default is to load a single scene, and loading a new scene will unload all the old scenes that have been loaded and destroy all the old ones GameObjects .

 1 
2
3
4
5
6
7
8
5.3
Scenemanager.loadscene (4);
Scenemanager.loadscene (//Scene name mode, ignoring case
Scenemanager.loadscene ("Path/to/scene/file/scenename"); Scenario resource path mode, ignoring case

5.2
Application.loadlevel (4);
Application.loadlevel ("Scenename");

Asynchronous version:

 1 
2
3
4
5
6
7
8
 //5.3 
Scenemanager.loadsceneasync (4);
scenemanager.loadsceneasync ( "Scenename");
scenemanager.loadsceneasync ( "Path/to/scene/file/scenename");
//5.2
Application.loadlevelasync (4);
application.loadlevelasync ( "Scenename");

Add a way to load a scene

We can also SceneManagement.LoadSceneMode.Additive load a new scene by specifying the way to add a new scene:

1
2
3
4
5
6
7
8
5.3
Scenemanager.loadscene (4, SceneManagement.LoadSceneMode.Additive);
Scenemanager.loadscene ("Scenename", SceneManagement.LoadSceneMode.Additive);
Scenemanager.loadscene ("Path/to/scene/file/scenename", SceneManagement.LoadSceneMode.Additive);

5.2
Application.loadleveladditive (4);
Application.loadleveladditive ("Scenename");

Asynchronous version:

 1 
2
3
4
5
6
7
8
5.3
Scenemanager.loadsceneasync (4, SceneManagement.LoadSceneMode.Additive);
Scenemanager.loadsceneasync ("Scenename", SceneManagement.LoadSceneMode.Additive);
Scenemanager.loadsceneasync ("Path/to/scene/file/scenename", SceneManagement.LoadSceneMode.Additive);

5.2
Application.loadleveladditiveasync (4);
Application.loadleveladditiveasync ("Scenename");

Uninstalling a scene

Unity5.3 also provides a UnloadScene way to unload a specified scene:

1
2
3
4
5.3
Scenemanager.unloadscene (//unload by Build Index
Scenemanager.unloadscene (//unload by scene name or scene path)
Unload by scene structure (loading the scene without providing a way to load through the scene structure)

The function returns whether the scene was successfully unloaded.

Summarize

With a brief introduction to the scenario Management API in version 5.3, we see a new scenario management approach in unity that provides a clearer and more reliable process for dynamic load management. If you are using version 5.3 or updating your code as soon as possible, use the new API to manage the scenario.

Unity 5.3 Load Scene later

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.