141 lines
3.7 KiB
C#
141 lines
3.7 KiB
C#
using System.Collections.Generic;
|
|
using Cysharp.Threading.Tasks;
|
|
using UnityEngine;
|
|
using UnityEngine.SceneManagement;
|
|
|
|
public enum AudioType
|
|
{
|
|
MUSIC,
|
|
SOUND
|
|
}
|
|
|
|
namespace PhxhSDK
|
|
{
|
|
public class AudioMgrBase : Singlenton<AudioMgrBase>, IStart, IInitable, IUpdatable
|
|
{
|
|
private float _vol;
|
|
private float _musicVol;
|
|
private float _soundVol;
|
|
|
|
protected Dictionary<int, Audio> MusicAudio;
|
|
protected Dictionary<int, Audio> SoundsAudio;
|
|
|
|
protected static Queue<Audio> FreeMusicAudio;
|
|
protected static Queue<Audio> FreeSoundsAudio;
|
|
|
|
protected bool Initialized = false;
|
|
|
|
/// <summary>
|
|
/// When set to true, new Audios that have the same audio clip as any other Audio, will be ignored
|
|
/// </summary>
|
|
public bool IgnoreDuplicateMusic { get; set; }
|
|
|
|
/// <summary>
|
|
/// When set to true, new Audios that have the same audio clip as any other Audio, will be ignored
|
|
/// </summary>
|
|
public bool IgnoreDuplicateSounds { get; set; }
|
|
|
|
public float GlobalVolume
|
|
{
|
|
get { return _vol; }
|
|
set { _vol = value; }
|
|
}
|
|
|
|
public float GlobalMusicVolume
|
|
{
|
|
get { return _musicVol; }
|
|
set { _musicVol = value; }
|
|
}
|
|
|
|
public float GlobalSoundsVolume
|
|
{
|
|
get { return _soundVol; }
|
|
set { _soundVol = value; }
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
|
|
}
|
|
|
|
public void Init()
|
|
{
|
|
if (!Initialized)
|
|
{
|
|
MusicAudio = new Dictionary<int, Audio>();
|
|
SoundsAudio = new Dictionary<int, Audio>();
|
|
FreeMusicAudio = new Queue<Audio>();
|
|
FreeSoundsAudio = new Queue<Audio>();
|
|
|
|
IgnoreDuplicateMusic = false;
|
|
IgnoreDuplicateSounds = false;
|
|
|
|
Initialized = true;
|
|
}
|
|
|
|
SceneManager.sceneLoaded += OnSceneLoaded;
|
|
}
|
|
|
|
public void Release()
|
|
{
|
|
|
|
}
|
|
|
|
public void Update(float dt)
|
|
{
|
|
|
|
}
|
|
|
|
protected void OnSceneLoaded(Scene scene, LoadSceneMode mode)
|
|
{
|
|
List<int> keys = new List<int>(MusicAudio.Keys);
|
|
foreach (var key in keys)
|
|
{
|
|
if (MusicAudio.ContainsKey(key))
|
|
{
|
|
var music = MusicAudio[key];
|
|
if (!music.persist && music.activated)
|
|
{
|
|
Object.Destroy(music.audioSource);
|
|
MusicAudio.Remove(key);
|
|
}
|
|
}
|
|
}
|
|
|
|
keys = new List<int>(SoundsAudio.Keys);
|
|
foreach (var key in keys)
|
|
{
|
|
if (MusicAudio.ContainsKey(key))
|
|
{
|
|
var sound = SoundsAudio[key];
|
|
if (!sound.persist && sound.activated)
|
|
{
|
|
Object.Destroy(sound.audioSource);
|
|
MusicAudio.Remove(key);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//管理器的外部播放接口
|
|
public virtual async UniTask PlayAudio(string key, Transform trans = null)
|
|
{
|
|
|
|
}
|
|
|
|
public virtual async UniTask PlayAudio(string key, Vector3 pos)
|
|
{
|
|
|
|
}
|
|
|
|
public virtual async UniTask PlayAudio(int id, Transform trans = null)
|
|
{
|
|
|
|
}
|
|
|
|
public virtual async UniTask PlayAudio(int id, Vector3 pos)
|
|
{
|
|
|
|
}
|
|
}
|
|
} |