CrossFade

From Unify Community Wiki
Revision as of 05:36, 9 December 2007 by Eric5h5 (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search


Author: Eric Haines (Eric5h5)

Description

Makes a smooth cross-fade from one camera view to another.

Usage

See the attached file for an example scene. The two cameras must be in the same scene, of course...drag these onto the appropriate slots in the inspector after you've attached the script to an object. The script has a fadeTime public variable; this is the time, in seconds, that it will take for the wipe to complete. It uses a fake "render texture" technique, which means it works in Unity Indie, but the downside is that the image in the first camera will become static during the fade, although the second camera can have motion. Also, it only works properly in the editor if the game window aspect is set to none so there are no black bars, or else the view will shift during the fade. This is just cosmetic, and doesn't affect an actual game build.

In actual use, you'd want to modify the script slightly: get rid of the Update function, and call ScreenCrossFade() appropriately instead.

JavaScript - Mouse Cursor.js

<javascript> // This only works properly in the editor if the game window aspect is set to none // so there are no black bars. var cam1 : GameObject; var cam2 : GameObject; var fadeTime : float = 2; private var inProgress = false; private var tex : Texture2D;

function Start() {

   // Set up GUITexture size
   guiTexture.pixelInset = Rect(0, 0, Screen.width, Screen.height);
   tex = new Texture2D(Screen.width, Screen.height, TextureFormat.RGB24, false);

}

function Update() {

   if (Input.GetKeyDown("space")) {ScreenCrossFade(fadeTime);}

}

function ScreenCrossFade(fadeTime : float) {

   if (inProgress) {return;}
   inProgress = true;
   
   // Get texture from screen
   tex.ReadPixels(Rect(0, 0, Screen.width, Screen.height), 0, 0, false);
   tex.Apply();
   guiTexture.texture = tex;
   
   // Switch cameras
   yield;  // So the first Time.deltaTime in the loop below isn't huge because of the tex.Apply() hit
   cam1.active = !cam1.active;
   cam2.active = !cam2.active;
   
   // Do the cross-fade
   guiTexture.enabled = true;
   for (i = 0.51; i > 0.0; i -= Time.deltaTime * 1/(fadeTime*2)) {
       guiTexture.color.a = i;
       yield;
   }
   
   // Clean up
   guiTexture.enabled = false;
   inProgress = false;

} </javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox