InAudio Documentation

Advanced Audio Plugin for Unity

User Tools

Site Tools


examples:playingmusic

Music Playing

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.

Open the window

To get started, open the window found here.

Create Music Group

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.

Renaming group

The name is purely for to help recognize it, it is not used for playback.

Add audio clip

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.

When dragged, it should look like this.

Playing Music

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);
    }
}

Drag to inspector

When we have the script ready, attach it to a game object and then drag the music group to the inspector.

Final look

This is how it will look. And now when you press play you should hear the music playing.

More Music

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.

Volume Control

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.

  1. Set the volume & pitch of the individual music node.
  2. Set if the node is muted or soloed.
  3. Playback control, solo & mute. To easily test the music.
  4. The final volume of a node in the hierarchy. Will also be affected by solo & mute.

Volume Control In Code

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;

examples/playingmusic.txt · Last modified: 2015/07/12 11:32 by peterolsted