Navigation
Quick start
Examples
Details
Extra
Feedback
- Email inaudio@outlook.com
Navigation
Quick start
Examples
Details
Extra
Feedback
In this example we will be looking at how to play music. The approach follows the same approach as audio in InAudio, you first create the definition then drag it to the inspector to a custom script and play it.
Music groups are used for playing music. They can contain audio clips and can also have other music groups as children which is played in sync.
To add a clip to play, just drag the audio onto the music group and they will be added to the music group. This is then the clip that will be played by this music group.
To play the music when the game starts, we need to an a script (or use the event hook).
This example will stop the music when the object gets disabled.
using UnityEngine; public class PlayMusicExample : MonoBehaviour { public InMusicGroup musicExample; void OnEnable() { InAudio.Music.Play(musicExample); } void OnDisable() { InAudio.Music.Stop(musicExample); } }
When we have the script ready, attach it to a game object and then drag the music group to the inspector.
Now that you know how to play a clip, we can do more. As the music definition is hierarchical, you can create a deep as a tree as you want. It is important to note that they are all in played & stopped sync with the parent volume & pitch affection its children. In this example, the nodes highlighted with a red marker are played in sync and the nodes marked in blue are played in sync.
Lastly, a word about volume & pitch control. These settings will affect all children of a node.
Volume, pitch & mute will affect the music in the same highlighted group as it self, like all the red markings.
Solo on the other hand will affect all music as mutes all but this node, its children and direct parents.
These things can also be done in code like this, following the code example futher up.
musicExample.Volume = 0.5f; musicExample.Pitch = 2.0f; musicExample.Solo = true; musicExample.Mute = false;