123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434 |
- //------------------------------------------------------------
- // Game Framework
- // Copyright © 2013-2021 loyalsoft. All rights reserved.
- // Homepage: http://www.game7000.com/
- // Feedback: http://www.game7000.com/
- //------------------------------------------------------------
- using GameFramework;
- using GameFramework.Sound;
- using System;
- using System.Collections;
- using UnityEngine;
- using UnityEngine.Audio;
- namespace UnityGameFramework.Runtime
- {
- /// <summary>
- /// 默认声音代理辅助器。
- /// </summary>
- public class DefaultSoundAgentHelper : SoundAgentHelperBase
- {
- private Transform m_CachedTransform = null;
- private AudioSource m_AudioSource = null;
- private EntityLogic m_BindingEntityLogic = null;
- private float m_VolumeWhenPause = 0f;
- private bool m_ApplicationPauseFlag = false;
- private EventHandler<ResetSoundAgentEventArgs> m_ResetSoundAgentEventHandler = null;
- /// <summary>
- /// 获取当前是否正在播放。
- /// </summary>
- public override bool IsPlaying
- {
- get
- {
- return m_AudioSource.isPlaying;
- }
- }
- /// <summary>
- /// 获取声音长度。
- /// </summary>
- public override float Length
- {
- get
- {
- return m_AudioSource.clip != null ? m_AudioSource.clip.length : 0f;
- }
- }
- /// <summary>
- /// 获取或设置播放位置。
- /// </summary>
- public override float Time
- {
- get
- {
- return m_AudioSource.time;
- }
- set
- {
- m_AudioSource.time = value;
- }
- }
- /// <summary>
- /// 获取或设置是否静音。
- /// </summary>
- public override bool Mute
- {
- get
- {
- return m_AudioSource.mute;
- }
- set
- {
- m_AudioSource.mute = value;
- }
- }
- /// <summary>
- /// 获取或设置是否循环播放。
- /// </summary>
- public override bool Loop
- {
- get
- {
- return m_AudioSource.loop;
- }
- set
- {
- m_AudioSource.loop = value;
- }
- }
- /// <summary>
- /// 获取或设置声音优先级。
- /// </summary>
- public override int Priority
- {
- get
- {
- return 128 - m_AudioSource.priority;
- }
- set
- {
- m_AudioSource.priority = 128 - value;
- }
- }
- /// <summary>
- /// 获取或设置音量大小。
- /// </summary>
- public override float Volume
- {
- get
- {
- return m_AudioSource.volume;
- }
- set
- {
- m_AudioSource.volume = value;
- }
- }
- /// <summary>
- /// 获取或设置声音音调。
- /// </summary>
- public override float Pitch
- {
- get
- {
- return m_AudioSource.pitch;
- }
- set
- {
- m_AudioSource.pitch = value;
- }
- }
- /// <summary>
- /// 获取或设置声音立体声声相。
- /// </summary>
- public override float PanStereo
- {
- get
- {
- return m_AudioSource.panStereo;
- }
- set
- {
- m_AudioSource.panStereo = value;
- }
- }
- /// <summary>
- /// 获取或设置声音空间混合量。
- /// </summary>
- public override float SpatialBlend
- {
- get
- {
- return m_AudioSource.spatialBlend;
- }
- set
- {
- m_AudioSource.spatialBlend = value;
- }
- }
- /// <summary>
- /// 获取或设置声音最大距离。
- /// </summary>
- public override float MaxDistance
- {
- get
- {
- return m_AudioSource.maxDistance;
- }
- set
- {
- m_AudioSource.maxDistance = value;
- }
- }
- /// <summary>
- /// 获取或设置声音多普勒等级。
- /// </summary>
- public override float DopplerLevel
- {
- get
- {
- return m_AudioSource.dopplerLevel;
- }
- set
- {
- m_AudioSource.dopplerLevel = value;
- }
- }
- /// <summary>
- /// 获取或设置声音代理辅助器所在的混音组。
- /// </summary>
- public override AudioMixerGroup AudioMixerGroup
- {
- get
- {
- return m_AudioSource.outputAudioMixerGroup;
- }
- set
- {
- m_AudioSource.outputAudioMixerGroup = value;
- }
- }
- /// <summary>
- /// 重置声音代理事件。
- /// </summary>
- public override event EventHandler<ResetSoundAgentEventArgs> ResetSoundAgent
- {
- add
- {
- m_ResetSoundAgentEventHandler += value;
- }
- remove
- {
- m_ResetSoundAgentEventHandler -= value;
- }
- }
- /// <summary>
- /// 播放声音。
- /// </summary>
- /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
- public override void Play(float fadeInSeconds)
- {
- StopAllCoroutines();
- m_AudioSource.Play();
- if (fadeInSeconds > 0f)
- {
- float volume = m_AudioSource.volume;
- m_AudioSource.volume = 0f;
- StartCoroutine(FadeToVolume(m_AudioSource, volume, fadeInSeconds));
- }
- }
- /// <summary>
- /// 停止播放声音。
- /// </summary>
- /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
- public override void Stop(float fadeOutSeconds)
- {
- StopAllCoroutines();
- if (fadeOutSeconds > 0f && gameObject.activeInHierarchy)
- {
- StartCoroutine(StopCo(fadeOutSeconds));
- }
- else
- {
- m_AudioSource.Stop();
- }
- }
- /// <summary>
- /// 暂停播放声音。
- /// </summary>
- /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
- public override void Pause(float fadeOutSeconds)
- {
- StopAllCoroutines();
- m_VolumeWhenPause = m_AudioSource.volume;
- if (fadeOutSeconds > 0f && gameObject.activeInHierarchy)
- {
- StartCoroutine(PauseCo(fadeOutSeconds));
- }
- else
- {
- m_AudioSource.Pause();
- }
- }
- /// <summary>
- /// 恢复播放声音。
- /// </summary>
- /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
- public override void Resume(float fadeInSeconds)
- {
- StopAllCoroutines();
- m_AudioSource.UnPause();
- if (fadeInSeconds > 0f)
- {
- StartCoroutine(FadeToVolume(m_AudioSource, m_VolumeWhenPause, fadeInSeconds));
- }
- else
- {
- m_AudioSource.volume = m_VolumeWhenPause;
- }
- }
- /// <summary>
- /// 重置声音代理辅助器。
- /// </summary>
- public override void Reset()
- {
- m_CachedTransform.localPosition = Vector3.zero;
- m_AudioSource.clip = null;
- m_BindingEntityLogic = null;
- m_VolumeWhenPause = 0f;
- }
- /// <summary>
- /// 设置声音资源。
- /// </summary>
- /// <param name="soundAsset">声音资源。</param>
- /// <returns>是否设置声音资源成功。</returns>
- public override bool SetSoundAsset(object soundAsset)
- {
- AudioClip audioClip = soundAsset as AudioClip;
- if (audioClip == null)
- {
- return false;
- }
- m_AudioSource.clip = audioClip;
- return true;
- }
- /// <summary>
- /// 设置声音绑定的实体。
- /// </summary>
- /// <param name="bindingEntity">声音绑定的实体。</param>
- public override void SetBindingEntity(Entity bindingEntity)
- {
- m_BindingEntityLogic = bindingEntity.Logic;
- if (m_BindingEntityLogic != null)
- {
- UpdateAgentPosition();
- return;
- }
- if (m_ResetSoundAgentEventHandler != null)
- {
- ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
- m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
- ReferencePool.Release(resetSoundAgentEventArgs);
- }
- }
- /// <summary>
- /// 设置声音所在的世界坐标。
- /// </summary>
- /// <param name="worldPosition">声音所在的世界坐标。</param>
- public override void SetWorldPosition(Vector3 worldPosition)
- {
- m_CachedTransform.position = worldPosition;
- }
- private void Awake()
- {
- m_CachedTransform = transform;
- m_AudioSource = gameObject.GetOrAddComponent<AudioSource>();
- m_AudioSource.playOnAwake = false;
- m_AudioSource.rolloffMode = AudioRolloffMode.Custom;
- }
- private void Update()
- {
- if (!m_ApplicationPauseFlag && !IsPlaying && m_AudioSource.clip != null && m_ResetSoundAgentEventHandler != null)
- {
- ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
- m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
- ReferencePool.Release(resetSoundAgentEventArgs);
- return;
- }
- if (m_BindingEntityLogic != null)
- {
- UpdateAgentPosition();
- }
- }
- private void OnApplicationPause(bool pause)
- {
- m_ApplicationPauseFlag = pause;
- }
- private void UpdateAgentPosition()
- {
- if (m_BindingEntityLogic.Available)
- {
- m_CachedTransform.position = m_BindingEntityLogic.CachedTransform.position;
- return;
- }
- if (m_ResetSoundAgentEventHandler != null)
- {
- ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
- m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
- ReferencePool.Release(resetSoundAgentEventArgs);
- }
- }
- private IEnumerator StopCo(float fadeOutSeconds)
- {
- yield return FadeToVolume(m_AudioSource, 0f, fadeOutSeconds);
- m_AudioSource.Stop();
- }
- private IEnumerator PauseCo(float fadeOutSeconds)
- {
- yield return FadeToVolume(m_AudioSource, 0f, fadeOutSeconds);
- m_AudioSource.Pause();
- }
- private IEnumerator FadeToVolume(AudioSource audioSource, float volume, float duration)
- {
- float time = 0f;
- float originalVolume = audioSource.volume;
- while (time < duration)
- {
- time += UnityEngine.Time.deltaTime;
- audioSource.volume = Mathf.Lerp(originalVolume, volume, time / duration);
- yield return new WaitForEndOfFrame();
- }
- audioSource.volume = volume;
- }
- }
- }
|