Screen Fader Problem
I have a problem with a script that should run the Fade In and Fade Out between the start and the end of the scene. The script is this:
using UnityEngine;
using System.Collections;
public class Fade_Screen_Scene : MonoBehaviour
{
public float fadeSpeed = 5.0F;
public GUITexture blackScreen;
private bool sceneStarting = true;
void Awake()
{
blackScreen.pixelInset = new Rect (0f, 0f, Screen.width, Screen.height);
}
void Update()
{
if (sceneStarting == true)
{
StartScene();
}
}
void FadeToClear()
{
blackScreen.color = Color.Lerp(blackScreen.color, Color.clear, fadeSpeed * Time.deltaTime);
}
void FadeToBlack()
{
blackScreen.color = Color.Lerp(blackScreen.color, Color.black, fadeSpeed * Time.deltaTime);
}
void StartScene()
{
FadeToClear();
if (blackScreen.color.a == 0.0f)
{
blackScreen.color = Color.clear;
blackScreen.enabled = false;
sceneStarting = false;
}
}
public void EndScene()
{
blackScreen.enabled = true;
FadeToBlack();
}
}
This script is put in a game object with a GUI Texture component
When the scene starts, a custom texture (in this case a black screen) Fade In, from black to clear. But when I press a trigger that enables the end of the scene, and calls the public void EndScene (), the texture DON'T Fade Out. I don't understand why and I tried so much. Please help me.
Answer by Landern · Feb 11, 2016 at 02:22 PM
It's because the EndScene gets called once, does a single step to fade and stops. When you're doing your StartScene method you support the continous calls each frame in the Update method that will show each step during a frame because of the method in the Update. Once faded(alpha set to 0) you're setting sceneStarting to false which will stop the StartScene method from being called in the Update Method.
You need to do something similar for your fade out so it can be called each frame until alpha is 1 or whatever you determine.
Thanks so much! I didn't see this important detail and now I fixed it. Here the new script:
public float fadeSpeed = 5.0F;
public GUITexture blackScreen;
public float delayEnding = 2.0F;
private bool sceneStarting = true;
private bool sceneEnding = false;
private IEnumerator coroutine;
void Awake()
{
blackScreen.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
}
void OnGUI()
{
if (sceneStarting == true && sceneEnding == false)
{
StartScene();
}
else
{
EndScene();
}
}
void FadeToClear()
{
blackScreen.color = Color.Lerp(blackScreen.color, Color.clear, fadeSpeed * Time.deltaTime);
}
void FadeToBlack()
{
blackScreen.color = Color.Lerp(blackScreen.color, Color.black, fadeSpeed * Time.deltaTime);
}
void StartScene()
{
FadeToClear();
if (blackScreen.color.a == 0.0f)
{
blackScreen.color = Color.clear;
blackScreen.enabled = false;
sceneStarting = false;
}
}
void EndScene()
{
blackScreen.enabled = true;
FadeToBlack();
if (blackScreen.color.a == 1.0f)
{
blackScreen.color = Color.black;
blackScreen.enabled = false;
}
}
public void CallEndScene()
{
coroutine = waitForEndScene(delayEnding);
StartCoroutine(coroutine);
}
public IEnumerator waitForEndScene(float delayEnding)
{
yield return new WaitForSeconds(delayEnding);
sceneEnding = true;
StopCoroutine(coroutine);
}
Your answer
Follow this Question
Related Questions
Animation Not Playing 0 Answers
How to fade in and out two panels in a scene? (c#) 1 Answer
How to fade in and out a image in a loop?,How do I fade in and out image on a loop? 0 Answers
Audio Fade In / Fade Out with Trigger 0 Answers
How do I use Mechanim to animate UI Image fade in and fade out? 0 Answers