Forum rules - please read before posting.

Multiple soundtracks styles?

edited December 2018 in Technical Q&A
(This is regarding AC built-in music, not third-party integration) What would be the best method of using AC to offer the user different soundtrack types? I'd like to have a menu option to allow for default classic style music or an enhanced soundtrack, which I would probably accomplish using a global variable to determine the type of soundtrack chosen, and each scene would refer to this global onStart cutscene and if the music was changed in an actionlist. It would just be two sets of music files with different music styles recorded.

There's also the issue of the player changing the menu setting mid-song, and the song would need to be changed to the appropriate music file for the style chosen and resume playback from where it was, presumably by script, but I'm not entirely sure what the script would need to be doing to hook into this. Using music file tags might be a handy way of choosing the same file playing and changing to a matching file song name with a different tag for style, or something of that variant.

I'm not sure if that would be the best practice, nor am I totally certain of how I should be hooking into this so any suggestions would be appreciated!


  • Having a different track play based on a menu option would just be a case of mapping that option to a Global Variable, and then reading that Variable to run a different Sound: Play music Action.

    It would likely be easier, however, to rely on a custom Action to do this all in one - since it'd be easier to match up the different tracks in script. To play a piece of music, all you need to do is call:

    Music music = KickStarter.stateHandler.GetMusicEngine ();
    music.Play (trackID, loop, isQueued, _time, resumeIfPlayedBefore);

    You could, for example, alternate your tracks so that the equivalent "alternate" track is always 1 greater than the "normal" one.

    As for changing a track mid-play, that'd certainly require custom scripting because you'd have to read the timeSamples from the old track and apply them to the new one.

  • Dang custom actions, I still haven't braved them yet! As for matching the time, this would be the same as you mentioned in the other thread about using hook events and grabbing the current playing sample?
  • No, you can just read the timeSamples from the Music object's AudioSource:

    int currentSampes = music.GetComponent <AudioSource>().timeSamples;

    I will, however, add an optional "timeSamples" parameter to the Play function so that you can more easily set the timeSamples of the new track, i.e.:

    music.Play (trackID, loop, isQueued, _time, false, currentSamples);
  • I was wondering if there’s been any plans to integrate a built-in way of playing multiple soundtracks out-of-the-box? I’d use it myself for loading a pair of files for each track and use a setting to determine which of the two files would play depending on the setting (classic style or modern style) but I could imagine such a feature would be good for games that use musical variations on a theme depending on the progress of the player and the soundtrack would adjust accordingly. It could also be used to simulate a iMuse type soundtrack.
  • You can already play as many music tracks as you like via the regular Sound object - the "music" system is mainly just a convenience tool to auto-handle saving/loading and queuing tracks up.

    Certainly something in the vein of iMuse emulation would warrant a separate asset, but as for a classic/modern mix, you could probaby get most of the way there by hooking into the OnPlayMusic / OnStopMusic custom events.

    Something like this:

    using UnityEngine;
    using System.Collections;
    using AC;
    public class AlternativeMusicTest : MonoBehaviour
        public Sound alternativeSound;
        private void OnEnable ()
            EventManager.OnPlayMusic += OnPlayMusic;
            EventManager.OnStopMusic += OnStopMusic;
        private void OnDisable ()
            EventManager.OnPlayMusic -= OnPlayMusic;
            EventManager.OnStopMusic -= OnStopMusic;
        private void OnPlayMusic (int trackID, bool loop, float fadeTime, int startingSample)
            int alternativeTrackID = trackID + 1;
            foreach (MusicStorage musicStorage in KickStarter.settingsManager.musicStorages)
                if (musicStorage.ID == alternativeTrackID)
                    AudioClip alternativeTrackClip = musicStorage.audioClip;
                    alternativeSound.GetComponent <AudioSource>().clip = alternativeTrackClip;
                    alternativeSound.FadeIn (fadeTime, loop, startingSample);
        private void OnStopMusic (float fadeTime)
            alternativeSound.FadeOut (fadeTime);

    What this does is play an alternative music track (the "true" track ID plus 1) on a separate Sound object. With Mixer Groups, you could then control the respective volumes of both this Sound object, and the default Music object.

  • Yeah, Cinema Paradiso 👌
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.