Listener

From Unify Community Wiki
(Difference between revisions)
Jump to: navigation, search
(JavaScript - Listener.js)
(JavaScript - Listener.js)
Line 18: Line 18:
 
private var loudnessExponent = Mathf.Log(Mathf.Sqrt(10), 2);
 
private var loudnessExponent = Mathf.Log(Mathf.Sqrt(10), 2);
  
function Volume (newVolume) : float
+
function Volume (newVolume)
 
{
 
{
 
volume = newVolume;
 
volume = newVolume;
Line 27: Line 27:
 
 
 
AudioListener.volume = listenerLoudness;
 
AudioListener.volume = listenerLoudness;
return listenerLoudness;
 
 
}
 
}
  

Revision as of 02:08, 19 May 2009

Author: Jessy

Contents

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 property performs the task that the similarly-named parameter of an Audio Listener was intended to perform. Use Listener.volume to read the value directly, but you will probably want to use the methods listed in the JavaScript and C# sections instead.

JavaScript - Listener.js

Use Listener.Volume(newVolume); to assign the volume parameter a value of newVolume, instead of assigning a value to volume directly. <javascript>@script RequireComponent(AudioListener)

var volume : float = 1;

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

function Volume (newVolume) { 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; }

function Start() {Volume(volume);}

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

C# - Listener.cs

<csharp>using UnityEngine; using System.Collections; [RequireComponent (typeof(AudioListener))]

public class Listener : MonoBehaviour { public float volume = 1;

float loudnessExponent = Mathf.Log(Mathf.Sqrt(10), 2); float listenerLoudness;

public float Volume { get {return listenerLoudness;} set { volume = value; listenerLoudness = Mathf.Pow(volume, loudnessExponent);

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

AudioListener.volume = listenerLoudness; } }

void Start() {Volume = volume;}

// Change values in the Editor; get results. void OnDrawGizmos () { Volume = volume; } }</csharp >

Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Toolbox