hello
I want to create loading scene like “Now Loading…”.
I researched and found “Application.LoadLevelAsync” finction, but it is only pro edition.
do you know another way?
something like this.
#using UnityEngine;
// fades in a image ( use a white texture for just a color )
// loads the level.
// fades out the image.
public class LoadTransition : public MonoBehaviour {
public string levelIndexOrName = "0";
public bool isIndex = true;
public float fadeOutSeconds;
public float fadeInSeconds;
public int guiDepth = -10;
public Texture image;
public Color imageOpaqueColor = Color.White;
public Color imageClearColor = new Color(1f,1f,1f,0f);
private float percentFadeInOrOut = 0f;
private bool fadingIn = false;
private float showingPercent { get { return fadingIn ? ( 1f - percentFadeInOrOut ) : percentFadeInOrOut; } }
IEnumerator Fade( bool fade_in, float seconds ) {
fadingIn = fade_in; percentFadeInOrOut = 0f;
float startTime = Time.realtimeSinceStartup;
do yield return null; while( ( percentFadeInOrOut= Mathf.Min ( (Time.realtimeSinceStartup - startTime) / fadeOutSeconds, 1f ) ) < 1f );
yield break;
}
IEnumerator Start() {
DontDestroyOnLoad(gameObject);
// fade out old scene
yield return StartCoroutine( Fade( false, fadeOutSeconds ) );
// let the gui render once at complete fade.
yield return new WaitForEndOfFrame();
// to be safe wait one more frame ( you can see if you need this next line or not )
yield return null;
// load the level
if( isIndex ) Application.LoadLevel( int.Parse(levelIndexOrName) );
else Application.LoadLevel(levelIndexOrName);
// wait 2 frames for awake and start ( or more if you need )
yield return null; yield return null;
// now fade in the newly loaded scene.
yield return StartCoroutine( Fade( true, fadeInSeconds ) );
// again wait a frame for gui to pickup
yield return null;
// and were done.
Destroy( gameObject );
}
void OnGUI() {
GUI.depth = guiDepth;
if( image ) {
GUI.color = Color.Lerp( imageClearColor, imageOpaqueColor, showingPercent );
GUI.DrawTexture( new Rect(0,0,Screen.width, Screen.height), image );
}
}
static LoadTransition InstantiateManually( float secondsFade, Texture image, Color color ) {
// make our game object with component.
GameObject loaderGameObject = new GameObject( "loadTransition", typeof(LoadTransition) );
LoadTransition loader = loaderGameObject.GetComponent<LoadTransition>();
// laziness: just set in and out to the same.
loader.fadeInSeconds = secondsFade;
loader.fadeOutSeconds = secondsFade;
if( image )
loader.image = image; // only set image if its non null ( this could preserve default settings )
// set in the color
loader.imageOpaqueColor = color;
loader.imageClearColor = color;
// force alpha on opaque to be 1 and clear:0
loader.imageOpaqueColor.a = 1f;
loader.imageClearColor.a = 0f;
return loader;
}
public static LoadTransition Load( string levelName, float secondsFade, Texture image, Color color ) {
LoadTransition loader = InstantiateManuallly;
loader.isIndex = false;
loader.levelIndexOrName = levelName;
return loader;
}
public static LoadTransition Load( int levelIndex, float secondsFade, Texture image, Color color ) {
LoadTransition loader = InstantiateManuallly;
loader.isIndex = true;
loader.levelIndexOrName = levelIndex.ToString();// or System.Convert
return loader;
}
// runs until we've been destroyed.
IEnumerator WaitUntilFinishedProc() { do yield return null; while(true); }
// gets something you can wait on with yield return.
public YieldInstruction WaitUntilFinished() { return StartCoroutine(WaitUntilFinishedProc()); }
// prefab use only. Instantiates the prefab but changes the level name
public static LoadTransition InstantiateWithLevel( LoadTransition prefab, string levelName ) {
LoadTransition loader = (LoadTransition)Instantiate(prefab);
loader.isIndex = false;
loader.levelIndexOrName = levelName;
}
public static LoadTransition InstantiateWithLevel( LoadTransition prefab, int levelIndex ) {
LoadTransition loader = (LoadTransition)Instantiate(prefab);
loader.isIndex = true;
loader.levelIndexOrName = levelIndex.ToString();// or System.Convert
}
}
Just to be clear though this will not animate while the scene is loading. It just fades in a texture, loads, unfades the texture.
So instead of
Application.LoadLevel(“sceneName”) you use
LoadTransition.Load(“sceneName”, 1f, SomeWhiteTexture, Color.Black); where SomeWhiteTexture is a all white opaque texture.
Giving you a black screen.
Since you said you wanted Now Loading… what you could do is make a prefab with LoadTransition on it and some GUI script that has “Now Loading…” stuff with it. Set the GUI script to use a GUI.depth lower ( so it draws on top ). Then when you want to load levels using that prefab do LoadTransition.InstantiateWithLevel(prefab, “levelNameHere”);
You could also just put the now loading bit in the LoadTransition script.
I wrote this in the quick reply box, hope it works out for you.
Hi BDev
thank you for replying.
I will try it.
Hello
It is my fixed your code.
using UnityEngine;
using System.Collections;
// fades in a image ( use a white texture for just a color )
// loads the level.
// fades out the image.
public class LoadTransition : MonoBehaviour {
public string levelIndexOrName = "0";
public bool isIndex = true;
public float fadeOutSeconds;
public float fadeInSeconds;
public int guiDepth = -10;
public Texture image;
public Color imageOpaqueColor = Color.white;
public Color imageClearColor = new Color(1F, 1F, 1F, 0F);
private float percentFadeInOrOut = 0F;
private bool fadingIn = false;
private float showingPercent { get { return fadingIn ? ( 1F - percentFadeInOrOut ) : percentFadeInOrOut; } }
IEnumerator Fade( bool fade_in, float seconds ) {
fadingIn = fade_in;
percentFadeInOrOut = 0F;
float startTime = Time.realtimeSinceStartup;
do yield return null; while( ( percentFadeInOrOut= Mathf.Min ( (Time.realtimeSinceStartup - startTime) / fadeOutSeconds, 1F ) ) < 1F );
yield break;
}
IEnumerator Start() {
DontDestroyOnLoad(gameObject);
// fade out old scene
yield return StartCoroutine( Fade( false, fadeOutSeconds ) );
// let the gui render once at complete fade.
yield return new WaitForEndOfFrame();
// to be safe wait one more frame ( you can see if you need this next line or not )
yield return null;
// load the level
if( isIndex ) Application.LoadLevel( int.Parse(levelIndexOrName) );
else Application.LoadLevel(levelIndexOrName);
// wait 2 frames for awake and start ( or more if you need )
yield return null; yield return null;
// now fade in the newly loaded scene.
yield return StartCoroutine( Fade( true, fadeInSeconds ) );
// again wait a frame for gui to pickup
yield return null;
// and were done.
Destroy( gameObject );
}
void OnGUI() {
GUI.depth = guiDepth;
if( image ) {
GUI.color = Color.Lerp( imageClearColor, imageOpaqueColor, showingPercent );
GUI.DrawTexture( new Rect(0,0,Screen.width, Screen.height), image );
}
}
static LoadTransition InstantiateManually( float secondsFade, Texture image, Color color ) {
// make our game object with component.
GameObject loaderGameObject = new GameObject( "loadTransition", typeof(LoadTransition) );
LoadTransition loader = loaderGameObject.GetComponent<LoadTransition>();
// laziness: just set in and out to the same.
loader.fadeInSeconds = secondsFade;
loader.fadeOutSeconds = secondsFade;
if( image )
loader.image = image; // only set image if its non null ( this could preserve default settings )
// set in the color
loader.imageOpaqueColor = color;
loader.imageClearColor = color;
// force alpha on opaque to be 1 and clear:0
loader.imageOpaqueColor.a = 1F;
loader.imageClearColor.a = 0F;
return loader;
}
public static LoadTransition Load( string levelName, float secondsFade, Texture image, Color color ) {
LoadTransition loader = InstantiateManually(secondsFade, image, color);
loader.isIndex = false;
loader.levelIndexOrName = levelName;
return loader;
}
public static LoadTransition Load( int levelIndex, float secondsFade, Texture image, Color color ) {
LoadTransition loader = InstantiateManually(secondsFade, image, color);
loader.isIndex = true;
loader.levelIndexOrName = levelIndex.ToString();// or System.Convert
return loader;
}
// runs until we've been destroyed.
IEnumerator WaitUntilFinishedProc() { do yield return null; while(true); }
// gets something you can wait on with yield return.
public YieldInstruction WaitUntilFinished() { return StartCoroutine(WaitUntilFinishedProc()); }
// prefab use only. Instantiates the prefab but changes the level name
public static LoadTransition InstantiateWithLevel( LoadTransition prefab, string levelName ) {
LoadTransition loader = (LoadTransition)Instantiate(prefab);
loader.isIndex = false;
loader.levelIndexOrName = levelName;
return loader;
}
public static LoadTransition InstantiateWithLevel( LoadTransition prefab, int levelIndex ) {
LoadTransition loader = (LoadTransition)Instantiate(prefab);
loader.isIndex = true;
loader.levelIndexOrName = levelIndex.ToString();// or System.Convert
return loader;
}
}
I tried the program.
It looks non-tested program.
Did you programed without editor’s checking? How genius you are!
Application.LoadLevel("sceneName")
I tried it and it was fine!!
but…
I defined below code.
public LoadTransition prefab;
And I wanna try it, but I can’t understand how I attach LoadTransition-type “Now Loading…” stuff prefab.
LoadTransition.InstantiateWithLevel(prefab, "levelNameHere");