HomeSoundEffectController.cs 691 B

123456789101112131415161718192021222324252627282930313233
  1. using System.Net.NetworkInformation;
  2. using UnityEngine;
  3. /* Home场景全局控制音效播放
  4. */
  5. public class HomeSoundEffectController: MonoBehaviour
  6. {
  7. public static HomeSoundEffectController Instance;
  8. //音效相关
  9. public AudioSource audioSource;
  10. public AudioClip[] soundEffect;
  11. void Awake()
  12. {
  13. // 单例模式,确保只有一个GlobalSoundManager
  14. if (Instance == null)
  15. {
  16. Instance = this;
  17. DontDestroyOnLoad(gameObject);
  18. }
  19. else
  20. {
  21. Destroy(gameObject);
  22. }
  23. }
  24. public void PlaySoundEffect(int id)
  25. {
  26. audioSource.PlayOneShot(soundEffect[id]);
  27. }
  28. }