Graphics Reference
In-Depth Information
For safety, the index number is checked to make sure that it is valid so that it won't
raise a null reference exception because of a mistake with the call. The index number will
be set to the last AudioClip in the array if it is higher than it should be and a warning
logged in the console to raise attention to it:
if(anIndexNumber>soundObjectList.Count)
{
Debug.LogWarning("BaseSoundController>Trying to do
PlaySoundByIndex with invalid index number. Playing
last sound in array, instead.");
anIndexNumber= soundObjectList.Count-1;
}
A temporary AudioClip is made to hold the required sound as it is played with
AudioSource.PlaySound():
tempSoundObj= (SoundObject)soundObjectList[anIndexNumber];
tempSoundObj.PlaySound(aPosition);
}
}
There is still some way to go before this sound controller provides a complete solution
to game audio, but it should serve as a good, solid start.
8.2 The Music Player
The MusicController.cs script is intended for playing music. It will set the volume accord-
ing to a PlayerPrefs value and handle volume fading and audio clip looping.
Below is the full script:
using UnityEngine;
using System.Collections;
public class MusicController : MonoBehavior
{
private float volume;
public string gamePrefsName= "DefaultGame"; // DO NOT FORGET TO SET
// THIS IN THE EDITOR!!
public AudioClip music;
public bool loopMusic;
private AudioSource source;
private GameObject sourceGO;
private int fadeState;
private int targetFadeState;
private float volumeON;
private float targetVolume;
public float fadeTime=15f;
public bool shouldFadeInAtStart= true;
Search WWH ::




Custom Search