- Home /
Problem is not reproducible or outdated
Making Images Fade In and Out
Basically, I'm working with code that changes a variable to set what image is being displayed based on a certain prompt.
That's working fine, but I want to make it so that, when I switch to a new image, the old image fades out and the new image fades in.
But I don't know where to start. How do I make stuff fade in and fade out?
It depends on what the "image" actually is. Please elaborate.
I suspect he's talking about textures displayed in the GUI, and he wants to fade between them like slides in a powerpoint presentation, or something. I think the easiest way to do that is to use GUITextures; they come with a color whose alpha you can gradually decrease and increase when you fade them in and out. I saw somewhere that people think GUI.DrawTexture is more convenient, but I couldn't confirm whether that method allows you to change the texture's alpha in an efficient way. I don't think it does.
Yes, but what kind of GUI...OnGUI code, GUITexture, texture on a plane....
@Christian Efficiency is subjective to an extend I guess, but you can easily change the alpha value using GUI.DrawTexture. You will just have to say: "GUI.color.a = value", before rendering the texture.
@OrangeLightning: Oh, right. :) I'd forgotten about GUI.color. Yeah, I guess you could use that.
Answer by pedronaroga · Feb 25, 2012 at 03:11 AM
There are two ways I can think of doing that. The first one, you'll do it manually. The other one, which I would recommend, would be using iTween (download here), and setting the alpha value of the images using iTween.ValueTo. There goes an example:
class FadeMaterials : MonoBehaviour {
public void FadeOut() {
iTween.ValueTo(gameObject, iTween.Hash(
"from", 1.0f, "to", 0.0f,
"time", 3f, "easetype", "linear",
"onupdate", "setAlpha");
}
public void FadeIn() {
iTween.ValueTo(gameObject, iTween.Hash(
"from", 0f, "to", 1f,
"time", 3f, "easetype", "linear",
"onupdate", "setAlpha");
}
public void setAlpha(float newAlpha) {
foreach (Material mObj in renderer.materials) {
mObj.color = new Color(
mObj.color.r, mObj.color.g,
mObj.color.b, newAlpha);
}
}
}
Now, whenever you want to fade out a gameobject, just call
gameobject.GetComponent<FadeMaterials>().FadeOut();
Obviously, to fade in another gameobject, just call:
gameobject.GetComponent<FadeMaterials>().FadeIn();
You can change the parameter "time" on the iTween calls, to change the fading time duration. You can also change the "easetype", to any acceptable iTween easetype values. You can find them here.
Please keep in mind that not all shaders support alpha. I'd suggest changing your materials' shaders to their equivalents in the Transparent section. For example, if you are using a Diffuse shader, change it to Transparent/Diffuse. If you are using a VertexLit shader, change it to Transparent/VertexLit.
Also, this will only work for 3D gameobjects in your scene. If you want to fade in/out items in your GUI, it'll be somewhat trickier, for you cannot change a texture alpha without using SetPixels (or so I think, I'm not sure).
I did not test the previous code. Just thought it'd point you in the right direction, even if theres an eventual sintax error.
Hope it helps.
If it can't alter the Texture2D stuff, which is what I need faded, that entire suggestion doesn't touch upon what I need.
Answer by James Tima · Feb 25, 2012 at 03:10 AM
You Can play with alpha value to create effect fade in fade out
You just repeated what everyone else said, but with less information.
Answer by devPhil · Feb 25, 2012 at 03:09 AM
Maybe this is what you're looking for?
http://answers.unity3d.com/questions/10511/change-alpha-on-individual-texture2ds.html
Answer by kievar1983 · Feb 24, 2012 at 04:03 PM
i found this to be helpful.. it might do exactly what you are looking to do..
public var theTexture : Texture2D;
private var alpha : float = 0;
var duration: float = 1;
function Start(){
yield FadeIn();
yield FadeOut();
yield NextLevel();
}
function FadeIn(){
var d = 0.3f / duration;
while( alpha < 1 ){ alpha += Time.deltaTime * d; yield; }
}
function FadeOut(){
var d = 0.3f / duration;
while( alpha > 0 ){ alpha -= Time.deltaTime * d; yield; }
}
function NextLevel(){
yield FadeOut();
Application.LoadLevel(1);
}
function OnGUI(){
GUI.color = Color(1,1,1, alpha );
GUI.DrawTexture(Rect(0,0,Screen.width, Screen.height), theTexture);
}
if you aren't loading a level get rid of the next level function. This is also done in javascript.
I converted it to C# and this line:
GUI.color = Color(1, 1, 1, alphaLeft);
Got me this error:
error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected
What now?
learn javascript.. it works cause i've used it for multiple faders of a 2d texture
Answer by dentedpixel · Dec 06, 2012 at 09:07 PM
You can use LeanTween to fade an object, here is an example:
var time:float = 2.0;
var alphaVal:float = 0.0;
LeanTween.alpha(gameObject, alphaVal, time);
Just make sure that the object has a shader that supports transparency! I would recommend Owl Labs Shaders: http://owlchemylabs.com/content/ (Unlit with Alpha);
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to convert texture2D to image in C# 3 Answers
Distribute terrain in zones 3 Answers
How do I iterate over a large Texture2d quickly? 1 Answer
Selection Grid 1 Answer