Fade In/Out Login GUI?


I was wondering - has anybody figured out how to literally fade on a GUI? I really need this for my login script....

Seems obvious don't it?

Thanks! Christian Stewart

To fade a GUITexture object:

public class FadeTexture : MonoBehaviour {

private GUITexture gt;
private float _alpha = 1F;
private bool fadeIn = false;

// Use this for initialization
void Start () 
    gt = (GUITexture) gameObject.GetComponent(typeof(GUITexture));


// Update is called once per frame
void Update () 

        _alpha = Mathf.Lerp(_alpha, 1F, Time.deltaTime);
        gt.color = new Color(.5F,.5F,.5F,_alpha);
        if(_alpha > .98F) fadeIn = false;
        _alpha = Mathf.Lerp(_alpha, 0F, Time.deltaTime);
        gt.color = new Color(.5F,.5F,.5F,_alpha);
        if(_alpha < .01F) fadeIn = true;

Just apply that script to a Game Object that has a GUITexture component and it'll fade in and out. Modify it to do whatever you need it to do. I'd suggest possibly wrapping the fade inside of a coroutine instead of Update.


To do something similar with OnGUI(), add this to the above code:

void OnGUI()
    GUI.color = new Color(.5F,.5F,.5F,_alpha);
    GUI.TextField(new Rect(0,0,300,30), "This will fade.");

    GUI.color = new Color(.5F,.5F,.5F,1F);
    GUI.TextField(new Rect(0,80,300,30), "This will not.");

Also take a look at the other 2 properties that are similar to GUI.color in the docs to isolate more specific parts of your GUI. Since OnGUI is immediate mode and procedural you can see that just redefining the color property later on in the script will produce different results in the same pass.

Hope that helps.