Playing and controlling different sounds each with their own AudioSource component
While the approach in the previous recipe (using PlayOneShot(...)
with a single AudioSource) is fine for one-off sound effects, when further control may be required over a playing sound, each sound will needs to be played in its own AudioSource component. In this recipe, we'll create two separate AudioSource components and pause/resume each with different arrow keys.
Getting ready
Try this with two audio clips that are several seconds long. We have included two free music clips inside folder 04_02
.
How to do it...
To play different sounds each with their own AudioSouce component, do the following:
- Create a new Unity 2D project and import the sound clip files.
- Create a GameObject in the scene containing an AudioSource component linked to the
186772__dafawe__medieval
AudioClip. This can be done in a single step by dragging the music clip from the Project panel into either the Hierarchy or Scene panels. Rename this new...