ADX2 LEの音関係の管理はSoundManagerを作った方がやりやすいというものを見たため、作成してみた。
環境
・Windows 10
・Unity 5.4.0f3
SoundManager
SoundManager.cs
using UnityEngine;
using System.Collections;
public class SoundManager : MonoBehaviour {
private string SESheetName = "CueSheet_0"; //SE用キューシート
private string BGMSheetName = "CueSheet_1"; //BGM用キューシート
private CriAtomSource atomSourceSE;
private CriAtomSource atomSourceBGM;
void Start () { //初期化
atomSourceSE = gameObject.AddComponent<CriAtomSource>();
atomSourceBGM = gameObject.AddComponent<CriAtomSource>();
atomSourceSE.cueSheet = SESheetName;
atomSourceBGM.cueSheet = BGMSheetName;
}
public void PlaySE(string cueName) { //SEを再生(引数:SE名)
atomSourceSE.androidUseLowLatencyVoicePool = true; //Android用遅延対策
atomSourceSE.Play(cueName);
}
public void PlayBGM(string cueName) { //BGMを再生(引数:BGM名)
atomSourceBGM.Play(cueName);
}
public void StopBGM() { //BGMを停止
atomSourceBGM.Stop();
}
public void PauseBGM() { //BGMを一時停止(ポーズ)
atomSourceBGM.Pause(true);
}
public void ResumeBGM() { //BGMを再開
atomSourceBGM.Pause(false);
}
public long GetBGMTime() { //BGMの再生時間を取得
return atomSourceBGM.time;
}
}
とりあえず、今使うものだけを羅列。
キューシート名や、その役割は作る人によって異なる。
使ってみる
音を使うオブジェクトに、SoundManager.csをアタッチ。
他のスクリプトから呼び出すときは、
hogehoge.cs
private SoundManager sm;
void Start() {
sm = gameObject.GetComponent<SoundManager>();
sm.PlayBGM("sample"); // カッコ内は曲名
}
といった感じで使う。(ここでは、Start()内でBGMとしてsampleを流す)