Listener

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
m (Description)
m (Obsolete.)
Line 1: Line 1:
[[Category: Sound]]
 
Author: ''Jessy''
 
  
==Description==
 
 
This is a script that replaces the '''Volume''' property of the Audio Listener.  It behaves more intuitively than that of the Audio Listener itself.
 
 
==General Instructions==
 
 
Attach this script to the Game Object that contains your Audio Listener.  If there is no Audio Listener present on the Game Object when you attach this script, one will automatically be created and attached.  Change the '''Volume''' value in the Editor for immediate results, or use the methods described in the JavaScript or C# sections.
 
 
The '''Volume''' property performs the task that the similarly-named parameter of an Audio Listener was intended to perform.
 
 
 
==JavaScript Instructions==
 
Use the following variable declaration in an external script, and drag the Game Object containing the Audio Listener component onto the '''Listener''' variable slot in the Inspector:
 
 
<javascript>var listener : Listener;</javascript>
 
 
 
Use ''listener.Volume(value);'' to assign the '''volume''' parameter a value of ''value'', instead of assigning a value to '''volume''' directly.  (You may find the way this is handled via C# to be more intuitive.)
 
 
 
==JavaScript - Listener.js==
 
<javascript>@script RequireComponent(AudioListener)
 
 
var volume : float = 1;
 
 
private var loudnessExponent = Mathf.Log(Mathf.Sqrt(10), 2);
 
 
function Volume (value)
 
{
 
// volume values outside 0-1 mean nothing in Unity
 
volume = Mathf.Clamp01(value);
 
AudioListener.volume = Mathf.Pow(volume, loudnessExponent);
 
}
 
 
function Start()
 
{Volume(volume);}
 
 
// Change values in the Editor; get results.
 
function OnDrawGizmos ()
 
{Volume(volume);</javascript>
 
 
 
== C# Instructions==
 
Use the following variable declaration in an external script, and drag the Game Object containing the Audio Listener component onto the '''Listener''' variable slot in the Inspector:
 
 
<csharp>public Listener listener;</csharp>
 
 
 
Use ''listener.Volume = value;'' to assign the '''volume''' parameter a value of ''value'', instead of assigning a value to '''volume''' directly.
 
 
 
== C# - Listener.cs==
 
<csharp>using UnityEngine;
 
[RequireComponent (typeof(AudioListener))]
 
 
public class Listener : MonoBehaviour
 
{
 
public float volume = 1;
 
 
float loudnessExponent = Mathf.Log(Mathf.Sqrt(10), 2);
 
 
public float Volume
 
{
 
// no need for a getter; use AudioListener.volume instead
 
 
set
 
{
 
// volume values outside 0-1 mean nothing in Unity
 
volume = Mathf.Clamp01(value);
 
AudioListener.volume = Mathf.Pow(volume, loudnessExponent);
 
}
 
}
 
 
void Start()
 
{Volume = volume;}
 
 
// Change values in the Editor; get results.
 
void OnDrawGizmos ()
 
{Volume = volume;}
 
}</csharp >
 

Revision as of 20:28, 11 May 2011

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox