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#