54 lines
1.5 KiB
C#
54 lines
1.5 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
|
|
namespace BF
|
|
{
|
|
public class SoundManager : ManagerBase
|
|
{
|
|
static SoundManager instance;
|
|
public static SoundManager Create()
|
|
{
|
|
BFLog.LogAssert(instance == null, "This method only allows BFMain to call once");
|
|
instance = new SoundManager();
|
|
return instance;
|
|
}
|
|
|
|
private SoundManager() { }
|
|
|
|
private static GameObject soundObject;
|
|
private static AudioSource musicAudioSource;
|
|
|
|
public override void Init()
|
|
{
|
|
soundObject = new GameObject("SoundManager");
|
|
soundObject.AddComponent<AudioListener>();
|
|
UnityEngine.GameObject.DontDestroyOnLoad(soundObject);
|
|
}
|
|
|
|
public AudioSource getMusicAudioSource()
|
|
{
|
|
if (musicAudioSource == null)
|
|
{
|
|
musicAudioSource = soundObject.AddComponent<AudioSource>();
|
|
}
|
|
return musicAudioSource;
|
|
}
|
|
|
|
public AudioSource addEffectAudioSource()
|
|
{
|
|
AudioSource effectAudioSource = soundObject.AddComponent<AudioSource>();
|
|
return effectAudioSource;
|
|
}
|
|
|
|
public void clearAudioSource()
|
|
{
|
|
AudioSource[] audioSources = soundObject.GetComponents<AudioSource>();
|
|
for (int i = 0; i < audioSources.Length; i++)
|
|
{
|
|
GameObject.Destroy(audioSources[i]);
|
|
}
|
|
}
|
|
}
|
|
}
|