You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
55 lines
1.5 KiB
using System.Collections.Generic; |
|
using UnityEngine; |
|
|
|
/// <summary> |
|
/// 音频管理 |
|
/// </summary> |
|
public class AudioManager : Singleton<AudioManager> |
|
{ |
|
private Dictionary<string, AudioPlayer> AudioPlayers = new Dictionary<string, AudioPlayer>(); |
|
// 创建AudioPlayer |
|
private AudioPlayer InstantiateAudioPlayerWithName(string name) |
|
{ |
|
GameObject audioPlayer = new GameObject(name); |
|
audioPlayer.transform.SetParent(transform); |
|
AudioPlayers.Add(name, audioPlayer.AddComponent<AudioPlayer>()); |
|
return AudioPlayers[name]; |
|
} |
|
/// <summary> |
|
/// 获取名字为name的AudioPlayer |
|
/// </summary> |
|
/// <param name="name">AudioPlayer的名字</param> |
|
/// <returns>名字为name的AudioPlayer</returns> |
|
public AudioPlayer GetAudioPlayer(string name) |
|
{ |
|
if (AudioPlayers.ContainsKey(name)) |
|
{ |
|
return AudioPlayers[name]; |
|
} |
|
else |
|
{ |
|
return InstantiateAudioPlayerWithName(name); |
|
} |
|
} |
|
/// <summary> |
|
/// 停止播放名字为name的AudioPlayer |
|
/// </summary> |
|
/// <param name="name">AudioPlayer的名字</param> |
|
public void Stop(string name) |
|
{ |
|
if (AudioPlayers.ContainsKey(name)) |
|
{ |
|
AudioPlayers[name].Stop(); |
|
} |
|
} |
|
public void StopAllWithout(string name) |
|
{ |
|
foreach (var item in AudioPlayers) |
|
{ |
|
if (item.Key != name) |
|
{ |
|
item.Value.Stop(); |
|
} |
|
} |
|
} |
|
}
|
|
|