- Home /
Fade in and out, texture separation
Hi, I want to make a fade in and fade out animation, but I cannot seem to find a way to change the alpha of the "Fade" texture ONLY. Left like this, the code fades the "Background" texture as well and at t=2 there is nothing to see on the screen.
I know I can solve this problem by separating the two textures into separate scripts or making one of them a GUI Texture component, but I would like to have both in the same script. Any help on this matter or suggestions on the code in general will be greatly appreciated. Thank you!
///*Splash Vars*///
var Alpha:float;
/*Images*/
var Fade:Texture;
var Background:Texture;
///*Code*///
function Start()
{
audio.volume=PlayerPrefs.GetFloat("Volume");
yield FadeAnimation(1.0,0.0,2.0);
yield WaitForSeconds(2);
yield FadeAnimation(0.0,1.0,2.0);
Application.LoadLevel(Application.loadedLevel+1);
}
function OnGUI()
{
GUI.color.a=Alpha;
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),Fade);
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),Background);
}
function FadeAnimation(AlphaStart:float,AlphaEnd:float,Duration:float)
{
for (var t:float=0.0;t<1.0;t=t+Time.deltaTime*1.0/Duration)
{
Alpha=Mathf.Lerp(AlphaStart,AlphaEnd,t);
yield;
}
}
Answer by Ejlersen · Oct 19, 2013 at 08:24 PM
function OnGUI()
{
GUI.color.a=Alpha;
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),Fade);
GUI.color.a = 1.0;
GUI.DrawTexture(Rect(0,0,Screen.width,Screen.height),Background);
}
Wow, thank you so much! The worst part being I already tried this but forgot that color is a float value (1.0 ins$$anonymous$$d of 1). :p
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Split BMP into smaller Textures using Unity 1 Answer
Adding texture into array at runtime 1 Answer
Changing texture with code 0 Answers
fade out black on touch with collider 2 Answers