// 此类别集成MonoBehaviour
[System.Serializable] public class AudioData { public AUDIO_TYPE m_AudioType; public AudioClip m_Clip; }
public class AudioManager : MonoBehaviour { public List<AudioData> m_SoundList; public List<AudioData> m_BgList; }
using System; using System.Collections.Generic; using UnityEngine; public class AudioControl : MonoBehaviour { private Dictionary<AUDIO_TYPE, AudioClip> m_AudioList = new Dictionary<AUDIO_TYPE, AudioClip>(); void Start() { AudioManager am = GameObject.FindObjectOfType<AudioManager>(); for (int i = 0; i < am.m_SoundList.Count; i++) { m_AudioList.Add(am.m_SoundList[i].m_AudioType, am.m_SoundList[i].m_Clip); } Destroy(GameObject.FindObjectOfType<AudioManager>().gameObject); } void OnGUI() { if (GUILayout.Button("播放1")) { Camera.main.GetComponent<AudioSource>().clip = m_AudioList[AUDIO_TYPE.AT_SCENE_BGM]; Camera.main.GetComponent<AudioSource>().Play(); } if (GUILayout.Button("播放2")) { Camera.main.GetComponent<AudioSource>().clip = m_AudioList[AUDIO_TYPE.AT_OPEN_UI]; Camera.main.GetComponent<AudioSource>().Play(); } if (GUILayout.Button("播放3")) { Camera.main.GetComponent<AudioSource>().clip = m_AudioList[AUDIO_TYPE.AT_UI_OPEN_CLOSE]; Camera.main.GetComponent<AudioSource>().Play(); } if (GUILayout.Button("播放4")) { Camera.main.GetComponent<AudioSource>().clip = m_AudioList[AUDIO_TYPE.AT_UI_WIDGET]; Camera.main.GetComponent<AudioSource>().Play(); } } }
原文地址:http://www.cnblogs.com/123ing/p/3836410.html