Skip to content

Instantly share code, notes, and snippets.

@stash
Last active March 3, 2018 02:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stash/7f008a9dbd95e0a01bcf7c9c24ee914e to your computer and use it in GitHub Desktop.
Save stash/7f008a9dbd95e0a01bcf7c9c24ee914e to your computer and use it in GitHub Desktop.

Simple music with introduction player.

Yes, you'll need to prepare your music by splitting it into an "intro" and a "loop" section as separate audio files.

Instructions (Unity 2017.3):

  1. Create an Empty Object, label it "BGM Player"
  2. Right click on "BGM Player", Create an Audio Source labelled "Intro". Uncheck "Play on Awake".
  3. Right click on "BGM Player", Create an Audio Source labelled "Loop". Uncheck "Play on Awake". Check "Loop".
  4. Click on "BGM Player" and add the "AudioLoopWithIntro" script.
  5. Drag "Intro" and "Loop" objects into the two corresponding script properties.
  6. Make a Prefab of this
  7. Assign an Audio Clip to each of the two Audio Source objects.
using UnityEngine;
public class AudioLoopWithIntro : MonoBehaviour {
public AudioSource introSource;
public AudioSource loopSource;
private void Start() {
// start the intro clip immediately
introSource.Play();
// schedule the loop clip to begin as soon as the intro clip ends.
loopSource.PlayScheduled(AudioSettings.dspTime + introSource.clip.length);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment