- Home /
Fade out GUILayout Area?
How do you fade an entire GUILayout's area?
Here's the code I'm using:
void OnGUI() {
Rect rec = new Rect(Screen.width,Screen.height,width,height);
GUILayout.BeginArea(rec, new GUIStyle("box"));
GUILayout.Space(5.0f);
GUILayout.Label("Hello!", style);
GUILayout.EndArea();
}
I just want the whole thing to fade out smoothly.
Thanks!
Comment
Best Answer
Answer by dsada · Jul 16, 2014 at 07:14 PM
You can lerp the alpha values of GUI.color like:
void OnGUI() {
GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,Mathf.Lerp(1,0,Time.time));
Rect rec = new Rect(Screen.width,Screen.height,width,height);
GUILayout.BeginArea(rec, new GUIStyle("box"));
GUILayout.Space(5.0f);
GUILayout.Label("Hello!", style);
GUILayout.EndArea();
}
if you got other gui elements in other scripts you should set back the GUI.color to the original value at the end of OnGUI()
Be creative :)
private bool fade = false;
private float speed = 0.5f;
void OnGUI() {
if(fade)
{
GUI.color = new Color(GUI.color.r,GUI.color.g,GUI.color.b,$$anonymous$$athf.Lerp(1,0,speed * Time.time));
}
Rect rec = new Rect(Screen.width,Screen.height,width,height);
GUILayout.BeginArea(rec, new GUIStyle("box"));
GUILayout.Space(5.0f);
GUILayout.Label("Hello!", style);
GUILayout.EndArea();
}
Set the fade variable to true when you would like to start :)
You can also set the speed by multiplying Time.time with a speed value as you can see above