HomeSoundEffectController.cs 791 B

12345678910111213141516171819202122232425262728293031323334
  1. using System.Collections;
  2. using UnityEngine;
  3. /* Home场景全局控制音效播放
  4. * 本代码挂在在Home场景的Player上
  5. */
  6. public class HomeSoundEffectController: MonoBehaviour
  7. {
  8. public static HomeSoundEffectController Instance;
  9. //音效相关
  10. public AudioSource audioSource;
  11. public AudioClip[] soundEffect;
  12. void Awake()
  13. {
  14. // 单例模式,确保只有一个GlobalSoundManager
  15. if (Instance == null)
  16. {
  17. Instance = this;
  18. //DontDestroyOnLoad(gameObject); // 必须关掉否则会导致原场景destroy不能执行
  19. }
  20. else
  21. {
  22. Destroy(gameObject);
  23. }
  24. }
  25. public void PlaySoundEffect(int id)
  26. {
  27. audioSource.PlayOneShot(soundEffect[id]);
  28. }
  29. }