The key feature of this recipe is the new CreateAudioSource(...) method. This method takes input as a reference to a sound clip file and a Boolean true/false value as to whether the sound should start playing immediately. The method does the following:
- It creates a new GameObject (with the same parent and at the same location as the GameObject doing the creating).
- It adds a new AudioSource component to the new GameObject.
- It sets the audio clip of the new AudioSource component to the provided AudioClip parameter.
- If the Boolean parameter is true, the AudioSource component is immediately sent a Play() message so that it starts playing the sound clip.
- A reference to the AudioSource component is returned.
The remainder of the MusicManager script class is very similar to that of the previous recipe. There are two public AudioClip variables, clipMedieval and clipArcade, which are set through drag and drop at design time to link to the sound clip files...