I want the scene to fade in from black to the actual scene. I have this code down here:
using UnityEngine;
using System.Collections;
public class FadeScript : MonoBehaviour
{
public static Texture2D Fade;
public bool fadingOut = false;
public float alphaFadeValue = 1;
public float fadeSpeed = 2;
public Texture2D blackscreen;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void OnGUI ()
{
alphaFadeValue -= Mathf.Clamp01(Time.deltaTime / 5);
GUI.color = new Color(0, 0, 0, alphaFadeValue);
GUI.DrawTexture( new Rect(0, 0, Screen.width, Screen.height ), blackscreen);
}
}
blackscreen is a completely black image by the way.
Anyway, the code doesn’t work the screen starts out white (not black) and after a second it immediately turns to the scene. What’s wrong here? Thank you!
rutter
2
I think the issue is with the way you’re keeping track of time. This works fine, for example:
public class Fade : MonoBehaviour {
Texture2D tex;
public Color texColor = Color.black;
public Color startColor = new Color(0f, 0f, 0f, 0f);
public Color endColor = new Color(0f, 0f, 0f, 1f);
public float duration = 2f;
bool show;
float timer;
void Start() {
//generate texture from scratch (optional)
const int size = 512;
tex = new Texture2D (size, size);
for (int i=0; i<size; i++) {
for (int j=0; j<size; j++) {
tex.SetPixel(i, j, texColor);
}
}
tex.Apply();
//call function in 3 seconds
Invoke("StartFade", 3f);
}
void StartFade() {
timer = 0f;
show = true;
}
void Update() {
timer += Time.deltaTime;
}
void OnGUI() {
if (show) {
float percent = Mathf.Clamp01 (timer / duration);
GUI.color = Color.Lerp (startColor, endColor, percent);
GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), tex);
}
So it turned out that my orignal code did work. The white screen was just my level loading and unfortunately it was covering up my fade animation.