1234567891011121314151617181920212223242526272829303132333435363738394041 |
- using System.Collections;
- using System.Net.NetworkInformation;
- using UnityEngine;
- /* Home场景全局控制音效播放
- * 本代码挂在在Home场景的Player上
- */
- public class DogBarkController: MonoBehaviour
- {
- public static DogBarkController Instance;
- //音效相关
- public AudioSource audioSource;
- public AudioClip[] soundEffect;
- void Awake()
- {
- // 单例模式,确保只有一个GlobalSoundManager
- if (Instance == null)
- {
- Instance = this;
- //DontDestroyOnLoad(gameObject); // 必须关掉否则会导致原场景destroy不能执行
- }
- else
- {
- Destroy(gameObject);
- }
- }
- public void PlayDogBarkWithDelay(int id, float delay=0.0f)
- {
- StartCoroutine(PlayDogBarkCorountine(id, delay));
- }
- private IEnumerator PlayDogBarkCorountine(int id, float delay=0.0f)
- {
- yield return new WaitForSeconds(delay);
- audioSource.PlayOneShot(soundEffect[id]);
- }
- }
|