Fade Effects in Unity

Source: Internet
Author: User

first, the question

Want to make a transition in the scene, do not want to jump directly. The simplest thing is to do a fade-in effect.


Second, the Code

Using Unityengine;

Using System.Collections; public class Levelloadfade:monobehaviour {public static void Fadeandloadlevel (String levelname, Texture2d fadetext Ure, float fadelength) {if (null = = Fadetexture) {fadeandloadlevel (LevelName, Color.whit
        E, fadelength);
        } Gameobject fade = new Gameobject ("Fade"); Fade.
        
        Addcomponent<levelloadfade> (); Fade. Getcomponent<levelloadfade> ().            
    Dofade (LevelName, Fadelength, Fadetexture, Color.White, false); 
        public static void Fadeandloadlevel (string levelname, color color, float fadelength) {color.a = 1;
        Texture2d fadetexture = new Texture2d (1, 1);
        Fadetexture.setpixel (0, 0, color);
        Fadetexture.apply ();
        
        Dontdestroyonload (fadetexture);
        Gameobject fade = new Gameobject ("Fade"); Fade.
        Addcomponent<levelloadfade> (); Fade. Getcomponent<levelloadfaDe> ().
    Dofade (LevelName, fadelength, fadetexture, color, true);
    } private texture2d M_fadetexture;
    Private Rect m_rect;

    
    Private Color M_color;
        void Awake () {m_rect = new Rect (0, 0, screen.width, screen.height);
    M_fadetexture = null;
            } void Ongui () {if (m_fadetexture! = null) {gui.depth =-100;
            Gui.color = M_color; Gui.
        Drawtexture (M_rect, m_fadetexture); }} void Dofade (string levelname, float fadelength, texture2d fadetexture, color color, bool Destroy
        Texture) {transform.position = Vector3.zero;
        Don ' t destroy the Fade game object during level load Dontdestroyonload (gameobject);
        M_color = Color;
    
        M_fadetexture = fadetexture;
    Startcoroutine (Docoroutinefade (LevelName, fadelength, fadetexture, Color, destroytexture)); } IEnumerator DocoroutiNefade (string levelname, float fadelength, texture2d fadetexture, color color, bool destroytexture) {//Fade
        Texture in float time = 0;
            while (Time < Fadelength) {time + = Time.deltatime;
            M_COLOR.A = Mathf.inverselerp (0, 1, time/fadelength);
        yield return null;
        
        }//Fadeout to start with COLOR.A = 1;
        
        Application.loadlevel (LevelName);
        Fade texture out time = 0;
            while (Time < Fadelength) {time + = Time.deltatime;
            M_COLOR.A = mathf.inverselerp (1, 0, time/fadelength);
        yield return null;
        
        } m_color.a = 0;
        M_fadetexture = null;
        
        Destroy (Gameobject);
        if (destroytexture) {Destroy (m_fadetexture); }}//update is called once per frame void update () {}}

Here's the one with

Application.loadlevel

So, will be blocked, if the machine is poor, or will be stuck in your fade screen, if the use of asynchronous

Application.loadlevelasync

That will be the fade in and fade out together broadcast. And then get stuck. This may have to be considered, mainly asynchronous loading completed the value is not very accurate, it seems that can not be used as a percentage of completion progress judgment.

Iv. Summary

In fact, I think the effect is in the fade out of time to start loading, and then not loaded when it is stuck in the fade interface, and then fade in. But this fade in effect on the good machine effect is good, we can try.

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.