I want to know about script when the time countdown to zero then scene1 changed to scene2 with fade in by automatic ?
This is the tutorial that i followed to achieve that.
I created another script, called Fading and a function LoadNewLevel. I put that Fading script on a game object in every scene of mine (the scene manager)
This is the entire script :
public Texture2D fadeOutTexture;
private float fadeSpeed = 1f;
private float alpha = 1.0f;
private float fadeDir = -1f;
void Awake()
{
gameObject.SendMessage("OnLevelWasLoaded",Application.loadedLevel);
}
void Start()
{
}
void OnGUI()
{
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
GUI.color = new Color(GUI.color.r, GUI.color.g, GUI.color.b, alpha);
GUI.depth = -1000;
GUI.DrawTexture( new Rect(0,0, Screen.width, Screen.height), fadeOutTexture );
}
public float beginFade(float dir)
{
fadeDir = dir;
return fadeSpeed;
}
void OnLevelWasLoaded ()
{
beginFade(-1f);
}
IEnumerator LoadLevel(string levelName)
{
float fspeed = beginFade(1f);
yield return new WaitForSeconds(fspeed);
Application.LoadLevel(levelName);
}
public void LoadNewLevel(string levelName)
{
StartCoroutine( LoadLevel(levelName) );
}
And on the fadeOutTexture field i drag a simple black texture.
When i want to switch scenes, i find the manager object, get this component and call the loadNewLevel
ex: GameObject.Find(“SceneMaanger”).GetComponent().LoadNewLevel(“Level2”);
insead of using Application.LoadLevel(“Level2”).
This works for me. You can have your own implementation of course.
Regarding the countdown to 0, you can just use Invoke with a parameter
public void fadeToNextLevel()
{
GameObject.Find("SceneMaanger").GetComponent<Fading>().LoadNewLevel("Level2");
}
//this can be called like this : Invoke("fadeToNextLevel", 2f) //which calls the function after 2 seconds.