unity AudioSource播放完声音后要执行的函数或条件操作
程序员文章站
2022-03-23 09:57:24
将脚本挂在要判断声音是否播放完毕的物体上using system.collections;using unityengine;using unityengine.events;[requirecomp...
将脚本挂在要判断声音是否播放完毕的物体上
using system.collections; using unityengine; using unityengine.events; [requirecomponent(typeof(audiosource))] public class audiomanager : monobehaviour { public static audiomanager instence = null; private audiosource _audio; void awake() { if (instence == null) { instence = this; } } void start() { _audio = getcomponent<audiosource>(); } void update() { //按下键盘按钮a键执行函数播放语音 if (input.getkeydown(keycode.a)) { playaudio(gameobject.getcomponent().clip) } } //接受音频文件和是否重复播放 public void playaudio(audioclip clip, unityaction callback = null, bool isloop = false) { //获取自身音频文件进行播放并且不重复播放 _audio.clip = clip; _audio.loop = isloop; _audio.play(); //执行协成获取音频文件的时间 startcoroutine(audioplayfinished(_audio.clip.length, callback)); } //执行协成函数 并且返回时间 private ienumerator audioplayfinished(float time, unityaction callback) { yield return new waitforseconds(time); //声音播放完毕后之下往下的代码 # region 声音播放完成后执行的代码 print("声音播放完毕,继续向下执行"); #endregion } }
补充:unity 的 audiosourse 播完的监听
最近涉及到 音频结束后的调用问题,unity 原生的音频组件 audiosourse 没有功能,于是自己写了一个。
下面是代码:
using assets.scripts.entities; using system; using system.collections; using system.collections.generic; using unityengine; public class audiosourceinfo { private float playtime = 0; public audiosource audiosource { get; private set; } public audiostate audiostate = audiostate.idle; public action afterplaying { get; set; } public int id = 0; public audiosourceinfo(gameobject go) { this.audiosource = go.addcomponent<audiosource>(); } public audioclip clip { get { return this.audiosource.clip; } set { this.audiosource.clip = value; playtime = 0; } } public bool loop { get { return this.audiosource.loop ; } set { this.audiosource.loop = value; } } public float volume { get { return this.audiosource.volume; } set { this.audiosource.volume = value; } } public void play() { if (null == this.audiosource) { return; } this.audiostate = audiostate.isplaying; this.audiosource.play(); } public void pause() { if (null == this.audiosource) { return; } if(this.audiosource.isplaying) { this.audiostate = audiostate.pause; this.audiosource.pause(); } } public void stop() { if (null == this.audiosource) { return; } this.audiostate = audiostate.stop; this.audiosource.stop(); if(afterplaying!= null) { this.afterplaying(); } } private void update() { if (this.audiosource != null && this.audiosource.clip!= null && this.audiostate == audiostate.isplaying) { playtime += time.fixeddeltatime; if (playtime >= this.clip.length) { playtime = 0; this.stop(); } } } } public enum audiostate { idle, isplaying, pause, stop, }
补充:unity3d audiosource如何监听播放完成并处理逻辑
想知道audiosource什么时候播放完成并处理相关的逻辑,比如切换曲目,而unity又没有提供相应的事件
于是想到下面几种方案:
1、update时时判断isplaying
2、获取音频的播放长度,invoke一下
后来查看api的时候突然想到,可以用协程啊。原理和invoke一样,这应该是最好的方案了。
不过如果音频暂停掉了之后而又没有更新协程函数的话,问题就出现了。所以暂停的时候记得更新协程函数。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
上一篇: 我买的是鸭绒被
下一篇: 误把汽车点烟器当成口红了