simple fade out fade in load level

hi guys,

im looking for a simple fade out fade in script in between my load level scripts.

atm i have

function OnControllerColliderHit(hit : ControllerColliderHit){
	
	if(hit.collider.gameObject.tag == "island"){

    	Application.LoadLevel("island_level");
	}
}

i take it i will need to slot in the fade out before these lines?? but what about the fade in? i’m sure this is really simple…

a little help with this would be great.

Chris

the fps tut has a script that works well:

/*
	Usage:

	// Load my level	
	LevelLoadFade.FadeAndLoadLevel("mylevel", Color.white, 0.5);

	// Reset the current level
	LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.white, 0.5);
*/

static function FadeAndLoadLevel (level, fadeTexture : Texture2D, fadeLength : float)
{
	if (fadeTexture == null)
		FadeAndLoadLevel(level, Color.white, fadeLength);

	var fade = new GameObject ("Fade");
	fade.AddComponent(LevelLoadFade);
	fade.AddComponent(GUITexture);
	fade.transform.position = Vector3 (0.5, 0.5, 1000);
	fade.guiElement.texture = fadeTexture;
	fade.GetComponent(LevelLoadFade).DoFade(level, fadeLength, false);
}

static function FadeAndLoadLevel (level, color : Color, fadeLength : float)
{
	var fadeTexture = new Texture2D (1, 1);
	fadeTexture.SetPixel(0, 0, color);
	fadeTexture.Apply();
	
	var fade = new GameObject ("Fade");
	fade.AddComponent(LevelLoadFade);
	fade.AddComponent(GUITexture);
	fade.transform.position = Vector3 (0.5, 0.5, 1000);
	fade.guiElement.texture = fadeTexture;

	DontDestroyOnLoad(fadeTexture);
	fade.GetComponent(LevelLoadFade).DoFade(level, fadeLength, true);
}

function DoFade (level, fadeLength : float, destroyTexture : boolean)
{
	// Dont destroy the fade game object during level load
	DontDestroyOnLoad(gameObject);

	// Fadeout to start with
	guiElement.color.a = 0;
	
	// Fade texture in
	var time = 0.0;
	while (time < fadeLength)
	{
		time += Time.deltaTime;
		guiElement.color.a = Mathf.InverseLerp(0.0, fadeLength, time);
		yield;
	}
	guiElement.color.a = 1;
	yield;

	// Complete the fade out (Load a level or reset player position)
	Application.LoadLevel(level);
	
	// Fade texture out
	time = 0.0;
	while (time < fadeLength)
	{
		time += Time.deltaTime;
		guiElement.color.a = Mathf.InverseLerp(fadeLength, 0.0, time);
		yield;
	}
	guiElement.color.a = 0;
	yield;

	Destroy (gameObject);

	// If we created the texture from code we used DontDestroyOnLoad,
	// which means we have to clean it up manually to avoid leaks
	if (destroyTexture)
		Destroy (guiElement.texture);
}

Okay. this works BUT! seems to always fade the same amount

eg. basically when you load a small level eg. a room, itll load well and show the fade, but with more complex levels it takes a while, and its as if the fade has taken place already - the complex level loads in from a black screen, not fading.

Help?!

maybe change the fade length parameter? (its currently set to 0.5 in the script)

i have tried this but each level takes a different amount of time to load so changing the length parameter will have a different effect on the fade for each level. is there a way to get the level to load fully then run the fade in??

cheers

Chris

maybe using this would do the trick? (i’m not sure never used it before)

http://unity3d.com/Documentation/ScriptReference/MonoBehaviour.OnLevelWasLoaded.html

I think that you may want to move the fade-in behavior into the target level that you are loading or give the good doctor’s* suggestion a try and use OnLevelWasLoaded (when the OnLevelWasLoaded function is called your target level is loaded and ready, at that point you can initiate your fade-in).

*Assuming that drJones is something akin to “Doctor Jones”… :stuck_out_tongue:

just a raiders fan ; )

i thought i would need a script on the level that is loading up, so it loads and then does the fade.

still unsure as to what i will need on the loading level??

thanks guys

If you want the fade to start after the level has finished loading, then the simplest thing is to put the GUITexture that you want faded into your new level and attach a fade script there (I thought I saw one in the wiki, but it seems to be down right now), so it will run/fade as soon as the level is loaded. You don’t need to do anyting to the level that initiated the load, since it’s gone by then.

I had some intro fade screens like that and then later tried moving them into my load-screen level, using DontDestroyOnLoad() and OnLevelWasLoaded() to preserve it and defer activation, but that’s really an optimization to avoid duplicating the fade object/script in every level (and I think a bug in 1.6.1 broke it). In any case, it’s best to start with the simple implementation first.