DogBarkController.cs 1.0 KB

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