I’m sure this is probably be asked before, but does anyone know of how to fade out the GUI. I tried using a box with a style and a texture, but couldn’t set the alpha on the texture to simulate the fade. Thanks.
This always happens. I find the answer after I post the question. Oh well, I ended up using GUI.color and it worked like a charm.
Fade GUI in:
function Start () {
yield Fade(0, .5, 1); // Start, end, length in seconds
}
function Fade (start : float, end : float, length : float) {
for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
guiTexture.color.a = Mathf.Lerp(start, end, i);
yield;
}
}
Fade GUI out:
function Start () {
yield Fade(.5, 0, 1); // Start, end, length in seconds
}
function Fade (start : float, end : float, length : float) {
for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
guiTexture.color.a = Mathf.Lerp(start, end, i);
yield;
}
}
Well, that works for GUIElement objects, but for OnGUI stuff, you’d change GUI.color.a instead, as besuser mentioned. You can still make it a function, though:
private var guiAlpha : float;
function Start () {
yield GUIFade(0, 1, 1); // Start, end, length in seconds
}
function OnGUI () {
GUI.color.a = guiAlpha;
if (GUILayout.Button("Click me to fade out")) {
GUIFade(1, 0, 2);
}
}
function GUIFade (start : float, end : float, length : float) {
for (i = 0.0; i <= 1.0; i += Time.deltaTime*(1/length)) {
guiAlpha = Mathf.Lerp(start, end, i);
yield;
}
guiAlpha = end; // Accounts for Time.deltaTime variance
}
–Eric
Just an update. GUI.Color.a worked great when I’m in the IDE, but for some reason when I build into an executable, the fade stopped working. I’m using FixedUpdate to update the alpha value. Anyone else having this problem also?
I just tested the script I posted above and it works fine as a build. Not sure why you’d use FixedUpdate for something like that, though; you should use Update for anything that isn’t physics.
–Eric
I used FixedUpdate because I didn’t use the Lerp function. Anyhow, after working on it some more, I got it to work on a GUI.box that I used to mask the other gui elements behind it. Previously, I used GUI.Color.a to control all the gui elements. That for some reason didn’t work. The mask idea seems ok.
Is something like this also possible for a Box?
You need to fade a local variable and copy that to GUI.color inside each OnGUI
Hmm, sorry, don’t get it - need an example.
Especially I don’t know what the “a” is doing.
Something like this?
OKay - got it! Largly!
a is alpha - juchee…
Yep, “a” is for alpha. Like “r”, “g”, and “b” are for red, green, and blue. This controls the alpha for everything in OnGUI after you set it. GUILayout.Box doesn’t return anything, so you can’t use it in an “if” statement. GUILayout.Button returns true or false depending on whether it was clicked or not.
So use my above example, but replace OnGUI with
function OnGUI () {
if (GUILayout.Button("Click me to fade out")) {
GUIFade(1, 0, 2);
}
GUI.color.a = guiAlpha;
GUILayout.Box("This is a box that will fade");
GUI.color.a = 1;
GUILayout.Box("This won't fade");
}
–Eric
That is after the GUI.Color.a = XY; line,
all elements act like XY says till the next GUI.Color.a = blubb; is coming?!
I think I get it now…
thx Eric and nicholas
Yes.
–Eric
Hmm… BUT…
What to do in this case?
GUI.color.a = guiAlpha;
Label (Rect (15, 15, 175, 33), logo);
GUI.color.a = 1;
if (GUI.Button(Rect(10, 650, 20, 40), "<>")) {
if (guiAlpha == 1) {
GUIFade(1, 0, 2);
}
else {
GUIFade(0, 1, 2);
}
GUI.color.a = guiAlpha;
GUILayout.BeginArea (Rect (10, 635, 1014, 60), title, skin.window);
Label(Rect(5, 0, 100, 60), labeltext);
if (Button (Rect (135, 5, 25, 25), Button1)) {
Debut.Log("upwards ^");
}
GUILayout.EndArea();
}
Got some errors in the console I can’t do anything with it.
Getting control 0’s position in a group with only 0 controls when doing used
UnityEngine.GUILayout:BeginArea(Rect, String, GUIStyle)
Sub1_BauGUI:OnGUI() (at Assets/GUI/Scripts/Sub1_BauGUI.js:99)
GUILayout: Mismatched BeginArea.
UnityEngine.GUILayout:BeginArea(Rect, String, GUIStyle)
Sub1_BauGUI:OnGUI() (at Assets/GUI/Scripts/Sub1_BauGUI.js:99)
InvalidOperationException: Operation is not valid due to the current state of the object
UnityEngine.GUILayout.EndArea ()
Sub1_BauGUI.OnGUI () (at Assets/GUI/Scripts/Sub1_BauGUI.js:140)
Line 99 is BeginArea
Line 140 EndArea
Rest above is working
According to the error message, you have a mismatched BeginArea(). The code you posted wouldn’t cause it, so it must be in the rest of the code.
–Eric
Ah - OK.
Got it now! Really…
Replaced the beginarea with a simple box and moved the if-loop with the GUIFade() at the end of the OnGUI()-Function…
ole ole - it works - only needed 5-6 hours for understanding… :roll:
thank got it’s weekend
I’m trying to get this to work in C# but not having much luck… a print statement shows that the guiAlpha Lerp is not happening properly… it jumps all over the place and never ends. I’m sure there is just a flaw in my logic… can someone point it out?
public texture tutBox;
static float guiAlpha = 0.0f;
static Color guiColor = new Color(1,1,1,guiAlpha);
void OnGUI(){
guiColor = new Color(1,1,1,guiAlpha);
GUI.color = guiColor;
StartCoroutine(GUIFade(0.0f, 1.0f, 2.0f));
GUI.Box(new Rect(Screen.width/2, Screen.height/2, tutBox.width, tutBox.height), tutBox);
}
IEnumerator GUIFade (float start, float end, float length) {
print ("fade started");
for (float i = 0.0f; i <= 1.0f; i += Time.deltaTime*(1/length)) {
guiAlpha = Mathf.Lerp(start, end, i);
print (guiAlpha);
yield return new WaitForEndOfFrame();
}
guiAlpha = end; // Accounts for Time.deltaTime variance
}
If I see that right, you initialize your alpha (guiAlpha) with 0.0f. But the function is fading out, not in. So if the for loop starts your gui is already full transparent.