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!
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
See this answer for a coroutine example:
You’ll need to decide whether to disable the renderer though.