x


Fade In/Out Login GUI?

Hello!

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

more ▼

asked May 22 '10 at 02:45 PM

user-1846 (google) gravatar image

user-1846 (google)
216 6 6 13

Do you mean a GUITexture or a texture drawn using Unity GUI?

May 22 '10 at 04:52 PM equalsequals
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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 () 
{
    print(_alpha);

    if(fadeIn)
    {
        _alpha = Mathf.Lerp(_alpha, 1F, Time.deltaTime);
        gt.color = new Color(.5F,.5F,.5F,_alpha);
        if(_alpha > .98F) fadeIn = false;
    }
    else
    {
        _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.

[Edit]

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.

==

more ▼

answered May 22 '10 at 05:23 PM

equalsequals gravatar image

equalsequals
4.5k 16 28 64

Thanks, but I meant GUI created with the GUI class in scripting. This is great, thanks for the help, but its not exactly what I needed... Thanks though for the help!

May 22 '10 at 05:36 PM user-1846 (google)

GUI meaning like text boxes and buttons :)

May 22 '10 at 05:37 PM user-1846 (google)

Ah I see, thats a misunderstanding due to lexicon. I'll post another answer.

May 22 '10 at 06:17 PM equalsequals

On second thought I'll just revise my previous answer. Cheers.

May 22 '10 at 06:18 PM equalsequals

Very Nice man!

Jun 27 '11 at 07:33 AM amir_bobo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3791
x3688
x1369
x189
x81

asked: May 22 '10 at 02:45 PM

Seen: 7077 times

Last Updated: Feb 12 '12 at 04:34 PM