unity AudioSource播放完声音后要执行的函数或条件操作
作者:是阿根 时间:2021-07-04 21:46:37
将脚本挂在要判断声音是否播放完毕的物体上
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一样,这应该是最好的方案了。
不过如果音频暂停掉了之后而又没有更新协程函数的话,问题就出现了。所以暂停的时候记得更新协程函数。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/my_bzh/article/details/91837208
标签:unity,AudioSource,播放声音,执行函数
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
实例详解Android自定义ProgressDialog进度条对话框的实现
2023-02-18 08:32:40
![](https://img.aspxhome.com/file/2023/9/94999_0s.jpg)
详解Java中方法重写和方法重载的6个区别
2023-11-28 13:42:18
![](https://img.aspxhome.com/file/2023/2/60072_0s.jpg)
Flutter瀑布流仿写原生的复用机制详解
2023-06-20 17:02:08
![](https://img.aspxhome.com/file/2023/0/98700_0s.jpg)
Java的外部类为什么不能使用private和protected进行修饰的讲解
2023-06-29 00:45:57
Java实现堆排序和图解
2023-11-11 12:13:37
![](https://img.aspxhome.com/file/2023/6/59336_0s.png)
高效C#编码优化原则
2023-06-13 03:16:23
java生成图片验证码示例程序
2023-09-13 10:17:57
C# 日历类功能的实例代码
2023-06-23 21:44:45
![](https://img.aspxhome.com/file/2023/3/85723_0s.png)
Java栈和基础队列的实现详解
2023-07-02 05:36:59
![](https://img.aspxhome.com/file/2023/7/61597_0s.gif)
Java 访问剪切板(复制,粘贴)的示例
2023-11-10 12:26:13
Java使用BigDecimal精确运算浮点数
2023-08-11 09:47:11
Java中List.contains(Object object)方法使用
2022-04-25 20:47:11
SpringBoot读取资源目录中JSON文件的方法实例
2023-04-26 02:00:42
![](https://img.aspxhome.com/file/2023/6/69136_0s.jpg)
JavaSE的三大接口:Comparator,Comparable和Cloneable详解
2021-09-01 03:14:20
![](https://img.aspxhome.com/file/2023/2/81242_0s.png)
java 如何读取properties文件
2023-08-28 05:23:01
![](https://img.aspxhome.com/file/2023/7/75417_0s.png)
Android实现Banner界面广告图片循环轮播(包括实现手动滑动循环)
2022-02-06 17:05:35
![](https://img.aspxhome.com/file/2023/1/93261_0s.gif)
SpringBoot打War包上传到阿里云的LINUX服务器的操作方法
2021-09-26 22:27:01
![](https://img.aspxhome.com/file/2023/7/84197_0s.jpg)
java使用Feign实现声明式Restful风格调用
2023-01-22 08:18:05
Java数据结构之线段树的原理与实现
2021-12-17 13:30:52
![](https://img.aspxhome.com/file/2023/8/72608_0s.png)
java使用jacob实现word转pdf
2023-05-12 11:45:33