Listener

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(JavaScript - Listener.js)
(JavaScript - Listener.js)
Line 16: Line 16:
  
 
var volume : float = 1;
 
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);
 
private var loudnessExponent = Mathf.Log(Mathf.Sqrt(10), 2);
  
function RefreshVolume ()
+
function Volume (newVolume) : float
{AudioListener.volume = Mathf.Pow(volume, loudnessExponent);}
+
{
 +
volume = newVolume;
 +
var listenerLoudness = Mathf.Pow(volume, loudnessExponent);
 +
 +
// volume values outside 0-1 mean nothing in Unity
 +
listenerLoudness = Mathf.Clamp(listenerLoudness, 0, 1);
 +
 +
AudioListener.volume = listenerLoudness;
 +
return listenerLoudness;
 +
}
  
 
function Start()
 
function Start()
{RefreshVolume();}
+
{Volume(volume);}
  
function Update ()  
+
// Change values in the Editor; get results.
{if (performanceMode == false)
+
function OnDrawGizmos ()  
RefreshVolume();}</javascript>
+
{Volume(volume);}</javascript>

Revision as of 01:31, 19 May 2009

Author: Jessy

Description

This is a script that replaces the Volume 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

Instructions

The Volume parameter performs the task that the similarly-named parameter of an Audio Listener was intended to perform.

Performance Mode makes the code execute faster, but the effects of Volume are not automatically updated every frame. You can call the RefreshVolume() function, to make changes only when necessary, from external code.

JavaScript - Listener.js

<javascript>@script RequireComponent(AudioListener)

var volume : float = 1;

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

function Volume (newVolume) : float { volume = newVolume; var listenerLoudness = Mathf.Pow(volume, loudnessExponent);

// volume values outside 0-1 mean nothing in Unity listenerLoudness = Mathf.Clamp(listenerLoudness, 0, 1);

AudioListener.volume = listenerLoudness; return listenerLoudness; }

function Start() {Volume(volume);}

// Change values in the Editor; get results. function OnDrawGizmos () {Volume(volume);}</javascript>

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox