- Home /
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
Do you mean a GUITexture or a texture drawn using Unity GUI?
Answer by equalsequals · May 22, 2010 at 05:23 PM
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.
==
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!
Ah I see, thats a misunderstanding due to lexicon. I'll post another answer.
On second thought I'll just revise my previous answer. Cheers.
Your answer
Follow this Question
Related Questions
Who Killed Who - Scrolling GUI Text 2 Answers
Play one animation and rewind another 0 Answers
How to hook up the particle system to jump animation? 1 Answer
Getting animations to play relative to player's direction? 4 Answers
Change animation of 4 Way Movement Sprites moving diagonally to last used non-diagonal direction? 0 Answers