DefaultSoundAgentHelper.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using GameFramework;
  8. using GameFramework.Sound;
  9. using System;
  10. using System.Collections;
  11. using UnityEngine;
  12. using UnityEngine.Audio;
  13. namespace UnityGameFramework.Runtime
  14. {
  15. /// <summary>
  16. /// 默认声音代理辅助器。
  17. /// </summary>
  18. public class DefaultSoundAgentHelper : SoundAgentHelperBase
  19. {
  20. private Transform m_CachedTransform = null;
  21. private AudioSource m_AudioSource = null;
  22. private EntityLogic m_BindingEntityLogic = null;
  23. private float m_VolumeWhenPause = 0f;
  24. private bool m_ApplicationPauseFlag = false;
  25. private EventHandler<ResetSoundAgentEventArgs> m_ResetSoundAgentEventHandler = null;
  26. /// <summary>
  27. /// 获取当前是否正在播放。
  28. /// </summary>
  29. public override bool IsPlaying
  30. {
  31. get
  32. {
  33. return m_AudioSource.isPlaying;
  34. }
  35. }
  36. /// <summary>
  37. /// 获取声音长度。
  38. /// </summary>
  39. public override float Length
  40. {
  41. get
  42. {
  43. return m_AudioSource.clip != null ? m_AudioSource.clip.length : 0f;
  44. }
  45. }
  46. /// <summary>
  47. /// 获取或设置播放位置。
  48. /// </summary>
  49. public override float Time
  50. {
  51. get
  52. {
  53. return m_AudioSource.time;
  54. }
  55. set
  56. {
  57. m_AudioSource.time = value;
  58. }
  59. }
  60. /// <summary>
  61. /// 获取或设置是否静音。
  62. /// </summary>
  63. public override bool Mute
  64. {
  65. get
  66. {
  67. return m_AudioSource.mute;
  68. }
  69. set
  70. {
  71. m_AudioSource.mute = value;
  72. }
  73. }
  74. /// <summary>
  75. /// 获取或设置是否循环播放。
  76. /// </summary>
  77. public override bool Loop
  78. {
  79. get
  80. {
  81. return m_AudioSource.loop;
  82. }
  83. set
  84. {
  85. m_AudioSource.loop = value;
  86. }
  87. }
  88. /// <summary>
  89. /// 获取或设置声音优先级。
  90. /// </summary>
  91. public override int Priority
  92. {
  93. get
  94. {
  95. return 128 - m_AudioSource.priority;
  96. }
  97. set
  98. {
  99. m_AudioSource.priority = 128 - value;
  100. }
  101. }
  102. /// <summary>
  103. /// 获取或设置音量大小。
  104. /// </summary>
  105. public override float Volume
  106. {
  107. get
  108. {
  109. return m_AudioSource.volume;
  110. }
  111. set
  112. {
  113. m_AudioSource.volume = value;
  114. }
  115. }
  116. /// <summary>
  117. /// 获取或设置声音音调。
  118. /// </summary>
  119. public override float Pitch
  120. {
  121. get
  122. {
  123. return m_AudioSource.pitch;
  124. }
  125. set
  126. {
  127. m_AudioSource.pitch = value;
  128. }
  129. }
  130. /// <summary>
  131. /// 获取或设置声音立体声声相。
  132. /// </summary>
  133. public override float PanStereo
  134. {
  135. get
  136. {
  137. return m_AudioSource.panStereo;
  138. }
  139. set
  140. {
  141. m_AudioSource.panStereo = value;
  142. }
  143. }
  144. /// <summary>
  145. /// 获取或设置声音空间混合量。
  146. /// </summary>
  147. public override float SpatialBlend
  148. {
  149. get
  150. {
  151. return m_AudioSource.spatialBlend;
  152. }
  153. set
  154. {
  155. m_AudioSource.spatialBlend = value;
  156. }
  157. }
  158. /// <summary>
  159. /// 获取或设置声音最大距离。
  160. /// </summary>
  161. public override float MaxDistance
  162. {
  163. get
  164. {
  165. return m_AudioSource.maxDistance;
  166. }
  167. set
  168. {
  169. m_AudioSource.maxDistance = value;
  170. }
  171. }
  172. /// <summary>
  173. /// 获取或设置声音多普勒等级。
  174. /// </summary>
  175. public override float DopplerLevel
  176. {
  177. get
  178. {
  179. return m_AudioSource.dopplerLevel;
  180. }
  181. set
  182. {
  183. m_AudioSource.dopplerLevel = value;
  184. }
  185. }
  186. /// <summary>
  187. /// 获取或设置声音代理辅助器所在的混音组。
  188. /// </summary>
  189. public override AudioMixerGroup AudioMixerGroup
  190. {
  191. get
  192. {
  193. return m_AudioSource.outputAudioMixerGroup;
  194. }
  195. set
  196. {
  197. m_AudioSource.outputAudioMixerGroup = value;
  198. }
  199. }
  200. /// <summary>
  201. /// 重置声音代理事件。
  202. /// </summary>
  203. public override event EventHandler<ResetSoundAgentEventArgs> ResetSoundAgent
  204. {
  205. add
  206. {
  207. m_ResetSoundAgentEventHandler += value;
  208. }
  209. remove
  210. {
  211. m_ResetSoundAgentEventHandler -= value;
  212. }
  213. }
  214. /// <summary>
  215. /// 播放声音。
  216. /// </summary>
  217. /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
  218. public override void Play(float fadeInSeconds)
  219. {
  220. StopAllCoroutines();
  221. m_AudioSource.Play();
  222. if (fadeInSeconds > 0f)
  223. {
  224. float volume = m_AudioSource.volume;
  225. m_AudioSource.volume = 0f;
  226. StartCoroutine(FadeToVolume(m_AudioSource, volume, fadeInSeconds));
  227. }
  228. }
  229. /// <summary>
  230. /// 停止播放声音。
  231. /// </summary>
  232. /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
  233. public override void Stop(float fadeOutSeconds)
  234. {
  235. StopAllCoroutines();
  236. if (fadeOutSeconds > 0f && gameObject.activeInHierarchy)
  237. {
  238. StartCoroutine(StopCo(fadeOutSeconds));
  239. }
  240. else
  241. {
  242. m_AudioSource.Stop();
  243. }
  244. }
  245. /// <summary>
  246. /// 暂停播放声音。
  247. /// </summary>
  248. /// <param name="fadeOutSeconds">声音淡出时间,以秒为单位。</param>
  249. public override void Pause(float fadeOutSeconds)
  250. {
  251. StopAllCoroutines();
  252. m_VolumeWhenPause = m_AudioSource.volume;
  253. if (fadeOutSeconds > 0f && gameObject.activeInHierarchy)
  254. {
  255. StartCoroutine(PauseCo(fadeOutSeconds));
  256. }
  257. else
  258. {
  259. m_AudioSource.Pause();
  260. }
  261. }
  262. /// <summary>
  263. /// 恢复播放声音。
  264. /// </summary>
  265. /// <param name="fadeInSeconds">声音淡入时间,以秒为单位。</param>
  266. public override void Resume(float fadeInSeconds)
  267. {
  268. StopAllCoroutines();
  269. m_AudioSource.UnPause();
  270. if (fadeInSeconds > 0f)
  271. {
  272. StartCoroutine(FadeToVolume(m_AudioSource, m_VolumeWhenPause, fadeInSeconds));
  273. }
  274. else
  275. {
  276. m_AudioSource.volume = m_VolumeWhenPause;
  277. }
  278. }
  279. /// <summary>
  280. /// 重置声音代理辅助器。
  281. /// </summary>
  282. public override void Reset()
  283. {
  284. m_CachedTransform.localPosition = Vector3.zero;
  285. m_AudioSource.clip = null;
  286. m_BindingEntityLogic = null;
  287. m_VolumeWhenPause = 0f;
  288. }
  289. /// <summary>
  290. /// 设置声音资源。
  291. /// </summary>
  292. /// <param name="soundAsset">声音资源。</param>
  293. /// <returns>是否设置声音资源成功。</returns>
  294. public override bool SetSoundAsset(object soundAsset)
  295. {
  296. AudioClip audioClip = soundAsset as AudioClip;
  297. if (audioClip == null)
  298. {
  299. return false;
  300. }
  301. m_AudioSource.clip = audioClip;
  302. return true;
  303. }
  304. /// <summary>
  305. /// 设置声音绑定的实体。
  306. /// </summary>
  307. /// <param name="bindingEntity">声音绑定的实体。</param>
  308. public override void SetBindingEntity(Entity bindingEntity)
  309. {
  310. m_BindingEntityLogic = bindingEntity.Logic;
  311. if (m_BindingEntityLogic != null)
  312. {
  313. UpdateAgentPosition();
  314. return;
  315. }
  316. if (m_ResetSoundAgentEventHandler != null)
  317. {
  318. ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
  319. m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
  320. ReferencePool.Release(resetSoundAgentEventArgs);
  321. }
  322. }
  323. /// <summary>
  324. /// 设置声音所在的世界坐标。
  325. /// </summary>
  326. /// <param name="worldPosition">声音所在的世界坐标。</param>
  327. public override void SetWorldPosition(Vector3 worldPosition)
  328. {
  329. m_CachedTransform.position = worldPosition;
  330. }
  331. private void Awake()
  332. {
  333. m_CachedTransform = transform;
  334. m_AudioSource = gameObject.GetOrAddComponent<AudioSource>();
  335. m_AudioSource.playOnAwake = false;
  336. m_AudioSource.rolloffMode = AudioRolloffMode.Custom;
  337. }
  338. private void Update()
  339. {
  340. if (!m_ApplicationPauseFlag && !IsPlaying && m_AudioSource.clip != null && m_ResetSoundAgentEventHandler != null)
  341. {
  342. ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
  343. m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
  344. ReferencePool.Release(resetSoundAgentEventArgs);
  345. return;
  346. }
  347. if (m_BindingEntityLogic != null)
  348. {
  349. UpdateAgentPosition();
  350. }
  351. }
  352. private void OnApplicationPause(bool pause)
  353. {
  354. m_ApplicationPauseFlag = pause;
  355. }
  356. private void UpdateAgentPosition()
  357. {
  358. if (m_BindingEntityLogic.Available)
  359. {
  360. m_CachedTransform.position = m_BindingEntityLogic.CachedTransform.position;
  361. return;
  362. }
  363. if (m_ResetSoundAgentEventHandler != null)
  364. {
  365. ResetSoundAgentEventArgs resetSoundAgentEventArgs = ResetSoundAgentEventArgs.Create();
  366. m_ResetSoundAgentEventHandler(this, resetSoundAgentEventArgs);
  367. ReferencePool.Release(resetSoundAgentEventArgs);
  368. }
  369. }
  370. private IEnumerator StopCo(float fadeOutSeconds)
  371. {
  372. yield return FadeToVolume(m_AudioSource, 0f, fadeOutSeconds);
  373. m_AudioSource.Stop();
  374. }
  375. private IEnumerator PauseCo(float fadeOutSeconds)
  376. {
  377. yield return FadeToVolume(m_AudioSource, 0f, fadeOutSeconds);
  378. m_AudioSource.Pause();
  379. }
  380. private IEnumerator FadeToVolume(AudioSource audioSource, float volume, float duration)
  381. {
  382. float time = 0f;
  383. float originalVolume = audioSource.volume;
  384. while (time < duration)
  385. {
  386. time += UnityEngine.Time.deltaTime;
  387. audioSource.volume = Mathf.Lerp(originalVolume, volume, time / duration);
  388. yield return new WaitForEndOfFrame();
  389. }
  390. audioSource.volume = volume;
  391. }
  392. }
  393. }