private IEnumerator Play() { yield return(new WaitUntil(() => IsAudioPlayerReady)); if (AudioPlayerToUse == null) { Debug.LogError("PlayAmbientAudio does not have AudioPlayer assigned.", this); yield break; } if (AudioPlayerToUse.Clips.Count == 0) { yield break; } int index = UseRandomClip ? Random.Range(0, AudioPlayerToUse.Clips.Count) : ClipIndex; if (index == -1) { Debug.LogError("No clip is selected in PlayAmbientAudio", this); yield break; } _handle = AudioPlayerToUse.Play(index, tracking: _trackingTarget); }
private static IEnumerator StopAfterInternal(AudioHandle handle, float time) { #if EDITOR_COROUTINES && UNITY_EDITOR if (!Application.isPlaying) { yield return(new EditorWaitForSeconds(time)); } else { yield return(new WaitForSeconds(time)); } #else yield return(new WaitForSeconds(time)); #endif handle.Stop(0); }
public static AudioSource Spawn(AudioHandle handle, Vector3?position, Transform tracking, float despawnTime) { AudioSource source; if (Pool.Count > 0) { source = Pool.Dequeue(); source.gameObject.SetActive(true); } else { var go = new GameObject("Audio Source", typeof(AudioSource), typeof(Tracker)) { hideFlags = HideFlags.HideAndDontSave }; if (Application.isPlaying) { Object.DontDestroyOnLoad(go); } source = go.GetComponent <AudioSource>(); } source.spatialBlend = (position != null || tracking != null) ? 1 : 0; if (tracking != null) { var tracker = source.GetComponent <Tracker>(); tracker.Target = tracking; tracker.Offset = position ?? Vector3.zero; } source.transform.position = position ?? Vector3.zero; if (despawnTime > 0) { StopAfter(handle, despawnTime); } return(source); }
private static void StopAfter(AudioHandle handle, float time) { // Select despawn strategy based on what packages are present #if UNITASK StopAfterInternal(handle, time).Forget(); #else #if EDITOR_COROUTINES && UNITY_EDITOR if (Application.isPlaying) { CoroutineRunner.RunCoroutine(StopAfterInternal(handle, time)); } else { EditorCoroutineUtility.StartCoroutineOwnerless(StopAfterInternal(handle, time)); } #else CoroutineRunner.RunCoroutine(StopAfterInternal(handle, time)); #endif #endif }
private static async UniTask StopAfterInternal(AudioHandle handle, float time) { await UniTask.Delay((int)(time * 1000)); handle.Stop(0); }