Preventing an audio clip from restarting if it is already playing
In a game, there might be several different events that cause a particular sound effect to start playing. If the sound is already playing, then in almost all cases, we won’t want to restart the sound. A limitation of the previous recipe was that if we repeatedly clicked the button, the AudioClip would keep being interrupted and restart from the beginning with each button click.
This recipe involves a custom C# script class that includes a test so that an AudioSource component is only sent a Play()
message if it is currently not playing.
Getting ready
This recipe adapts the previous one. So, make a copy of the project from the previous recipe, and work from this new copy.
How to do it...
To prevent an audio clip from restarting, perform the following steps:
- Open the project, which is a copy of the previous recipe.
- Create a second UI button named
Button-play-not-interrupt
by...