- Home /
Make a fade in/out inside 'upDate()' function
I'm new using Unity, and I want to make a fade in/out to my loading screen scene I've made. My code is the following:
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (loadedScene == false) {
loadedScene = true;
StartCoroutine( HandleIt() );
StartCoroutine (LoadNewScene ());
} else {
FadeIn ();
if (currentAmount < 100) {
currentAmount += speed * Time.deltaTime;
TextIndicator.GetComponent<Text>().text = ((int)currentAmount).ToString () + "%";
} else {
TextIndicator.gameObject.SetActive (false);
complete = true;
FadeOut ();
}
progressBar.GetComponent<Image>().fillAmount = currentAmount / 100;
}
}
// The coroutine runs on its own at the same time as Update() and takes an integer indicating which scene to load.
IEnumerator LoadNewScene() {
while (complete == false) {
// This line waits for 3 seconds before executing the next line in the coroutine.
// This line is only necessary for this demo. The scenes are so simple that they load too fast to read the "Loading..." text.
yield return new WaitForSeconds(1);
}
yield return new WaitForSeconds(0.5f);
// Start an asynchronous operation to load the scene that was passed to the LoadNewScene coroutine.
AsyncOperation async = SceneManager.LoadSceneAsync(2);
// While the asynchronous operation to load the new scene is not yet complete, continue waiting until it's done.
while (!async.isDone) {
yield return null;
}
}
void FadeOut() {
fadeOverlay.CrossFadeAlpha(1, 0.4f, false);
}
/* I guess this starts the Fade Image at the beginning...xD */
void FadeIn() {
fadeOverlay.CrossFadeAlpha(0, 0.3f, true);
}
private IEnumerator HandleIt()
{
FadeIn ();
yield return new WaitForSeconds( 0.3f );
}
As you can see I try different things for the fade in and for the fade out, but none works...when I run it I see no fade in/out at all...So, can anyone help me? I'd be very grateful!
Comment
Your answer
Follow this Question
Related Questions
Fade In / Out not working C# 1 Answer
Weird behaviour when coroutine fader is called in the middle of a fade 0 Answers
coroutines : trouble editing and accessing the same public variable 4 Answers
convert timer from update() to coroutine or invokerepeating 0 Answers
Coroutine - events not working 2 Answers