Graphics Reference
In-Depth Information
source.playOnAwake= true;
source.clip= music;
source.volume= volume;
The final part of the Start() function deals with setting up the default fade settings.
When shouldFadeInAtStart is set to true, the volume will start at 0, and the fadeState will
be 0. The fader variables will be explained in full along with the fader code further down
within the Update() loop:
// the script will automatically fade in if this is set
if(shouldFadeInAtStart)
{
fadeState=0;
volume=0;
} else {
fadeState=1;
volume=volumeON;
}
// set up default values
targetFadeState=1;
targetVolume=volumeON;
The AudioSource created to play music needs to have its volume set at the beginning
for it to start out at the volume set by the preference file:
source.volume=volume;
}
The Update() function begins by checking to see whether the AudioSource in the
variable source is playing by polling its AudioSource.isPlaying property. If this is false
and the music is set to loop (by the loopMusic variable), the next line makes a call to Play()
on the AudioSource, restarting the music:
void Update ()
{
// if the audiosource is not playing and it's supposed to
// loop, play it again (Sam?)
if( !source.isPlaying && loopMusic )
source.Play();
The fader works by having a fadeState variable and a targetFadeState. When targetFade
State is different to fadeState, it gets to work on fading the volume in whichever direction
it needs to go until it reaches the target volume. When the volume is at the target volume,
fadeState and targetFadeState will be the same. The target volume is decided by the state of
targetFadeState. When targetFadeState is 0, the target volume will be 0. When targetFade
State is 1, the target volume will be the value of the variable volumeON, which was given its
value back in the Start() function when the volume was grabbed from PlayerPrefs:
// deal with volume fade in/out
if(fadeState!=targetFadeState)
{
if(targetFadeState==1)
Search WWH ::




Custom Search