using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AudioManager : MonoSingleton<AudioManager>
{
public Dictionary<string, AudioClip> audioClipList = new Dictionary<string, AudioClip>();
private AudioSource audioSource;
public float PlayAudioClip(string clipName)
{
AudioClip audio = GetAudioClip(clipName);
if (audio == null)
{
Debug.Log("未找到指定名称的音频文件");
}
else
{
if (!transform.GetComponent<AudioSource>())
{
transform.gameObject.AddComponent<AudioSource>();
}
audioSource = transform.GetComponent<AudioSource>();
audioSource.clip = audio;
audioSource.Play();
}
return audio.length;
}
public AudioClip GetAudioClip(string clipName)
{
AudioClip clip = null;
if (audioClipList.ContainsKey(clipName))
{
audioClipList.TryGetValue(clipName, out clip);
return clip;
}
clip = Resources.Load("AudioClip/" + clipName) as AudioClip;
if (clip != null)
{
audioClipList.Add(clipName, clip);
}
return clip;
}
}