Scene switching in Unity

Source: Internet
Author: User

In Unity game development, it seems impossible to solve all the problems in a single scene, so the switchover between multiple scenes is inevitable. If it is just a switch, it seems to say anything, but when the scene is relatively large do not want to let the player wait to load or between scenes and scenes want to through some of the screen, animation to show something to let the player look forward to, we have to seriously consider. This article mainly describes two ways to increase the number of transitions in the screen or animation, etc., to improve the player's immersion, of course, you can also make a seamless scene.

1 Operating Base functions
loadlevel loads the level by its name or index.  
load scene, before loading you need to put the scene in build settings Add, scene loading, the scene of the activation of the object will call Monobehavior:o Nlevelwasloaded (). When using this method, the objects in the previous scene will be deleted directly.
loadleveladditive loads A level additively.
This method does not destroy objects in the current scene, and objects in the new scene will be added, which is useful in a number of sequential loading scenarios.
loads the level additively and asynchronously in the background.
Record scenes asynchronously in the background
unity loads all the scene resources in the background thread, which allows you to play a progress bar during the loading of a new scene or to create a world of processes for the player by asynchronously loading
The method returns asyncoperation structure, structure isdone indicates completion, progress gives the current playback progress. Note that the performance of the background thread in the editor is lower than in the game.
Loads the level asynchronously in the background.
Asynchronously record a scene in the background
Unity loads all the scene resources in the background thread, which allows you to play a progress bar during the loading of a new scene or to create a world of processes for players by loading asynchronously
The method returns the asyncoperation structure, in which Isdone indicates whether it is complete andprogress gives the current playback progress. Note that the performance of the background thread in the editor is lower than in the game.
Whether to destroy

Loadlevel and Loadlevelasync will destroy objects in the original scene immediately after loading, and loadleveladditive and Loadleveladditiveasync will retain the objects in the original scene after loading, This approach allows for seamless integration of scenarios where you need to load the back of the scene in the right place, but you still have to consider the release of the resource.

Synchronous or asynchronous asynchronous loading can get the progress of the loading process and whether the load is completed, in this way you can change the progress bar or other performance in the switch, which is generally done by the co-process:
 Public class
{    
    {        async = Application.loadlevelasync ("mybiglevel");         yield return Async ;        Debug.Log ("Loadingcomplete");}    }
It is important to note that prior to use, theBuild SettingsAdd scene to the 2 Switch Screen realization method

There are many ways to implement a screen or animation in a scene switch, and there are two main ways I can see it here:

The first way: Add a new scene

Add a new scene to the way you can refer to the blog: Unity3d Research Institute of the asynchronous loading game scene and the asynchronous loading game resource progress bar (31), adding a new scene has the advantage of adding a lot of flexibility, you in the intermediate transition scene to add video playback, pictures or GUI, The downside is that if you want to achieve commonality, you need to do some data assistance at the top level, such as the picture in the pre-loading scene set up to play when the switch is in the top-level data management module, after the immersion switch scene in the acquisition of the data, according to the data to switch playback animation and so on.

On the left, there are three scenes in the test project, the pre is the current scene, the middle is the switching scene, and next is the scene that needs to be loaded. The image on the right is the existing object in the middle scene, and the implementation script is hung. Case logic: Pre-scene directly into the middle scene, pre-loading the next scene in the middle scene to ensure that the new scene does not immediately display the next scene, but wait for the user's touch or click action

The specific operation process:

(1) using the synchronous loading method in the pre scene to enter the middle scene directly, because the middle scene is very small, so this process will be very fast

Application.loadlevel ("Middle");

(2) Logic of the middle scene

 Public classNewbehaviourscript2:monobehaviour { PublicTexture Backimage =NULL; PrivateAsyncOperationAsync=NULL; voidStart () {//This object will not be destroyed in the next scene.Dontdestroyonload ( This); //start loading the sceneStartcoroutine ("Loadscene"); }        //Asynchronous loadingIEnumerator Loadscene () {Async= Application.loadlevelasync ("Next"); yield return Async; Debug.Log ("complete!"); }    voidOngui () {//Toggle the background of the scene, can be a picture or animation, or ~ ~Gui. Drawtexture (NewRect (0,0, Screen.width, screen.height), backimage); //progress is displayed during loading, or it can be a progress bar        if(Async!=NULL&&Async. IsDone = =false) {Guistyle style=NewGuistyle (); Style.fontsize= -; Gui. Label (NewRect (0, $, Screen.width, -),Async. Progress. ToString ("F2"), style); }        //at the end of the load, pops up whether the next scene, Analog hand tour "touch anywhere into the game"        if(Async!=NULL&&Async. IsDone = =true)        {            if(GUI. Button (NewRect ( -, -, -, -),NewGuicontent ("jump into the next scene")) {Destroy ( This); }        }    }}

A scene to add a full screen of the background map, of course, you can use animation, video, etc., the scene loaded immediately after the loading of the next scene (asynchronous), based on the progress of asynchronous loading progress bar, animation, etc. progress control

B Note: The Middleobj object is set to Dontdestroyonload (this), which is not destroyed in the new scene, even after the new scene is loaded, the full-screen background still exists until the object is destroyed.

C Middleobj objects are destroyed in two ways, one is manual, just like in the code below, click the middle scene button to destroy yourself, another automatic way is in the new scene (next scene) of the Monobehavior:o Destroy the object in the nlevelwasloaded () function.

void onlevelwasloaded (int  level)   {       = Gameobject.find ("middleobj" ) );        if NULL )       {           Gameobject.destroy (obj);       }   }

Rain pine in the implementation of the new loading scene first to hide all the objects in the scene, the scene after the switch and then slowly display, rather than through the dontdestroyonload way, is also a way ha. If you want to design this way as a common mode, you can add a data manager, pre-loading the required pictures, animation resources and so on, OK, you can use in a number of occasions.

The second way to use prefab

This approach is similar in principle to the first, except that instead of using a new scene, you add logic in the pre scene similar to the Middleobj object in the middle scene above and set it to Dontdestroyonload. In this way can refer to is Scenemanager scene management plug-in, Scenemanager use can refer to:


http://blog.csdn.net/onerain88/article/details/12303511

http://www.haogongju.net/art/2499058

I'm not going to say much about it. Ha, let me first look at the key principles of its implementation: Scenemanger uses asynchronous loading of new scenes, after loading, it will use a different screen effect depending on the selection, and this effect will not disappear in the new scene, until the desired effect target reached, will show the first scene. Scenemanger provides a few columns of screen effect prefab and scene management, very good plugin ha.

Test code Download: Http://pan.baidu.com/s/1o67dAiA Password: l976, with Scenemanager plug-in (spent 15 of the ocean bought)

Drizzle flag: Unity

Scene switching in Unity

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.