经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 软件/图像 » unity » 查看文章
unity AudioSource播放完声音后要执行的函数或条件操作
来源:jb51  时间:2021/4/12 19:00:17  对本文有异议

将脚本挂在要判断声音是否播放完毕的物体上

  1. using System.Collections;
  2. using UnityEngine;
  3. using UnityEngine.Events;
  4. [RequireComponent(typeof(AudioSource))]
  5. public class AudioManager : MonoBehaviour
  6. {
  7. public static AudioManager instence = null;
  8. private AudioSource _audio;
  9. void Awake()
  10. {
  11. if (instence == null)
  12. {
  13. instence = this;
  14. }
  15. }
  16. void Start()
  17. {
  18. _audio = GetComponent<AudioSource>();
  19. }
  20. void Update()
  21. {
  22. //按下键盘按钮A键执行函数播放语音
  23. if (Input.GetKeyDown(KeyCode.A))
  24. {
  25. PlayAudioGameObject.GetComponent().clip
  26. }
  27. }
  28. //接受音频文件和是否重复播放
  29. public void PlayAudio(AudioClip clip, UnityAction callback = null, bool isLoop = false)
  30. {
  31. //获取自身音频文件进行播放并且不重复播放
  32. _audio.clip = clip;
  33. _audio.loop = isLoop;
  34. _audio.Play();
  35. //执行协成获取音频文件的时间
  36. StartCoroutine(AudioPlayFinished(_audio.clip.length, callback));
  37. }
  38. //执行协成函数 并且返回时间
  39. private IEnumerator AudioPlayFinished(float time, UnityAction callback)
  40. {
  41. yield return new WaitForSeconds(time);
  42. //声音播放完毕后之下往下的代码
  43. # region 声音播放完成后执行的代码
  44. print("声音播放完毕,继续向下执行");
  45. #endregion
  46. }
  47. }

补充:Unity 的 AudioSourse 播完的监听

最近涉及到 音频结束后的调用问题,unity 原生的音频组件 AudioSourse 没有功能,于是自己写了一个。

下面是代码:

  1. using Assets.Scripts.Entities;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. public class AudioSourceInfo
  7. {
  8. private float playTime = 0;
  9. public AudioSource AudioSource { get; private set; }
  10. public AudioState AudioState = AudioState.Idle;
  11. public Action AfterPlaying { get; set; }
  12. public int ID = 0;
  13. public AudioSourceInfo(GameObject go)
  14. {
  15. this.AudioSource = go.AddComponent<AudioSource>();
  16. }
  17. public AudioClip Clip
  18. {
  19. get
  20. {
  21. return this.AudioSource.clip;
  22. }
  23. set
  24. {
  25. this.AudioSource.clip = value;
  26. playTime = 0;
  27. }
  28. }
  29. public bool Loop
  30. {
  31. get
  32. {
  33. return this.AudioSource.loop ;
  34. }
  35. set
  36. {
  37. this.AudioSource.loop = value;
  38. }
  39. }
  40. public float Volume
  41. {
  42. get
  43. {
  44. return this.AudioSource.volume;
  45. }
  46. set
  47. {
  48. this.AudioSource.volume = value;
  49. }
  50. }
  51. public void Play()
  52. {
  53. if (null == this.AudioSource)
  54. {
  55. return;
  56. }
  57. this.AudioState = AudioState.IsPlaying;
  58. this.AudioSource.Play();
  59. }
  60. public void Pause()
  61. {
  62. if (null == this.AudioSource)
  63. {
  64. return;
  65. }
  66. if(this.AudioSource.isPlaying)
  67. {
  68. this.AudioState = AudioState.Pause;
  69. this.AudioSource.Pause();
  70. }
  71. }
  72. public void Stop()
  73. {
  74. if (null == this.AudioSource)
  75. {
  76. return;
  77. }
  78. this.AudioState = AudioState.Stop;
  79. this.AudioSource.Stop();
  80. if(AfterPlaying!= null)
  81. {
  82. this.AfterPlaying();
  83. }
  84. }
  85. private void Update()
  86. {
  87. if (this.AudioSource != null && this.AudioSource.clip!= null && this.AudioState == AudioState.IsPlaying)
  88. {
  89. playTime += Time.fixedDeltaTime;
  90. if (playTime >= this.Clip.length)
  91. {
  92. playTime = 0;
  93. this.Stop();
  94. }
  95. }
  96. }
  97. }
  98. public enum AudioState
  99. {
  100. Idle,
  101. IsPlaying,
  102. Pause,
  103. Stop,
  104. }

补充:Unity3d AudioSource如何监听播放完成并处理逻辑

想知道AudioSource什么时候播放完成并处理相关的逻辑,比如切换曲目,而unity又没有提供相应的事件

于是想到下面几种方案:

1、Update时时判断isPlaying

2、获取音频的播放长度,Invoke一下

后来查看api的时候突然想到,可以用协程啊。原理和Invoke一样,这应该是最好的方案了。

不过如果音频暂停掉了之后而又没有更新协程函数的话,问题就出现了。所以暂停的时候记得更新协程函数。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持w3xue。如有错误或未考虑完全的地方,望不吝赐教。

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号