SoundMgr.cs 1013 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SoundMgr : MonoBehaviour
  5. {
  6. public AudioSource audiosource;
  7. public static SoundMgr _instance;
  8. void Awake()
  9. {
  10. audiosource = gameObject.AddComponent<AudioSource>();
  11. audiosource.playOnAwake = false; //playOnAwake设为false时,通过调用play()方法启用
  12. _instance = this;
  13. }
  14. //private void Update()
  15. //{
  16. // if (Input.GetMouseButtonDown(1))
  17. // {
  18. // Debug.Log("AAA");
  19. // PlayMusicByName("tishi");
  20. // }
  21. //}
  22. //如果当前有其他音频正在播放,停止当前音频,播放下一个
  23. public void PlayMusicByName(string name, bool loop)
  24. {
  25. AudioClip clip = Resources.Load<AudioClip>("Sounds/" + name);
  26. if (audiosource.isPlaying)
  27. {
  28. audiosource.Stop();
  29. }
  30. audiosource.clip = clip;
  31. audiosource.loop = loop;
  32. audiosource.Play();
  33. }
  34. }