FadeInOut

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 +
[[Category: JavaScript]][[Category: Boo]][[Category: Scripts]][[Category: Camera Controls]]
 +
 +
== JavaScript ==
 +
 +
=== Introduction ===
 +
 +
Author: [[User:PsychicParrot|PsychicParrot]]
 +
 
Little script to fade an image (stretched to fit the screen, so use a 1x1 black pixel for a simple black fade in/out).  
 
Little script to fade an image (stretched to fit the screen, so use a 1x1 black pixel for a simple black fade in/out).  
  
Line 12: Line 20:
  
 
Enjoy!
 
Enjoy!
 +
 +
=== Source (FadeInOut.js) ===
  
 
<javascript>
 
<javascript>
Line 69: Line 79:
 
}
 
}
 
</javascript>
 
</javascript>
 +
 +
== Boo ==
 +
 +
=== Introduction ===
 +
 +
Author: [[User:Adrian|Adrian]]
 +
 +
Extended version written in Boo (put it into "Standard Assets/Scripts" to use it from within JavaScript files).
 +
 +
Main change is that rather than defining a fade speed you can set the duration of the fade in seconds.
 +
 +
Also provides methods to set the fade duration when the method is called and static methods to call the fade script on the main camera (by calling CameraFade.FadeInMain()).
 +
 +
=== Source (CameraFade.boo) ===
 +
 +
<boo>import UnityEngine
 +
 +
class CameraFade (MonoBehaviour):
 +
 +
# ---------------------------------------- #
 +
# PUBLIC FIELDS
 +
 +
# Texture used for fading
 +
public fadeTexture as Texture2D
 +
 +
# Default time a fade takes in seconds
 +
public fadeDuration as single = 2
 +
 +
# Depth of the gui element
 +
public guiDepth as int = -1
 +
 +
# Fade into scene at start
 +
public fadeIntoScene as bool = true
 +
 +
# ---------------------------------------- #
 +
# PRIVATE FIELDS
 +
 +
# Current alpha of the texture
 +
currentAlpha as single = 1
 +
 +
# Current duration of the fade
 +
currentDuration as single
 +
 +
# Direction of the fade
 +
fadeDirection as int = -1
 +
 +
# ---------------------------------------- #
 +
# START FADE METHODS
 +
 +
def FadeIn(duration as single):
 +
# Set fade duration
 +
currentDuration = duration
 +
# Set alpha to opaque
 +
currentAlpha = 1
 +
# Set direction to Fade in
 +
fadeDirection = -1
 +
 +
def FadeIn():
 +
FadeIn(fadeDuration)
 +
 +
def FadeOut(duration as single):
 +
# Set fade duration
 +
currentDuration = duration
 +
# Set alpha to transculent
 +
currentAlpha = 0
 +
# Set direction to fade out
 +
fadeDirection = 1
 +
 +
def FadeOut():
 +
FadeIn(fadeDuration)
 +
 +
# ---------------------------------------- #
 +
# STATIC FADING FOR MAIN CAMERA
 +
 +
static def FadeInMain(duration as single):
 +
GetInstance().FadeIn(duration)
 +
 +
static def FadeInMain():
 +
GetInstance().FadeIn()
 +
 +
static def FadeOutMain(duration as single):
 +
GetInstance().FadeOut(duration)
 +
 +
static def FadeOutMain():
 +
GetInstance().FadeOut()
 +
 +
# Get script fom Camera
 +
static def GetInstance() as CameraFade:
 +
# Get Script
 +
fader as CameraFade = Camera.main.GetComponent(CameraFade)
 +
# Check if script exists
 +
if (fader == null):
 +
raise System.Exception("No CameraFade attached to the main camera.")
 +
return fader
 +
 +
# ---------------------------------------- #
 +
# SCENE FADEIN
 +
 +
def Start():
 +
if fadeIntoScene:
 +
FadeIn()
 +
 +
# ---------------------------------------- #
 +
# FADING METHOD
 +
 +
def OnGUI():
 +
 +
# Fade alpha if active
 +
if ((fadeDirection == -1 and currentAlpha > 0)
 +
or
 +
(fadeDirection == 1 and currentAlpha < 1)):
 +
# Advance fade by fraction of full fade time
 +
currentAlpha += fadeDirection * (Time.deltaTime / currentDuration)
 +
# Clamp to 0-1
 +
currentAlpha = Mathf.Clamp01(currentAlpha)
 +
# Apply alpha
 +
GUI.color.a = currentAlpha;
 +
 +
# Draw only if not transculent
 +
if (currentAlpha > 0):
 +
# Draw texture at depth
 +
GUI.depth = guiDepth;
 +
GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
 +
</boo>

Revision as of 11:54, 26 May 2008


Contents

JavaScript

Introduction

Author: PsychicParrot

Little script to fade an image (stretched to fit the screen, so use a 1x1 black pixel for a simple black fade in/out).

Simply apply it to your camera (or an empty GameObject), set the texture to use, set the fadeSpeed and call fadeIn() or fadeOut()

Easiest way is probably to apply this script to your main camera in the scene, then wherever you want to fade use something like:

Camera.main.SendMessage("fadeOut");

or

Camera.main.SendMessage("fadeIn");

Enjoy!

Source (FadeInOut.js)

<javascript>

// FadeInOut // //-------------------------------------------------------------------- // Public parameters //--------------------------------------------------------------------

public var fadeOutTexture : Texture2D; public var fadeSpeed = 0.3;

var drawDepth = -1000;

//-------------------------------------------------------------------- // Private variables //--------------------------------------------------------------------

private var alpha = 1.0;

private var fadeDir = -1;

//-------------------------------------------------------------------- // Runtime functions //--------------------------------------------------------------------

//--------------------------------------------------------------------

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), fadeOutTexture); }

//--------------------------------------------------------------------

function fadeIn(){ fadeDir = -1; }

//--------------------------------------------------------------------

function fadeOut(){ fadeDir = 1; }

function Start(){ alpha=1; fadeIn(); } </javascript>

Boo

Introduction

Author: Adrian

Extended version written in Boo (put it into "Standard Assets/Scripts" to use it from within JavaScript files).

Main change is that rather than defining a fade speed you can set the duration of the fade in seconds.

Also provides methods to set the fade duration when the method is called and static methods to call the fade script on the main camera (by calling CameraFade.FadeInMain()).

Source (CameraFade.boo)

<boo>import UnityEngine

class CameraFade (MonoBehaviour):

# ---------------------------------------- # # PUBLIC FIELDS

# Texture used for fading public fadeTexture as Texture2D

# Default time a fade takes in seconds public fadeDuration as single = 2

# Depth of the gui element public guiDepth as int = -1

# Fade into scene at start public fadeIntoScene as bool = true

# ---------------------------------------- # # PRIVATE FIELDS

# Current alpha of the texture currentAlpha as single = 1

# Current duration of the fade currentDuration as single

# Direction of the fade fadeDirection as int = -1

# ---------------------------------------- # # START FADE METHODS

def FadeIn(duration as single): # Set fade duration currentDuration = duration # Set alpha to opaque currentAlpha = 1 # Set direction to Fade in fadeDirection = -1

def FadeIn(): FadeIn(fadeDuration)

def FadeOut(duration as single): # Set fade duration currentDuration = duration # Set alpha to transculent currentAlpha = 0 # Set direction to fade out fadeDirection = 1

def FadeOut(): FadeIn(fadeDuration)

# ---------------------------------------- # # STATIC FADING FOR MAIN CAMERA

static def FadeInMain(duration as single): GetInstance().FadeIn(duration)

static def FadeInMain(): GetInstance().FadeIn()

static def FadeOutMain(duration as single): GetInstance().FadeOut(duration)

static def FadeOutMain(): GetInstance().FadeOut()

# Get script fom Camera static def GetInstance() as CameraFade: # Get Script fader as CameraFade = Camera.main.GetComponent(CameraFade) # Check if script exists if (fader == null): raise System.Exception("No CameraFade attached to the main camera.") return fader

# ---------------------------------------- # # SCENE FADEIN

def Start(): if fadeIntoScene: FadeIn()

# ---------------------------------------- # # FADING METHOD

def OnGUI():

# Fade alpha if active if ((fadeDirection == -1 and currentAlpha > 0) or (fadeDirection == 1 and currentAlpha < 1)): # Advance fade by fraction of full fade time currentAlpha += fadeDirection * (Time.deltaTime / currentDuration) # Clamp to 0-1 currentAlpha = Mathf.Clamp01(currentAlpha) # Apply alpha GUI.color.a = currentAlpha;

# Draw only if not transculent if (currentAlpha > 0): # Draw texture at depth GUI.depth = guiDepth; GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture); </boo>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox