示例#1
0
        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);
        }
示例#2
0
        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);
        }
示例#3
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);
        }
示例#4
0
        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
        }
示例#5
0
        private static async UniTask StopAfterInternal(AudioHandle handle, float time)
        {
            await UniTask.Delay((int)(time * 1000));

            handle.Stop(0);
        }