Beispiel #1
0
        public void Play(AudioAsset asset, object context)
        {
#if UNITY_EDITOR // for debugging purposes
            _GameObject.transform.SetAsFirstSibling();
#endif
            _GameObject.SetActive(true);
            Context = context;

            AudioSysUtil.ConfigureAudioSource(_AudioSource, asset);
            _AudioSource.Play();
        }
Beispiel #2
0
 public void Play(AudioAsset asset, object context)
 {
     if (FindFreeChannel(out AudioChannel channel))
     {
         channel.Play(asset, context);
     }
     else
     {
         Debug.LogWarningFormat("[Audio] All {0} channels(s) are full", _AudioChannels.Length);
     }
 }
        private void HandlePlayEvent(AudioEvent audioEvent)
        {
            AudioAsset asset = ResolveAsset(audioEvent);

            if (!asset)
            {
                return;
            }

            _AudioChannelPool.Play(asset,
                                   audioEvent);
        }
        private AudioAsset ResolveAsset(AudioEvent audioEvent)
        {
            AudioAsset asset = null;

            for (int i = 0; i < _Libraries.Length; i++)
            {
                asset = _Libraries[i].Resolve(audioEvent.Identifier);
                if (asset)
                {
                    break;
                }
            }

            AudioLog.Assert(asset, $"{audioEvent.Identifier} could not be found in any audio library.");
            return(asset);
        }
        public void Play(AudioAsset asset, AudioEvent audioEvent)
        {
            IsFree = false;

            if (audioEvent.FollowTransform)
            {
                _TransformFollower.StartFollowing(audioEvent.FollowTransform);
            }
            _GameObject.transform.position = audioEvent.WorldPosition;

            _Callbacks.OnUpdate -= Update;
            _Callbacks.OnUpdate += Update;

            _GameObject.SetActive(true);
            Context = audioEvent.Context;
            AudioSysUtil.ConfigureAudioSource(_AudioSource, asset);
            _AudioSource.Play();
            _GameObject.name = $"Audio Channel {_ChannelNumber}: {audioEvent.Identifier}";
        }
        /// <summary>
        /// Will configure an audio source according to the settings in an AudioAsset
        /// </summary>
        public static void ConfigureAudioSource(AudioSource audioSource, AudioAsset asset)
        {
            if (asset.AudioClips == null || asset.ClipCount == 0)
            {
                AudioLog.Warning($"tried playing asset {asset.name} but it has no clips.");
                return;
            }

            float     pitch = asset.GetPitch();
            AudioClip clip  = asset.GetClip();

            if (clip == null)
            {
                AudioLog.Warning($"{asset.name} asset returned null as clip.");
                return;
            }

            audioSource.spatialBlend = asset.SpatialBlend;
            audioSource.dopplerLevel = asset.Doppler;

            audioSource.loop                  = asset.IsLooped;
            audioSource.panStereo             = asset.Pan;
            audioSource.outputAudioMixerGroup = asset.AudioMixerGroup;
            audioSource.pitch                 = pitch;
            audioSource.clip                  = clip;
            audioSource.volume                = asset.Volume;
            audioSource.maxDistance           = asset.MaxDistance;
            audioSource.minDistance           = asset.MinDistance;

            if (asset.RandomStartPosition)
            {
                audioSource.timeSamples = Random.Range(0, clip.samples - 1);
            }
            else
            {
                audioSource.timeSamples = pitch < 0 ? (clip.samples - 1) : 0;
            }
        }
Beispiel #7
0
        public static void ConfigureAudioSource(AudioSource audioSource, AudioAsset asset)
        {
            if (asset.ClipCount == 0)
            {
                AudioLog.Warning($"tried playing asset {asset.name} but it has no clips.");
                return;
            }

            float     pitch = asset.GetPitch();
            AudioClip clip  = asset.GetClip();

            if (clip == null)
            {
                AudioLog.Warning($"{asset.name} asset returned null as clip.");
                return;
            }

            audioSource.panStereo             = asset.Pan;
            audioSource.outputAudioMixerGroup = asset.AudioMixerGroup;
            audioSource.pitch       = pitch;
            audioSource.clip        = clip;
            audioSource.volume      = asset.Volume;
            audioSource.timeSamples = pitch < 0 ? (clip.samples - 1) : 0;            // reverse clip if we play backwards
        }