unity AudioSource播放完聲音后要執(zhí)行的函數(shù)或條件操作
將腳本掛在要判斷聲音是否播放完畢的物體上
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鍵執(zhí)行函數(shù)播放語音
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();
//執(zhí)行協(xié)成獲取音頻文件的時間
StartCoroutine(AudioPlayFinished(_audio.clip.length, callback));
}
//執(zhí)行協(xié)成函數(shù) 并且返回時間
private IEnumerator AudioPlayFinished(float time, UnityAction callback)
{
yield return new WaitForSeconds(time);
//聲音播放完畢后之下往下的代碼
# region 聲音播放完成后執(zhí)行的代碼
print("聲音播放完畢,繼續(xù)向下執(zhí)行");
#endregion
}
}
補充:Unity 的 AudioSourse 播完的監(jiān)聽
最近涉及到 音頻結束后的調(diào)用問題,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如何監(jiān)聽播放完成并處理邏輯
想知道AudioSource什么時候播放完成并處理相關的邏輯,比如切換曲目,而unity又沒有提供相應的事件
于是想到下面幾種方案:
1、Update時時判斷isPlaying
2、獲取音頻的播放長度,Invoke一下
后來查看api的時候突然想到,可以用協(xié)程啊。原理和Invoke一樣,這應該是最好的方案了。

不過如果音頻暫停掉了之后而又沒有更新協(xié)程函數(shù)的話,問題就出現(xiàn)了。所以暫停的時候記得更新協(xié)程函數(shù)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
C#操作IIS程序池及站點的創(chuàng)建配置實現(xiàn)代碼
最近在做一個WEB程序的安裝包;對一些操作IIS進行一個簡單的總結;主要包括對IIS進行站點的新建以及新建站點的NET版本的選擇,還有針對IIS7程序池的托管模式以及版本的操作2013-03-03
C#泛型集合類System.Collections.Generic
這篇文章介紹了C#中的泛型集合類System.Collections.Generic,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

