Fade in/out

Hi :smile:
There is s scene in the intro of a game, this scene includes 640x480 GUI texture (developer name jpg image)

I’m trying make this image fade in then after seconds fade out, but the problem is image fade in and stopped not fade out …!

function Start () {
   yield Fade(0, .5, 1);  // Start, end, length in seconds
   }

function Fade (start : float, end : float, length : float) {
   for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
      guiTexture.color.a = Mathf.Lerp(start, end, i);
      yield;
   }
}
function End() {
   yield Fade(.5, 0, 1);  // Start, end, length in seconds
   }

what is my mistake here …?

I may think is probably because the function Mathf.Lerp(minimum, maximum, Time.time); that should go from a min value to a max. In your case when you perform Fade out you are going from a min value of 5 to 0. This is my guess :slight_smile:

EDIT: Ok sorry what I said in this post is not true :slight_smile:

This fade/in code:

function Start () {
   yield Fade(0, .5, 1);  // Start, end, length in seconds
   }

function Fade (start : float, end : float, length : float) {
   for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
      guiTexture.color.a = Mathf.Lerp(start, end, i);
      yield;
   }
}

This fade/out code:

function Start () {
   yield Fade(.5, 0, 1);  // Start, end, length in seconds
   }

function Fade (start : float, end : float, length : float) {
   for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
      guiTexture.color.a = Mathf.Lerp(start, end, i);
      yield;
   }
}

How can i apply them to one GUI texture object to make fade in then fade out

I use this C# function for it:

IEnumerator FadeAlpha (float from, float to, float duration)
    {
        float startTime = Time.time;
        while (Time.time - startTime < duration)
        {
            m_FadeColor.a = Mathf.Lerp(from, to, (Time.time - startTime) / duration);

            yield return 0;
        }
    }

But if you look for other posts you will find lots of stuff about Fading :slight_smile:

You’re never calling the End() function. But I wouldn’t suggest making that a separate function anyway.

function Start () {
   yield Fade(0, .5, 1);  // Start, end, length in seconds
   yield WaitForSeconds(1.0); // Or whatever
   yield Fade(.5, 0, 1);  // Start, end, length in seconds   
}

–Eric

hello everyone

So im having a problem with my fade in function, I came here and this page was very helpful but i cant get mine to work. Its a javascript attached to a GUITexture. I just need the fade in ability I’m not using the fade out here is the code im using:

function Start () {
yield Fade(0, .5, 1); // Start, end, length in seconds
}

function Fade (start : float, end : float, length : float) {
for (i = 0.0; i < 1.0; i += Time.deltaTime*(1/length)) {
guiTexture.color.a = Mathf.Lerp(start, end, i);
yield;
}
}

as you can see the above was very inspiring but when i start the game my image dosent fade. Any help here would be greatly appreciated.

Well I have used this one several times both for iPhone and PC/Mac use.

var guiObject : GUITexture; 
var fadeTime = 1.0; 
enum Fade {In, Out} 
 
 
// Fade in the GUITexture, wait a couple of seconds, then fade it out 
function Start () { 
   yield FadeGUITexture(guiObject, fadeTime, Fade.In); 
   yield WaitForSeconds(2.0); 
   yield FadeGUITexture(guiObject, fadeTime, Fade.Out); 
   Application.LoadLevel (Application.loadedLevel+1);
   } 

function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) { 
   var start = fadeType == Fade.In? 0.0 : 1.0; 
   var end = fadeType == Fade.In? 1.0 : 0.0; 
   var i = 0.0; 
   var step = 1.0/timer; 
    
   while (i < 1.0) { 
      i += step * Time.deltaTime; 
      guiObject.color.a = Mathf.Lerp(start, end, i)*.5; 
      yield; 
   } 
}

Hope it helps :smile:

Thanx andy_h for the quick response and help but unfortunately it never worked…maybe i did something wrong with the code. I put in your code almost exactly, I did remove these 2 lines:

yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
Application.LoadLevel (Application.loadedLevel+1);

only because Im not useing a fade out and im not loading another level but other then that i kept it the same. I never got any errors or any wierd glitches it just never faded. Any more help would be greatly appreciated. Also im kinda a noob so please bare with me, thanks

Did you attach the script to the GUI texture asset in the Hierarchy?

yup

sorry about that dident meen to hit the submit button issues on my side sorry anyway yes the script is attached to the GUITexture no problem there

If you change the fade time on the top line, to say 5, does that work.

I’m not getting any errors and it works fine here. The only difference is I have the fade time set to 3.

Thanx again but still no change maybe it would help if I posted my all my code:

var MaskTexture : Texture2D;
static var depth : int;
var guiObject : GUITexture;
var fadeTime = 5.0;
enum Fade {In, Out}

function Update () {

}

// Fade in the GUITexture, wait a couple of seconds, then fade it out
function Start () {
yield FadeGUITexture(guiTexture, fadeTime, Fade.In);
yield WaitForSeconds(2.0);
//yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
// Application.LoadLevel (Application.loadedLevel+1);
}

function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;

while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}

function OnGUI()
{
GUI.DrawTexture(Rect((Screen.width / 2 - 280), 0.0, 560.0, 291.0),
MaskTexture, ScaleMode.StretchToFill, true, 0);
GUI.depth = 1;
}

the function onGUI is what places the image on the screen and its position hopefully this will give you a better clue as to the problem thanx

I suspect it’s because you are calling the fade before calling the GUI texture.

Is it a single full screen texture in it’s own scene, like a splash screen, or is it an element for a HUD or something.

The image is an element for a HUD. I also moved the onGUI function below the Update function but above the start function to see if it was because of what you suggested it being below the fade function but again no change, the image still stays regular with no fade.

Interesting. OK let me try a few things.

sure thanx again for the help also not sure if this will help but before i used the onGUI to position the image I placed it on screen and positioned it useing the transform panel then used the fade in code I put up on my first comment and it worked no problem, but as soon as I put in the onGUI function for positioning and scaleing the fade stopped working…not sure if this helps but thought Id let you know just in case thanx again

Hi All

Has anybody been able to figure out this issue yet? Any help would be greatly appreciated thanks.

You’ll probably have to post the malfunctioning code here, or it will be almost impossible for anyone to help you.

I did post my malfunctioning code on the first page but for convience sake here it is again:

var MaskTexture : Texture2D;
static var depth : int;
var guiObject : GUITexture;
var fadeTime = 5.0;
enum Fade {In, Out}

function OnGUI()
{
GUI.DrawTexture(Rect((Screen.width / 2 - 280), 0.0, 560.0, 291.0),
MaskTexture, ScaleMode.StretchToFill, true, 0);
GUI.depth = 1;
}

function Update () {

}

// Fade in the GUITexture, wait a couple of seconds, then fade it out
function Start () {
yield FadeGUITexture(guiTexture, fadeTime, Fade.In);
yield WaitForSeconds(2.0);
//yield FadeGUITexture(guiObject, fadeTime, Fade.Out);
// Application.LoadLevel (Application.loadedLevel+1);
}

function FadeGUITexture (guiObject : GUITexture, timer : float, fadeType : Fade) {
var start = fadeType == Fade.In? 0.0 : 1.0;
var end = fadeType == Fade.In? 1.0 : 0.0;
var i = 0.0;
var step = 1.0/timer;

while (i < 1.0) {
i += step * Time.deltaTime;
guiObject.color.a = Mathf.Lerp(start, end, i)*.5;
yield;
}
}

any help would be great thanks