using System.Collections; using System.Collections.Generic; using UnityEngine; public class SoundMgr : MonoBehaviour { public AudioSource audiosource; public static SoundMgr _instance; void Awake() { audiosource = gameObject.AddComponent(); audiosource.playOnAwake = false; //playOnAwake设为false时,通过调用play()方法启用 _instance = this; } //private void Update() //{ // if (Input.GetMouseButtonDown(1)) // { // Debug.Log("AAA"); // PlayMusicByName("tishi"); // } //} //如果当前有其他音频正在播放,停止当前音频,播放下一个 public void PlayMusicByName(string name, bool loop) { AudioClip clip = Resources.Load("Sounds/" + name); if (audiosource.isPlaying) { audiosource.Stop(); } audiosource.clip = clip; audiosource.loop = loop; audiosource.Play(); } }