コード例 #1
0
ファイル: SfxService.cs プロジェクト: Fantamstick/unice
        /// <summary>
        /// Play Sfx audio track.
        /// </summary>
        /// <param name="audio">Audio clip and settings SO</param>
        /// <param name="followTarget">Target for the sfx to follow when playing.</param>
        public CancellationTokenSource Play(ISfxAudio audio, Transform followTarget = null)
        {
            PoolTracker = PoolTracker ?? new SfxPoolTracker(PoolSize);

            try
            {
                // set transform parent
                if (followTarget == null)
                {
                    followTarget = Mic.Transform;
                }

                // borrow sfx from pool
                SfxPlayComponent sfxPlayComponent = PoolTracker.Borrow(audio, followTarget);

                if (sfxPlayComponent == null)
                {
                    return(null);
                }

                // play sfx and return cancellation token source
                return(sfxPlayComponent.Play(audio, audioMixerGroup, followTarget));
            }
            catch (ErrPoolExhausted)
            {
                Debug.Log("Pool is full and audio is low priority. Skipping...");

                return(null);
            }
        }
コード例 #2
0
ファイル: SfxPlayComponent.cs プロジェクト: Fantamstick/unice
        public CancellationTokenSource Play(ISfxAudio audioSO, AudioMixerGroup audioMixerGroup, Transform followTarget)
        {
            audioData = audioSO;

            // setup position and priority settings
            FollowTarget = followTarget;
            Priority     = audioSO.Priority;

            // setup audio source
            audioSO.Audio.LoadAsync().ContinueWith(() => {
                AudioSource = GetComponent <AudioSource>();
                AudioSource.outputAudioMixerGroup = audioMixerGroup;
                AudioSource.clip   = audioSO.Audio.GetAudioClip();
                AudioSource.loop   = audioSO.Audio.Details.Looping;
                AudioSource.volume = audioSO.Audio.Details.MaxVolume;
                AudioSource.gameObject.SetActive(true);

                // play
                AudioSource.Play();
            });

            // create cancellation token
            var cts = new CancellationTokenSource();

            Ct = cts.Token;

            return(cts);
        }
コード例 #3
0
        /// <summary>
        /// Borrow SFX play instance.
        /// </summary>
        public SfxPlayComponent Borrow(ISfxAudio audio, Transform followTarget)
        {
            if (pool.ItemsAvailable == 0)
            {
                // obtain ranks of the requested sfx and compare it with the lowest ranked sfx.
                int rank       = audio.Priority.GetRank(followTarget);
                int lowestRank = lowestPrioritySfx.GetRank();

                // don't play sfx if it has the lowest rank.
                if (rank == lowestRank)
                {
                    return(null);
                }

                // stop lowest priority sfx and return it to pool.
                Stop(lowestPrioritySfx);
            }

            try
            {
                SfxPlayComponent sfx = pool.Borrow();

                activeSfx.Add(sfx);

                return(sfx);
            }
            catch (ErrPoolExhausted e)
            {
                Debug.LogError(e.Message);

                return(null);
            }
        }