Game Development Reference
In-Depth Information
audio.Play();
}
This function takes a string. This string will be used to select the sound to be played.
A good example for this would be if you wanted to play a rain atmospheric sound,
you would have a key that is assigned as
Rain
in your
KeyValuePair
list and its
AudioClip
value would be a rain sound file.
Within the
PlayRepeat
function, we use a
for
loop to iterate through the
KeyValuePair
list. If one of the keys in the
KeyValuePair
list matches the string
passed to the
PlayRepeat
function, we assign the clip of the
AudioSource
to that
key's value, which would be an
AudioClip
. Lastly, we break the
for
loop, set the
AudioSource
loop property to
true
, and play the sound.
Next, we will add a function that will allow us to play a sound that doesn't loop. Add
this function to the script:
void Play(string atmSong)
{
for(int i = 0; i < atmList.Count; i++)
{
if(atmList[i].Key == atmSong)
{
audio.clip = atmList[i].Value;
break;
}
}
audio.loop = false;
audio.Play();
}
This function will run in the same way as the
PlayRepeat
function, except we aren't
setting the loop property of the
AudioSource
list. Since the loop property isn't being
set to
true
, the sound will only play once.