Fade In and then back Out

var fadeDuration:float=0.5;
private var timeLeft:float=0.5;
var goforit = true;
 
function Awake () {
   timeLeft = fadeDuration;
}

function Update () {
	if (guiElement.color.a >= 0.5) {
		goforit = false;
		fade (false);
	} else if (goforit == true) {
		fade (true);
	}
}
 
function fade(direction:boolean){
   var alpha;
   if (direction){
      if (guiElement.color.a < 0.5){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiElement.color.a=0.5-(alpha/2);
      } else {
         timeLeft = fadeDuration;
      }
   } else {
      if (guiElement.color.a > 0){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft*fadeDuration);
         guiElement.color.a=0+(alpha/2);
      } else {
         timeLeft = fadeDuration;
      }
   }
}

As you can see, I’ve tried to edit the Wiki “FadeIn” code to include fading back out once at full opacity with minimum success. What should I do to this?

var fadeDuration:float=0.5;
private var timeLeft:float=0.5;
var goforit = true;
 
function Awake () {
   timeLeft = fadeDuration;
}

function Update () {
   if (goforit == true) {
      fade (true);
   } else {
      fade (false);
   }
}
 
function fade(direction:boolean){
   var alpha;
   if (direction){
//fade in
      if (guiElement.color.a <= 0.5){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiElement.color.a+=(alpha/2);
      } else {
         goforit = false;
         timeLeft = fadeDuration;
      }
   }
//fade out
      else {
      if (guiElement.color.a >= 0){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft*fadeDuration);
         guiElement.color.a-=(alpha/2);
      } else {
         timeLeft = fadeDuration;
      }
   }
}

dunno. too tired and don’t have unity running. hope it helps tho…

Pete’s script will work, but it can be even easier than that. The original script will do both: fade in and fade out, you just need to set the GUItexture’s Alpha setting in the inspector to “0” and then pass fade(true) to fade it in and fade(false) to fade it back out. HTH

Thats what I thought the original script should do, but when I used it, it just cut straight to black instead of fading out. O_o Pete’s script doesn’t work either. It just flashes the GUItexture on screen for a frame or two and then goes away completely.

.5/.5 = 1 = alpha, alpha/2 = .5, so color.a += .5 happens in the first frame. make fadeDuration a higher number, maybe between 5.0 to 10.0?

some event that sets goforit back to true will restart the fade function.

I modified ZeroFractal’s script to serve my purposes, this should work for you. My version allows you to set a pre-fadein delay, fade duration, and then open the next scene.

var fadeDuration:float = 0.5;
var initialDelay:float = 5;
var displayDelay:float = 10;
var nextSceneDelay:float = 5;
var nextScene = "MyNextScene";
private var timeLeft:float = 0.5;
 
function Awake () {
   timeLeft = fadeDuration;
}


function Update () {
	if (initialDelay > 0) {
		initialDelay = initialDelay-Time.deltaTime;
	}
	else {
		fade(true);
		if (displayDelay > 0) {
			displayDelay = displayDelay-Time.deltaTime;
		}
		else {
			fade(false);
				if (nextSceneDelay > 0) {
					nextSceneDelay = nextSceneDelay-Time.deltaTime;
				}
				else {
					Application.LoadLevel (nextScene);
				}
		}
	}
}
 
function fade(direction:boolean){
   var alpha;
   if (direction){
      if (guiTexture.color.a < 0.5){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiTexture.color.a=0.5-(alpha/2);
      } else {
         timeLeft = fadeDuration;
      }
   } else {
      if (guiTexture.color.a > 0){
         timeLeft = timeLeft - Time.deltaTime;
         alpha = (timeLeft/fadeDuration);
         guiTexture.color.a=0+alpha/2;
      } else {
         timeLeft = fadeDuration;
      }
   }
}

Er, that script goes to the specified level as soon as DisplayDelay is done.

Yeah, you’re right. You need to add another nested “if” loop to wait for the last delay. I’ll mod the script first chance I get and repost…

Oh, I see. :stuck_out_tongue: Never mind, I figured it out. Thanks!

The posted script was edited and should work now…

FYI, I created a C# script to also fade in, out receive notifications when that happens.

using UnityEngine;
using System.Collections;

public class Fade : MonoBehaviour 
{
	public float	  mDuration		= 1f;
	public float	  mDisplayDelay	= 0.5f;
	public Color	  mColor = Color.black;
	public GameObject mListener;
	
	enum State
	{
		FADING_IN,
		FADING_WAIT,
		FADING_OUT
	}
	
	private float     mTimeLeft = -666f;
	private State	  mState = State.FADING_IN;

	void Update() 
	{
		if (mTimeLeft < -600f)
		{
			// start fading
			guiTexture.enabled = true;
			mTimeLeft = mDuration;
			if (mListener)
				mListener.BroadcastMessage("FadeStart", null, SendMessageOptions.DontRequireReceiver);
			guiTexture.color = new Color(mColor.r, mColor.g, mColor.b, 0f);
		}
		switch (mState)
		{
			case State.FADING_IN:
				mTimeLeft -= Time.deltaTime;
				if (mTimeLeft <= 0)
				{
					mState = State.FADING_WAIT;
					mTimeLeft = 0;
					if (mListener)
						mListener.BroadcastMessage("FadeMiddle", null, SendMessageOptions.DontRequireReceiver);
				}
				guiTexture.color = new Color(mColor.r, mColor.g, mColor.b, 0.5f * ((mDuration - mTimeLeft) / mDuration));
				break;
			case State.FADING_WAIT:
				mTimeLeft -= Time.deltaTime;
				if (mTimeLeft <= -mDisplayDelay)
				{
					mState = State.FADING_OUT;
					mTimeLeft = mDuration;
				}
				break;
			case State.FADING_OUT:
				mTimeLeft -= Time.deltaTime;
				if (mTimeLeft <= 0)
				{
					if (mListener)
						mListener.BroadcastMessage("FadeEnd", null, SendMessageOptions.DontRequireReceiver);
					guiTexture.enabled = false;
					Destroy(this);
					return;
				}
				guiTexture.color = new Color(mColor.r, mColor.g, mColor.b, 0.5f * ((1f - (mDuration - mTimeLeft) / mDuration)));
				break;
		}
	}
}

Then I have a singleton GuiManager class with the GUITexture object as a property, with this method:

	public void DoFade(float theDuration, float theDisplayDelay, Color theColor, GameObject theListener)
	{
		Fade fade = (Fade)mFader.gameObject.AddComponent("Fade");
		fade.mDuration = theDuration;
		fade.mDisplayDelay = theDisplayDelay;
		fade.mColor = theColor;
		fade.mListener = theListener;
	}

May seem overkill, but then I’m able to do from anywhere in my code, this:

GuiMgr.mInst.DoFade(1f,1f,Color.red,gameObject);

And I get a nice fade and receive events of it in this very same GO.

EDIT: By the way, it would be great to be able to do something like guiTexture.color.a = X from C#

manuelflara thanx for sharing code. Your code worked great for me. I’m accessing your c# Fade class from javascript file. But, your approach for accessing C# Fade class is looking more appropriate. Can you tell more about singleton GuiManager here?

http://forum.unity3d.com/viewtopic.php?p=130131#130131