How to add a GUi.Button to faceout script

I want to add a GUI.Button to a script, than when a Button pressed then the fadeout starts

here is the fadeout script

#pragma strict
// FadeInOut
var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;
 
private var alpha = 0.0; 
private var fadeDir = -1;

function OnGUI(){
    alpha -= fadeDir * fadeSpeed * Time.deltaTime;  
    alpha = Mathf.Clamp01(alpha); 
    GUI.color.a = alpha;
    GUI.depth = drawDepth;
    GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
	}

No one?

#pragma strict

// FadeInOut

var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;

 

private var alpha = 0.0; 
private var fadeDir = -1;
private var fadeEnabled = false;
	
function OnGUI(){
	if(GUI.Button(Rect(0,0,100,21),"Fade out")){
		fadeEnabled = true;
	}
	
	if(fadeEnabled){
		alpha -= fadeDir * fadeSpeed * Time.deltaTime;  
		alpha = Mathf.Clamp01(alpha); 
		GUI.color.a = alpha;
		GUI.depth = drawDepth;
		GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
	}
}

This kind of thing?

Yeah something like that but the thing is, I want the button to open a new level and when pressed it fades out and takes you to the level, is it possible?

I tried to add an if statement like this:

if(GUI.Button(Rect(0,0,100,21),"Fade out")){

        fadeEnabled = true;

        if(fadeEnabled == true){
        Application.LoadLevel("the level");
}

but it doesnt work, any idea?

Firstly; your scenes/levels are number-indexedrather then by name.

Secondly; afaik only in unity pro its possible to create something like an actual loading screen. In indie scene switching is performed with the last image visible i believe.

Firstly; your scenes/levels are number-indexedrather then by name.

Secondly; afaik only in unity pro its possible to create something like an actual loading screen. In indie scene switching is performed with the last image visible i believe.

well pressing the button takes me to the next scene/level, but it doesnt fade out, and I do have Unity Pro…

This is easily do-able in Unity free
Just have another test, if the fade is complete (alpha < 0), THEN load the level

Could you please write it in code? I tried but it didnt do anything

thats what I added

if(alpha<0){
Application.LoadLevel("the level");
}

but it didnt help, is that how you meant?

Anyone please? =)

Ehm. I may be wrong, but i think something in your logical ‘order’ is thought up wrong :slight_smile:

Am i correct to assume that when you click the button you want to blend your screen to the fadetexture?

Then it’s correct you’re starting with alpha 0,
But i don’t see the point why you’re inversing your math (both the fadeDir and the formula being in negative)
However, this would mean the fade is complete when alpha>=1 :slight_smile:

Ill take a more thorough work later, but maybe it leads you somewhere

You clamp the alpha value from 0 to 1, that said alpha will never be < 0.
Also i noticed that your alpha value is incrementing so you should check for alpha >= 1.

That worked like a charm, thanks alot for all help =)