SmoothAudioFade

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(JavaScript - Fade.js)
(C# - Fade.cs)
Line 1: Line 1:
 
[[Category: Sound]]
 
[[Category: Sound]]
 
Author: ''Jessy''
 
Author: ''Jessy''
 
 
 
 
 
== C# - Fade.cs ==
 
<csharp>
 
// If you don't want to have the clip be audible to start off with,
 
// set the Audio Source "Volume" to zero, and disable the following "Play" option.
 
 
using UnityEngine;
 
using System.Collections;
 
 
public class Fade : MonoBehaviour {
 
public bool play;  // if the attached Audio Source is playing
 
 
public float loudness; // Use this to manually control the volume instead of using the default Audio Source variable.
 
// If you set "play" to false initially, set the Audio Source volume to zero.
 
// Otherwise, set this and the Audio Source volume setting to the same value, initially.
 
 
public float fadeTime;  // time (in seconds) in which the audio fades out
 
 
private bool playing; // if the audio is playing
 
 
// to prevent fades from occurring right away
 
private bool fadeReady;
 
private bool early;
 
 
// track if the fades have been completed
 
private bool fadedIn;
 
private bool fadedOut;
 
 
private float loudnessNow; // used for proper volume ramping if switching fade direction before fade completion
 
private float switchTime; // the time when you last switched the sound on or off
 
 
private float dB; // a value created by an exponential curve, used to scale
 
// the volume in a manner that sounds natural to humans
 
 
private float fadeScale; // a value created by an a further exponential curve;
 
// makes "dB" into a percentage to multiply the volume by for smooth fades
 
 
void Start () {
 
playing = play;
 
fadeReady = false;
 
}
 
 
void FixedUpdate () {
 
early = ( (Time.time < fadeTime) && (fadeReady == false) );
 
fadeScale = Mathf.Pow(10, fadeTime) - 1;
 
 
if (play) {
 
if (playing == false) {
 
playing = true;
 
switchTime = Time.time;
 
loudnessNow = audio.volume; // grabs the volume when switching the sound on
 
// works whether the sound is currently fading or not
 
 
if (fadeReady == false) {
 
fadeReady = true;
 
}
 
}
 
 
fadedIn = Time.time >= (switchTime + fadeTime);
 
 
if (fadedIn || early) {
 
audio.volume = loudness;
 
}
 
 
else {
 
dB = Mathf.Pow(10, (Time.time - switchTime)) - 1;
 
audio.volume = (dB / fadeScale) * (loudness - loudnessNow) + loudnessNow;
 
}
 
 
}
 
 
else {
 
if (playing) {
 
playing = false;
 
switchTime = Time.time;
 
loudnessNow = audio.volume; // grabs the volume when switching the sound on
 
// works whether the sound is currently fading or not
 
if (fadeReady == false) {
 
fadeReady = true;
 
}
 
}
 
 
fadedOut = ( (Time.time - switchTime - fadeTime) >=0 );
 
 
if (fadedOut || early) {
 
audio.volume = 0;
 
}
 
 
else {
 
dB = Mathf.Pow(10, Mathf.Abs(Time.time - switchTime - fadeTime)) - 1;
 
audio.volume = (dB / fadeScale) * loudnessNow;
 
}
 
}
 
}
 
}
 
</csharp>
 

Revision as of 19:02, 12 September 2008

Author: Jessy

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox