- Home /
How to "fade out" a scene
How I can make a scene go to another one, but by making the screen darker and darker... I want it to be smooth
Answer by Griffo · Nov 01, 2012 at 03:59 PM
Drop a black texture in the inspector and place script on the camera.
#pragma strict
// FadeInOut
var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;
private var alpha = 1.0;
private var fadeDir = -1;
function OnGUI(){
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
GUI.color.a = alpha;
GUI.depth = drawDepth;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}
The idea is definitely there. $$anonymous$$y concern is about OnGUI(). This function is called once or more each frame. Here you are calling it every frame for nothing. You might use a custom function with a black GUITexture that is called on Start and when leaving only.
What about something like ..
private var fade : boolean;
function OnGUI(){
if (fade){
// GUI code goes here
}
}
Well, what about:
void Start(){
FadeIn();
}
void Update(){
if(levelDone){
//all kind of stuff
FadeOut();
Application.LoadLevel(next);
}
}
Calling a function means a jump from the program to a special location in memory where the function is stored. It also involves some data movements such as storage of the program counter, of the this pointer onto the stack and any other parameters. OnGui is called at least once per frame so this would happen at least once per frame. A boolean check is about a couple of cycles. Nonetheless, your way works perfectly as well. I just mentioned there are also other ways.
$$anonymous$$y way was the only way that came to $$anonymous$$d and I really appreciate your input, now I've read your comments I agree it's a waste of time to call it if not needed, optimisation .. every little helps I suppose.
Answer by Griffo · Nov 01, 2012 at 04:17 PM
That script will fade in .. to fade out use this one
#pragma strict
// FadeInOut
var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;
private var alpha = 0.0;
private var fadeDir = -1;
function OnGUI(){
alpha -= fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
GUI.color.a = alpha;
GUI.depth = drawDepth;
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
}
Wow, thanks so much for this. It's by far the best Fade In/Out script! All the others I've found are way too complex! Thanks again.
Hy $$anonymous$$r Griffo,this work perfectly in javascript but i tried several times to change this code to C# and doesnt work ,you know why ? Thanks.
@Griffo, how can I make the fade speed longer? I tried putting it to very high numbers but it seams to be that only 0.9 is the highest. Is that true?
You could just change line 14 to
alpha += fadeDir * fadeSpeed * (Time.deltaTime*0.2);
And change the last value to what you want.
Answer by shohan4556 · Oct 03, 2016 at 05:59 AM
I would like to post here the updated script that will work in unity-5.x
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class FadeScreen : MonoBehaviour {
public Texture2D fadeTexture;
[Range(0.1f,1f)]
public float fadespeed;
public int drawDepth = -1000;
private float alpha = 1f;
private float fadeDir = -1f;
// Use this for initialization
void Start () {
}
void OnGUI() {
alpha += fadeDir * fadespeed * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
Color newColor = GUI.color;
newColor.a = alpha;
GUI.color = newColor;
GUI.depth = drawDepth;
GUI.DrawTexture( new Rect(0,0, Screen.width, Screen.height), fadeTexture);
}
}
Answer by n_soufiani · May 26, 2014 at 12:40 AM
I managed to convert it into c# so if anyone is interested, here it is:
public bool fade; public Texture2D fadeTexture; public float fadeSpeed = 0.2f; public int drawDepth = -1000; private float alpha = 1.0f; private int fadeDir = -1; if (fade) { alpha += fadeDir * fadeSpeed * Time.deltaTime; alpha = Mathf.Clamp01 (alpha); // GUI.color = new Color() { a = 0.5f }; GUI.color = new Color() {a=0.8f}; GUI.depth = drawDepth; GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), fadeTexture); }
That doesn't really work, just stays stuck on black but it doesn't actually fade. Try this ins$$anonymous$$d for C#...
if (isFadingOut)
{
alpha -= fadeDir * fadeSpeed * Time.deltaTime;
alpha = $$anonymous$$athf.Clamp01(alpha);
Color thisAlpha = GUI.color;
thisAlpha.a = alpha;
GUI.color = thisAlpha;
GUI.depth = drawDepth;
GUI.DrawTexture(new Rect(0, 0, Screen.width, Screen.height), fadeImage);
}
Answer by BDX777 · May 26, 2014 at 12:56 AM
You could try lerping the alpha of a blank black GUITexture from invisible to visible, but I'm not sure how you would go about doing that.
Your answer
Follow this Question
Related Questions
Why doesn't my players camera work and why does it spaz out randomly 1 Answer
Picture-in-Picture Effect 1 Answer
How do I take a high quality screen shot from my scene view camera? 5 Answers
How to render all opaque meshes with the same effect? 1 Answer
Why does Viking Village Water move with camera in Editor? 0 Answers