Search Unity

Sequenced Music (intro to loop)

Discussion in 'Audio & Video' started by SNS_Case, Jul 1, 2016.

  1. SNS_Case

    SNS_Case

    Joined:
    Oct 7, 2011
    Posts:
    58
    I've added a set of scripts to my github that's freely available for usage/modification. This enables you to play two separate tracks, an intro track and looping track, that are automatically sequenced to play one after the other, and then loop. It's pretty basic stuff but I thought it might be a nice way to help out if someone needs it.

    Link: https://github.com/casefc3s/SequencedAudio

    Readme:
    SequencedAudio
    Simple scripts that can be used to play audio tracks seamlessly (i.e. intro track to loop track) in Unity.

    Usage
    Add the SequencedMusicPlayer to a GameObject in the scene and also add two AudioSource components to that same object, then assign them to the SMP in the Inspector. Then add a MusicSet to another GameObject in the same scene. Assign the audio tracks to the MusicSet and select the options you want (fading/play on awake). It is not necessary to have tracks assigned to both fields if they're not needed.

    Recommendations
    I suggest using the Singleton class from the Unify Wiki and applying that to the SequencedMusicPlayer, then placing the SMP on an object in your initial loading scene that is persistent via DontDestroyOnLoad(). This will eliminate the need to use FindObjectOfType in the MusicSet's Start(), and instead you can call SequencedMusicPlayer.Instance.PlayTracks() directly. This will also enable you to place MusicSet's in individual scenes, so that you can have a persistent music player controlling what audio is being played as scenes change.
     
    coezo likes this.
  2. SRH4T3

    SRH4T3

    Joined:
    Aug 9, 2016
    Posts:
    4
    Can this be also used to have a "outro" track played after a gameplay trigger has told the loop to stop?
     
  3. SNS_Case

    SNS_Case

    Joined:
    Oct 7, 2011
    Posts:
    58
    It can certainly be updated to include that functionality, I'll look into it soon!
     
  4. coezo

    coezo

    Joined:
    Nov 27, 2016
    Posts:
    1
    That's very good! Thank you!
     
    SNS_Case likes this.