From Unify Community Wiki
Revision as of 18:16, 23 April 2009 by Jessy (Talk | contribs)

Jump to: navigation, search

Author: Jessy


This is a script that replaces the property of the Audio Listener. I feel that its behaves more intuitively than that of the Audio Listener itself. See the page for my Audio script for extensive details.  :-D


If your Game Object has only one Audio Source, then this script will automatically control it, when attached. Otherwise, you can programmatically assign what the script controls via the audioSource property.

The Volume, Min Volume, Max Volume, and Rolloff Factor parameters perform the tasks that the similarly-named parameters of Audio Sources were intended to perform. If scripting these, where you normally would type "audio" in your code, use "Audio" instead.

Rolloff Threshold defines a distance from the Audio Listener within which sound will not get quieter over distance. (Unity normally fixes this distance to 1 world unit.) This can be especially useful for dealing with "extreme" scale characters. (Use large values for giant monsters, small values for insects.) You may find that, by using this parameter, you can avoid having to worry about changing Rolloff Factor to anything other than 1.

If Ignore Distance is unchecked, it allows stereo clips to fade in volume over distance (but they do not get positioned in 3D "sound space"). For mono clips, checking this is a more efficient method for achieving the same result as Rolloff Factor = 0.

Performance Mode makes the code execute faster, but the effects of all properties, other than Rolloff Threshold and Ignore Distance, are not automatically updated every frame. You can call the RefreshLoudness(), RefreshMinLoudness(), RefreshMaxLoudness(), RefreshVolume(), and RefreshRolloffExponent() functions, to make changes only when necessary, from external code. It makes sense to leave Performance Mode off when setting up the scene, and then turn it on after tweaking, if you don't need these properties to be extremely dynamic.

JavaScript - Listener.js

<javascript>@script RequireComponent(AudioListener)

var volume : float = 1;

// determines whether "RefreshVolume" will run in Update(), // or must be called manually var performanceMode = false;

private var loudnessExponent = Mathf.Log(Mathf.Sqrt(10), 2);

function RefreshVolume () {audioListener.volume = Mathf.Pow(volume, loudnessExponent);}

function Start () {RefreshVolume ();}

function Update () {if (performanceMode == false) RefreshVolume ();}</javascript>

Personal tools