Loop background music with intro
Verification environment
- Windows
-
- Windows 11
- Unity Editor
-
- 2021.3.3f1
- Input System Package
-
- 1.3.0
Prerequisites for this tip
The following settings have been made in advance as a premise for the description of this tip.
About the material included with the sample
BGM is borrowed from the following site.
About the audio file of the loop with intro
This time, only the standard Unity function will play a loop with an intro, but this is not supported as a standard function. In the first place, the specifications of the loop with intro as an audio file are not fixed, so the creation method differs depending on the game framework.
This time, we will prepare two audio files for the "intro" part and the "loop" part, play the intro part once, and play the loop part repeatedly. Therefore, please prepare the above two files as audio files.
Some distribution sites consider this and distribute audio files separately in advance. If not, you need to make it yourself with a voice editing tool.
If you want to split the file in two, we recommend the following format of the audio file.
- OggVorbis (.ogg)
- WAV (.wav)
For more information, see the official Unity documentation.
About playing looping background music with intro
Create a UI that allows you to play, pause, and stop background music as a sample. It has the same layout as a normal background music playback sample.
Add two audio files to your project, split into two parts, an intro and a loop.
This time, we will create our own program to play the looping BGM with the intro.
Create a script and leave it named IntroLoopAudio
.
The script looks like this: I'm referring to the code on the following site, but I added a lot of code because it didn't work well with WebGL and I wanted a little more control.
[Reference] 【Unity】Implement Intro + Loop Play - 7080 + 1
using UnityEngine;
<summary>
イントロ付きループ BGM を制御するクラスです。
</summary>
<remarks>
WebGL では PlayScheduled で再生するとループしないのでその対応を入れている。
WebGL では2つの AudioSource を交互に再生。
</remarks>
public class IntroLoopAudio : MonoBehaviour
{
<summary>BGM のイントロ部分の音声データ。</summary>
[SerializeField] private AudioClip AudioClipIntro;
<summary>BGM のループ部分の音声データ。</summary>
[SerializeField] private AudioClip AudioClipLoop;
<summary>BGM のイントロ部分の AudioSource。</summary>
private AudioSource _introAudioSource;
<summary>BGM のループ部分の AudioSource。</summary>
private AudioSource[] _loopAudioSources = new AudioSource[2];
<summary>一時停止中かどうか。</summary>
private bool _isPause;
<summary>現在の再生するループ部分のインデックス。</summary>
private int _nowPlayIndex = 0;
<summary>ループ部分に使用する AudioSource の数。</summary>
private int _loopSourceCount = 0;
<summary>再生中であるかどうか。一時停止、非アクティブの場合は false を返す。</summary>
private bool IsPlaying
=> (_introAudioSource.isPlaying || _introAudioSource.time > 0)
|| (_loopAudioSources[0].isPlaying || _loopAudioSources[0].time > 0)
|| (_loopAudioSources[1] != null && (_loopAudioSources[1].isPlaying || _loopAudioSources[1].time > 0));
<summary>現在アクティブで再生しているループ側の AudioSource。</summary>
private AudioSource LoopAudioSourceActive
=> _loopAudioSources[1] != null && _loopAudioSources[1].time > 0 ? _loopAudioSources[1] : _loopAudioSources[0];
<summary>現在の再生時間 (s)。</summary>
public float time
=> _introAudioSource == null ? 0
: _introAudioSource.time > 0 ? _introAudioSource.time
: LoopAudioSourceActive.time > 0 ? AudioClipIntro.length + LoopAudioSourceActive.time
: 0;
void Start()
{
_loopSourceCount = 2; // WebGL でなければ 1 でもよい
// AudioSource を自身に追加
_introAudioSource = gameObject.AddComponent<AudioSource>();
_loopAudioSources[0] = gameObject.AddComponent<AudioSource>();
if (_loopSourceCount >= 2)
{
_loopAudioSources[1] = gameObject.AddComponent<AudioSource>();
}
_introAudioSource.clip = AudioClipIntro;
_introAudioSource.loop = false;
_introAudioSource.playOnAwake = false;
_loopAudioSources[0].clip = AudioClipLoop;
_loopAudioSources[0].loop = _loopSourceCount == 1;
_loopAudioSources[0].playOnAwake = false;
if (_loopAudioSources[1] != null)
{
_loopAudioSources[1].clip = AudioClipLoop;
_loopAudioSources[1].loop = false;
_loopAudioSources[1].playOnAwake = false;
}
}
void Update()
{
// WebGL のためのループ切り替え処理
if (_loopSourceCount >= 2)
{
// 終了する1秒前から次の再生のスケジュールを登録する
if (_nowPlayIndex == 0 && _loopAudioSources[0].time >= AudioClipLoop.length - 1)
{
_loopAudioSources[1].PlayScheduled(AudioSettings.dspTime + (AudioClipLoop.length - _loopAudioSources[0].time));
_nowPlayIndex = 1;
}
else if (_nowPlayIndex == 1 && _loopAudioSources[1].time >= AudioClipLoop.length - 1)
{
_loopAudioSources[0].PlayScheduled(AudioSettings.dspTime + (AudioClipLoop.length - _loopAudioSources[1].time));
_nowPlayIndex = 0;
}
}
}
public void Play()
{
// クリップが設定されていない場合は何もしない
if (_introAudioSource == null || _loopAudioSources == null) return;
// Pause 中は isPlaying は false
// 標準機能だけでは一時停止中か判別不可能
if (_isPause)
{
_introAudioSource.UnPause();
if (_introAudioSource.isPlaying)
{
// イントロ中ならループ開始時間を残り時間で再設定
_loopAudioSources[0].Stop();
_loopAudioSources[0].PlayScheduled(AudioSettings.dspTime + AudioClipIntro.length - _introAudioSource.time);
}
else
{
if (_loopSourceCount >= 2)
{
// WebGL の場合は切り替え処理を実行
if (_loopAudioSources[0].time > 0)
{
_loopAudioSources[0].UnPause();
if (_loopAudioSources[0].time >= AudioClipLoop.length - 1)
{
_loopAudioSources[1].Stop();
_loopAudioSources[1].PlayScheduled(AudioSettings.dspTime + (AudioClipLoop.length - _loopAudioSources[0].time));
_nowPlayIndex = 1;
}
}
else
{
_loopAudioSources[1].UnPause();
if (_loopAudioSources[1].time >= AudioClipLoop.length - 1)
{
_loopAudioSources[0].Stop();
_loopAudioSources[0].PlayScheduled(AudioSettings.dspTime + (AudioClipLoop.length - _loopAudioSources[0].time));
_nowPlayIndex = 0;
}
}
}
else
{
// WebGL 以外は UnPause するだけ
_loopAudioSources[0].UnPause();
}
}
}
else if (IsPlaying == false)
{
// 最初から再生
Stop();
_introAudioSource.Play();
// イントロの時間が経過した後に再生できるようにする
// 設定する時間はゲーム刑か時間での設定となる
_loopAudioSources[0].PlayScheduled(AudioSettings.dspTime + AudioClipIntro.length);
}
_isPause = false;
}
<summary>BGM を一時停止します。</summary>
public void Pause()
{
if (_introAudioSource == null || _loopAudioSources == null) return;
_introAudioSource.Pause();
_loopAudioSources[0].Pause();
if (_loopAudioSources[1] != null) _loopAudioSources[1].Pause();
_isPause = true;
}
<summary>BGM を停止します。</summary>
public void Stop()
{
if (_introAudioSource == null || _loopAudioSources == null) return;
_introAudioSource.Stop();
_loopAudioSources[0].Stop();
if (_loopAudioSources[1] != null) _loopAudioSources[1].Stop();
_isPause = false;
}
}
Since the code is long, I will omit the details, but I set the intro and loop AudioClip
in advance,
When you start playing, play the intro first. For loops, schedule them to play when the intro ends.
Once the loop starts playing, play it repeatedly.
Originally, loop
for loops, it was enough to set the true
property to , but it didn't work properly in WebGL.
AudioSource
I prepare two loops and switch them alternately to play.
If you don't have to consider WebGL, you can cut your code in half.
After you create a script, you attach it to an object. Normally, it may be better to create an empty object and attach it, but it is troublesome, so attach it to EventSystem.
There are intro and loop items, so drop and set the audio file respectively.
At this point, no special processing is required.
I want to process it when I click the button, so I create a script (ButtonEvent
) for the button.
The script looks like this:
using UnityEngine;
public class ButtonEvent : MonoBehaviour
{
[SerializeField] private IntroLoopAudio IntroLoopAudio;
public void OnClickPlay()
{
IntroLoopAudio.Play();
}
public void OnClickPause()
{
IntroLoopAudio.Pause();
}
public void OnClickStop()
{
IntroLoopAudio.Stop();
}
}
IntroLoopAudio
from the inspector and add actions for each button.
The script is attached to EventSystem. Since you need to set Intro Loop Audio, set the EventSystem that has Intro Loop Audio.
Now assign methods to the click events of the three buttons.
Once everything is set up, run the game and try to play it. When the playback ends to the end, you can see that it loops from the middle of the song and plays. Of course, this assumes that the audio data is neatly divided and the loops are connected neatly.
Display the current duration
As a bonus from here,IntroLoopAudio
we have added a property that allows us to get the current playback time in , so let's display it.
First, place the text to display the time.
Create a script.
using UnityEngine;
using UnityEngine;
using UnityEngine.UI;
public class TextEvent : MonoBehaviour
{
[SerializeField] private IntroLoopAudio IntroLoopAudio;
private Text _text;
// Start is called before the first frame update
void Start()
{
_text = GetComponent<Text>();
}
// Update is called once per frame
void Update()
{
_text.text = $"AudioPlayTime : {IntroLoopAudio.time}";
}
}
Attach a script to the text and set the EventSystem with IntroLoopAudio.
Run it and see if the current playback time is displayed. Also, when looping, try to check if the time returns to the loop point.