Graphics Reference
In-Depth Information
volume=0;
fadeState=0;
targetFadeState=1;
targetVolume=volumeON;
fadeTime=fadeAmount;
}
public void FadeOut ( float fadeAmount )
{
volume=volumeON;
fadeState=1;
targetFadeState=0;
targetVolume=0;
fadeTime=fadeAmount;
}
}
8.2.1 Script Breakdown
The MusicController.cs script should be added to an empty gameObject in a scene. It
derives from MonoBehavior to use the Start() and Update() functions called by the
engine:
public class MusicController : MonoBehavior
{
The MusicController script can automatically fade music in when it loads, making for
a nicer transition into the scene. shouldFadeInAtStart should be set in the Unity editor
Inspector window on the component:
public bool shouldFadeInAtStart= true;
void Start ()
{
As with the sound controller, it is important to set the gamePrefsName string to load
and save preferences correctly:
// we will grab the volume from PlayerPrefs when this script
// first starts
volumeON= PlayerPrefs.GetFloat(gamePrefsName+"_MusicVol");
In the next part of the script, a new gameObject is created and an AudioSource added
to it. The AudioSource is set to play on awake so that the music starts automatically at the
start of the scene. The music variable should be set in the Unity editor Inspector window,
so that the music AudioClip can be carried over to the new source:
// create a game object and add an AudioSource to it, to
// play music on
sourceGO= new GameObject("Music_AudioSource");
source= sourceGO.AddComponent<AudioSource>();
source.name= "MusicAudioSource";
Search WWH ::




Custom Search