SoundGameEffectController.cs 759 B

123456789101112131415161718192021222324252627282930313233
  1. using UnityEngine;
  2. /* WalkDogs场景,音乐游戏控制音效播放
  3. * 本代码挂在Player上
  4. */
  5. public class SoundGameEffectController: MonoBehaviour
  6. {
  7. public static SoundGameEffectController 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); // 必须关掉否则会导致原场景destroy不能执行
  18. }
  19. else
  20. {
  21. Destroy(gameObject);
  22. }
  23. }
  24. public void PlaySoundEffect(int id)
  25. {
  26. audioSource.PlayOneShot(soundEffect[id]);
  27. }
  28. }