Unity3d 4.61 implements a fade-out scene transition method.

Source: Internet
Author: User

Still in the process of learning, if there is much to see please pointing.

The scene can be loaded directly by using the original Application.loadlevel () function.

But because it's too stiff, here's a fade-in effect to make a transition.

First, the idea:

As long as the loading is complete and the switch is complete, the screen is black before the switch is completed.

1. Create a mask.

The color is in black. #000

The alpha is set to 0 by default.

Let the mask layer cover the entire screen.

The set mask layer is not destroyed.

2. When the scene switch is performed, the alpha value of the mask layer is modified, and the opacity is changed from transparent to opaque.

Note: In the script, alpha value 0 is transparent and 1 is opaque.

3. Scene switch. 4. After the scene switch succeeds, modify the mask layer's alpha value, which is changed from opaque to transparent.

Second, the main issues:1. What if you keep the mask layer on top?

Use "UI" and "Panel" as a mask layer.

The UI layer is always on top of the top.

If you don't use the UI, adjust it to not be blocked by other game elements.

2. The same is the UI layer element, if the mask layer to cover the other UI, then the other UI panel will not be used.

This is not a solution yet ... My current practice is to direct the script to do another processing control directly does not show up.

3. After switching the scene, the original scene of the game objects will be destroyed, how to let the mask layer is not destroyed?

Add a script to the mask layer, add this to the script, you can check the document.

    void Awake ()    {        Dontdestroyonload (this. transform.gameobject);    }

Third, the development1. Define some public methods for ease of use.

Since I now only know how to use the "Tag" element in the script to get the game component, first create a class and create a static method that is public.

 Public classsettings:monobehaviour{/// <summary>    ///loads the object according to the tag. /// </summary>    /// <returns></returns>     Public StaticTSource findcomponentbytag<tsource> (stringTagwheretsource:component {tsource source=NULL; //locate the object. Gameobject SourceObject =Gameobject.findwithtag (tag); if(SourceObject! =NULL) {Source= sourceobject.getcomponent<tsource>(); }        Else{Debug.Log ("not Found ""+ Tag +""script. "); }        returnsource; }}

2. Create a mask layer in Unity.

2.1. Create a camera, rename it to "Loadscenecamera", and adjust the display screen.

2.2. Create a "Canvas" below the camera. and create a "panel" under "Canvas" and rename it "Shade".

2.3. Select the Mask layer ("Shade"), create a tag in inspector for the Mask layer ("Shade"), name it "Shade", and select "Shade" in the "Tag" option.

2.4. Create a script for the Mask layer ("Shade") and name it "Loadscene".

3. Write the script.

usingUnityengine;usingUnityengine.ui;usingSystem.Collections; Public classLoadscene:monobehaviour {/// <summary>    ///whether to hide the screen (that is: Fade out). /// </summary>    Private Static BOOL_ishide =false; /// <summary>    ///whether the screen is displayed (that is, fade in). /// </summary>    Private Static BOOL_isshow =false; /// <summary>    ///the Alpha value of the gradient. /// </summary>    Private Static float_start =0; /// <summary>    ///The scene name in the toggle. /// </summary>    Private Static string_scene =string.    Empty; voidUpdate () {if(_ishide) {//gets the original color of the mask layer. Color color = settings.findcomponentbytag<image> ("Shade"). Color; //because the Alpha value is "0" for transparency, "1" stands for opacity. //to be smooth, this is set to increase by 0.1 per update. _start + =0.1f; //sets the Alpha value to the new Color object, based on the value of color. Color end =NewColor (COLOR.R, COLOR.G, Color.b, _start); //sets the color of the matte layer. //color.lerp (color, end, 1); The method represents the color gradient from which to what color. Please check the documentation for details. Settings.findcomponentbytag<image> ("Shade"). color = Color.lerp (color, end,1); //when _start is greater than or equal to 1 o'clock, it indicates that the gradient is complete.             if(_start >=1)            {                //load a new scene. Application.loadlevel (_scene); //Resets the value to 1. _start =1; //set to not fade out. _ishide =false; //set to be able to fade in. _isshow =true; }        }        //When the fade out operation is complete.         if(!_ishide &&_isshow) {            //Ibid. Color color = settings.findcomponentbytag<image> ("Shade"). Color; //setting values is the opposite of fading out. _start-=0.1f; if(_start <=0)            {                //Resets the value to 0. _start =0; //set to Not fade in. _isshow =false; } Color End=NewColor (COLOR.R, COLOR.G, Color.b, _start); Settings.findcomponentbytag<Image> ("Shade"). color = Color.lerp (color, end,1); }    }    /// <summary>    ///call this method directly when you need to use a fade effect to load the scene. /// </summary>    /// <param name= "name" >The scene name. </param>     Public Static voidLoadlevel (stringname) {_scene=name; _ishide=true; }    /// <summary>    ///setting the current object cannot be destroyed. /// </summary>    voidAwake () {Dontdestroyonload ( This. Transform.gameobject); }}

Call the Loadscene.loadlevel () method directly when you need to use the fade effect to load the scene.

Unity3d 4.61 implements a fade-out scene transition method.

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.