I ran into a problem using the script from the FPS tutorial. The texture that is faded in and out shows UNDER the GUI elements drawn by the scenes.
Apparently all GUI elements drawn inside OnGUI methods display on top of all GUITextures. In order to display the transition texture on top of everything you have to use GUI.DrawTexture instead of the GUITexture component.
Here's a modified version of LevelLoadFade.js that uses GUI.DrawTexture.
NOTE #1: You have to set GUI.depth to something GREATER THAN the value used in this script (-100). Since depth is a static variable, you HAVE to set it back to 0 or whatever in your other OnGUI methods, or they'll all just get drawn at depth -100.
EDIT: I discovered I could use GUI.color to animate the alpha value of the texture without having to change any pixel data, so this script can do custom textures again.
/*
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.GetComponent(LevelLoadFade).DoFade(level, fadeLength, fadeTexture, Color.white, false);
}
static function FadeAndLoadLevel(level, color:Color, fadeLength:float) {
color.a = 1; // Make sure the texture is opaque so it will actually cover the scene
var fadeTexture = new Texture2D (1, 1);
fadeTexture.SetPixel(0, 0, color);
fadeTexture.Apply();
DontDestroyOnLoad(fadeTexture);
var fade = new GameObject ("Fade");
fade.AddComponent(LevelLoadFade);
fade.GetComponent(LevelLoadFade).DoFade(level, fadeLength, fadeTexture, color, true);
}
private var _fadeTexture:Texture2D;
private var _rect:Rect;
private var _color:Color;
function Awake():void {
_rect = new Rect(0, 0, Screen.width, Screen.height);
_fadeTexture = null;
}
function OnGUI():void {
if(_fadeTexture != null) {
GUI.depth = -100; // Lower depth values display on TOP of higher ones
GUI.color = _color;
GUI.DrawTexture(_rect, _fadeTexture);
}
}
function DoFade(level, fadeLength:float, fadeTexture:Texture2D, color:Color, destroyTexture:boolean) {
transform.position = Vector3.zero;
// Dont destroy the fade game object during level load
DontDestroyOnLoad(gameObject);
// Fadeout to start with
_color = color;
_color.a = 0;
_fadeTexture = fadeTexture;
// Fade texture in
var time = 0.0;
while(time < fadeLength) {
time += Time.deltaTime;
_color.a = Mathf.InverseLerp(0, 1, time / fadeLength);
yield;
}
_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;
_color.a = Mathf.InverseLerp(1, 0, time / fadeLength);
yield;
}
_color.a = 0;
yield;
_fadeTexture = null;
Destroy(gameObject);
if(destroyTexture)
Destroy(fadeTexture);
}