- Home /
Repeat fade in and out
Hi everyone
I have a GUITexture that I want to fade in and out continue. When the GUITexture fades out, it needs to fade in again and then it has to to fade out again, and this should repeat everytime.
But the problem is that the Script I wrote fades the GUITexture once in and out.
This is the Script
private var alpha : float = 0;
var myTexture:Texture2D;
var myTextureWidth:float = 200;
var myTextureHeight:float = 100;
var duration: float = 1;
function Start(){
yield FadeIn();
yield FadeOut();
}
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 OnGUI(){
GUI.color = Color(1,1,1, alpha );
GUI.DrawTexture(Rect( (Screen.width - myTextureWidth)*0.5,(Screen.height - myTextureHeight)*0.5, myTextureWidth, myTextureHeight), myTexture);
}
Is there someone who can help me out to make the GUITexture fades in and out continue.
Thanks in advance
Comment
Best Answer
Answer by robertbu · Apr 23, 2013 at 04:00 PM
To repeat the cycle over and over, try Mathf.PingPong():
#pragma strict
var myTexture:Texture2D;
var myTextureWidth:float = 200;
var myTextureHeight:float = 100;
var duration: float = 1;
function OnGUI(){
GUI.color = Color(1,1,1, Mathf.PingPong(Time.time, duration)/duration);
GUI.DrawTexture(Rect( (Screen.width - myTextureWidth)*0.5,(Screen.height - myTextureHeight)*0.5, myTextureWidth, myTextureHeight), myTexture);
}