- Home /
Changing opacity of a GUITexture element
Hi,
I'm very new to Unity, and am looking to 'pulse' (fade in & out repeatedly) a GUITexture or GUIText element.
Can anybody give me any pointers on how I might achieve this? Searched for answers here but couldn't find much.
Cheers
Answer by JoshOClock · Dec 17, 2010 at 10:00 PM
You want to change the Alpha of the Color.
http://unity3d.com/support/documentation/ScriptReference/Color-a.html
Using a sin wave the code would be something like this in c#:
void OnGUI()
{
Color textureColor = guiTexture.color;
textureColor.a = Mathf.Sin(Time.time * 5.0f);
guiTexture.color = textureColor;
}
Just make the 5.0f higher for a faster rate.
Wouldn't GetComponent.<GUITexture>().color.a = someValue;
work as well?
Answer by Rasmus Schlnsen · Sep 29, 2010 at 10:27 AM
You should look into iTween. It's a very nice tweening library for Unity and you could use it for fading. There is even built in function for this. I'm linking to an answer I gave earlier. You can find it here
Thanks for the quick response.
Having delved a bit further into Unity itself I've discovered the Animation View and the ability to animate position, alpha, rotation properties etc. using a timeline.
Co$$anonymous$$g from a Flash background this seems a bit more intuitive to me. Are you aware of any arguments against using the animation view?
I have a 15 year background in Flash (and I'm the developer behind iTween) and all I can say is "whatever floats your boat". Neither approach is better or worse except that iTween and code based animation systems allow for quicker tweaking and control. Good luck.
Answer by s4vi0r · Dec 17, 2010 at 09:43 PM
You could use interpolation.
Ex.
// Converts a white color to a black one trough time. var lerpedColor : Color = Color.white; function Update() { lerpedColor = Color.Lerp(Color.white, Color.black, Time.time); }
Take this idea now merge it with a guiTexture.color
function Update() { guiTexture.color = Color.Lerp(guiTexture.color, Color.red, Time.time); }
Another idea would be to use something like a sine wave. Might be even better option, since it would be infinite.