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.