- Home /
How can I individually fade the background (and not the GUI)?
Hey all,
I'm trying to make a script that will allow me to fade the background. I've managed to find and edit one, namely this:
public var fadeOutTexture : Texture2D; public var fadeSpeed : float = 0.3; var drawDepth : int = -1000; private var alpha = 1.0; private var fadeDir : int = -1; private var screenwidth = Screen.width; private var screenheight = Screen.height;
function OnGUI(){
alpha += fadeDir * fadeSpeed * Time.deltaTime;
alpha = Mathf.Clamp01(alpha);
GUI.color.a = alpha;
GUI.depth = drawDepth;
GUI.DrawTexture(Rect(0, 0, screenwidth, screenheight), fadeOutTexture);
}
function fadeIn(){ fadeDir = -1;
Debug.Log ("ScreenFade : Fading In"); }
function fadeOutIn(waitTime : float){ fadeOut(); Debug.Log ("ScreenFade : Fading Out and In"); yield WaitForSeconds (waitTime); fadeIn(); }
function fadeOut(){ fadeDir = 1;
Debug.Log ("ScreenFade : FadingOut"); }
function Start(){ alpha=1; fadeIn(); }
It works very well, but what I would like to is fade everything out, except for the GUI. I've tried managing this with GUI.Depth, but I read somewhere on the forum that the GUI is always on top, no matter what.
I'm drawing a background with DrawTexture (which I change dynamically throughout the game), so it's part of the GUI, and that means it either all fades, or nothing does.
Is there a way for me to assign a lower/higher depth to the individual DrawTexture, so I can isolate it (read: fade only that DrawTexture)? I guess what I need, is for this fade texture to get above the DrawTexture, but under the other GUI elements.
Thanks in advance,
Veliremus
Answer by Mike 3 · May 07, 2010 at 10:38 AM
it doesn't all have to fade - if you change GUI.color to a faded value, call the DrawTexture function, then change GUI.color to Color.white, it'll fade only that single object
edit:
GUI.DrawTexture(Rect(0, 0, screenwidth, screenheight), backgroundTexture);
GUI.color = new Color(1,1,1,0.5); GUI.DrawTexture(Rect(0, 0, screenwidth, screenheight), fadeOutTexture); GUI.color = Color.white;
GUI.DrawTexture(Rect(100, 100, 100, 100), anotherTexture);
that will draw a background, followed by a faded texture, followed by a smaller object on top of it
I don't quite follow, can you please give me a script example?
Thanks for the quick response. But that keeps the texture faded, right? What I mean is actually fading in and out. For example, I want my game to switch scenes (not Unity scenes) by fading out and into the new scene, but without fading the GUI out and in.
Same idea - you just need to change the alpha value for the GUI.color for each texture you're fading out/in
But doesn't this take out the animation? $$anonymous$$aybe I'm acting like a noob, but I don't understand how I would call my fade function from another script. To eloborate, I write all my scenes in X$$anonymous$$L, and my parser reads the tags. When it sees example for instance, it draws the background. The same thing would happen with the fades, but I don't see how I can do this. Sorry, very inexperienced with Javascript.