- Home /
Easy fade in/out on level load/end?
Hello! I'm almost finished with a game I am working on, but I would like to have a simple fade in and out for when a scene begins and ends. I've googled it and found a bunch of links on answers, the forum, and in the community wiki, but no matter what I try, none of the methods work. So, do any of you know a way of achieving a fade effect, and explain for a semi-noob? Maybe the scripts I saw did work, and I am just not applying them right, I don't know. I wish the wiki included example projects. Also, I do not have Unity Pro, I saw there is an easy method with pro but I don't have it. Thanks!
Answer by wuchiang · Jul 22, 2012 at 09:32 PM
Just make an object with a black texture on it and make a material that uses a transparency shader for it. Then you can interpolate the transparency(alpha) of the material using a script.
Here's an example script:
using UnityEngine;
using System.Collections;
public class Fading : MonoBehaviour
{
//====================================================================================================
// Member Variables
//====================================================================================================
public float StartAlpha = 1.0f; // The transparency value to start fading from
public float EndAlpha = 0.0f; // The transparency value to end fading at
public float FadingSpeed = 1.0f; // The speed of the effect
private float Timer = 0.0f; // The time passed since fading was enabled
private bool FadingOn = true; // Controls whether to fade or not
//====================================================================================================
// Custom Functions
//====================================================================================================
// Use this function to control fading using another script
// i.e.
// Fading fadingScript = fadingObject.GetComponent<Fading>();
// fadingScript.Fade(true);
public void Fade(bool fade)
{
FadingOn = fade;
}
//====================================================================================================
// Unity Functions
//====================================================================================================
void LateUpdate()
{
// Don't do anything if we are not fading
if (!FadingOn)
return;
// Increase the timer by the amount of time passed since the last frame
Timer += Time.deltaTime;
// Change the material's color, keeping RGB intact and interpolating alpha between
// StartAlpha and EndAlpha
renderer.material.color = new Color
(
renderer.material.color.r,
renderer.material.color.g,
renderer.material.color.b,
Mathf.Lerp(StartAlpha, EndAlpha, Timer * FadingSpeed)
);
// Done fading
if (renderer.material.color.a == EndAlpha)
{
// Stop fading and reset timer
Timer = 0.0f;
FadingOn = false;
// Do whatever you need to do
// i.e.: transition or destroy this object
}
}
}
Here's an example project that puts this script to use: https://dl.dropbox.com/u/9658048/Unity-Example-Projects/Fading.zip
@wuchiang curious to know why LateUpdate() as opposed to Update() ?
You can also achieve the same by creating an animation that animates the alpha value of a material within the hierarchy of the attached GameObject.
You could optionally have an Animation Event that you could use to disable the renderer on fade in - you don't want to be rendering a completely transparent polygon or two.
A good approach it to do the above in a Coroutine and the you will not be Updating() unnecessarily.
I usually do color interpolations and similar changes in LateUpdate if they're not necessary in Update, I don't think it really matters in this case though.
That animation example sounds really handy! I'll try it out sometime.
Also, if a coroutine was used wouldn't another script then be necessary to trigger this functionality? I suppose that would be better than updating unnecessarily. If you have the time, Could you show me an example of how you would use a coroutine to do this? I don't have a lot of experience with them other than using them with WaitForSeconds.
@wuchiang thank you, but there is a strange glitch. For some reason, when I try to start the level via the start screen or the game over screen, it won't load.
$$anonymous$$y code shouldn't be doing that. Are you sure it's not caused by something else?
@wuchiang $$anonymous$$aybe.... I have another problem too now. How do I get it to fade out at the end of the scene? Here are the scripts controlling it: Fading: public class Fading : $$anonymous$$onoBehaviour { ============================================ public float StartAlpha = 1.0f; // The transparency value to start fading from public float EndAlpha = 0.0f; // The transparency value to end fading at public float FadingSpeed = 1.0f; // The speed of the effect
private float Timer = 0.0f; // The time passed since fading was enabled private bool FadingOn = true; // Controls whether to fade or not private bool FadingOut = true; // Controls whether to fade or not
//==================================================================================================== // Custom Functions //====================================================================================================
// Use this function to control fading using another script // i.e. // Fading fadingScript = fadingObject.GetComponent(); // fadingScript.Fade(true); public void Fade (bool fade) { FadingOn = fade; } public void FadeOut (bool fadeO) { FadingOut = fadeO; }
//==================================================================================================== // Unity Functions //==================================================================================================== void LateUpdate () { // Don't do anything if we are not fading if (!FadingOn || !FadingOut) return;
// Increase the timer by the amount of time passed since the last frame Timer += Time.deltaTime;
// Change the material's color, keeping RGB intact and interpolating alpha between // StartAlpha and EndAlpha if (FadingOn) { renderer.material.color = new Color ( renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, $$anonymous$$athf.Lerp (StartAlpha, EndAlpha, Timer FadingSpeed) ); } if (FadingOut) { renderer.material.color = new Color ( renderer.material.color.r, renderer.material.color.g, renderer.material.color.b, $$anonymous$$athf.Lerp (EndAlpha, StartAlpha, Timer FadingSpeed) ); } // Done fading if (renderer.material.color.a == EndAlpha) { // Stop fading and reset timer Timer = 0.0f; FadingOn = false;
// Do whatever you need to do // i.e.: transition or destroy this object } }
}
Fadeout: using UnityEngine; using System.Collections;
public class FadeOut : $$anonymous$$onoBehaviour { public Transform fadeObject; bool loaded = false; // Use this for initialization IEnumerator Example() { yield return new WaitForSeconds(3); loaded = true; }
// Update is called once per frame void Update () {
if(PlayerCollision.lives < 1 && loaded){ fadeObject.GetComponent().FadeOut(true); Application.LoadLevel("GameOver"); } } } It is fading in then immeadiately fading out. I put in the yield to see if this was being caused by the lives ot loading fast eough. Thanks!
Answer by Bovine · Jul 22, 2012 at 10:26 PM
See this answer for a coroutine example:
http://answers.unity3d.com/questions/286453/quick-change-over-time-math.html
You'll need to decide whether to disable the renderer though.
Thanks a lot, that certainly does help me think of much more compact and easier ways of doing things.
Your answer
![](https://koobas.hobune.stream/wayback/20220613074539im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Fade In and out when level is loaded 2 Answers
Fade animation in 1 Answer
Fade 3d text with lerp? 1 Answer
How to fade in GUI text. 2 Answers
Assigning UV Map to model at runtime 0 Answers