C# 在unity中播放多个音频片段
可能会有类似的问题,但我找不到任何有用的 我有一个简单的scrips,它有一个类列表,其中包含音频片段、id和事件系统,分别对应于这些元素中的每一个 我需要使音频剪辑字段列表,并使他们按顺序播放,直到最后一个,然后停止和火灾事件 以下是我仅用于一个音频剪辑插槽的代码: (我也尝试了音频剪辑阵列,但它只是播放音频剪辑阵列上的第一个) 公共类MainAudioManager:MonoBehavior{ public List currentsoundinfoList=新列表(); 声源;声源; soundInfo当前soundInfo; 浮动长度; 无效开始(){ audioSource=gameObject.GetComponent(); } 公共无效SetAudioToPlay(内部ID){ 对于(int i=0;iC# 在unity中播放多个音频片段,c#,unity3d,audio,arraylist,audioclip,C#,Unity3d,Audio,Arraylist,Audioclip,可能会有类似的问题,但我找不到任何有用的 我有一个简单的scrips,它有一个类列表,其中包含音频片段、id和事件系统,分别对应于这些元素中的每一个 我需要使音频剪辑字段列表,并使他们按顺序播放,直到最后一个,然后停止和火灾事件 以下是我仅用于一个音频剪辑插槽的代码: (我也尝试了音频剪辑阵列,但它只是播放音频剪辑阵列上的第一个) 公共类MainAudioManager:MonoBehavior{ public List currentsoundinfoList=新列表(); 声源;声源; so
}好吧,我自己找到了解决办法。下面是创建声音列表的最终结果,其中每个声音列表上都单独附加了事件,并且可以通过另一个事件和类的ID播放,供需要的任何人使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(AudioSource))]
public class MainAudioManager : MonoBehaviour {
public List<soundInfo> soundList = new List<soundInfo>();
AudioSource audioSource;
soundInfo curentSoundInfo;
float audioLenght;
void Start () {
audioSource = gameObject.GetComponent<AudioSource> ();
}
public void SetAudioToPlay (int ID) {
for (int i = 0; i < soundList.Count; i++) {
if (soundList [i].ID == ID) {
curentSoundInfo = soundList [i];
StartCoroutine(playSequencely());
return;
}
}
}
IEnumerator playSequencely () {
yield return null;
for (int cnt = 0; cnt < curentSoundInfo.clipsToPlay.Length; cnt++) {
audioSource.clip = curentSoundInfo.clipsToPlay [cnt];
audioSource.Play ();
while (audioSource.isPlaying) {
yield return null;
}
}
//Debug.Log ("Last Audio Is Playing");
curentSoundInfo.onStop.Invoke ();
}
}
[System.Serializable]
public class soundInfo {
public string name;
public int ID;
[TextArea (2, 8)] public string About;
public AudioClip[] clipsToPlay;
//public float delayBetween;
public UnityEvent onStop;
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.Events;
[所需组件(类型(音频源))]
公共类主音频管理器:MonoBehavior{
公共列表声音列表=新列表();
声源;声源;
soundInfo当前soundInfo;
浮动长度;
无效开始(){
audioSource=gameObject.GetComponent();
}
公共无效SetAudioToPlay(内部ID){
for(int i=0;i
好的,我自己找到了解决方案。下面是创建声音列表的最终结果,其中每个声音列表上都单独附加了事件,并且可以通过另一个事件和类的ID播放,供需要的任何人使用
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
[RequireComponent(typeof(AudioSource))]
public class MainAudioManager : MonoBehaviour {
public List<soundInfo> soundList = new List<soundInfo>();
AudioSource audioSource;
soundInfo curentSoundInfo;
float audioLenght;
void Start () {
audioSource = gameObject.GetComponent<AudioSource> ();
}
public void SetAudioToPlay (int ID) {
for (int i = 0; i < soundList.Count; i++) {
if (soundList [i].ID == ID) {
curentSoundInfo = soundList [i];
StartCoroutine(playSequencely());
return;
}
}
}
IEnumerator playSequencely () {
yield return null;
for (int cnt = 0; cnt < curentSoundInfo.clipsToPlay.Length; cnt++) {
audioSource.clip = curentSoundInfo.clipsToPlay [cnt];
audioSource.Play ();
while (audioSource.isPlaying) {
yield return null;
}
}
//Debug.Log ("Last Audio Is Playing");
curentSoundInfo.onStop.Invoke ();
}
}
[System.Serializable]
public class soundInfo {
public string name;
public int ID;
[TextArea (2, 8)] public string About;
public AudioClip[] clipsToPlay;
//public float delayBetween;
public UnityEvent onStop;
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.Events;
[所需组件(类型(音频源))]
公共类主音频管理器:MonoBehavior{
公共列表声音列表=新列表();
声源;声源;
soundInfo当前soundInfo;
浮动长度;
无效开始(){
audioSource=gameObject.GetComponent();
}
公共无效SetAudioToPlay(内部ID){
for(int i=0;i
偶数连接到什么?您指的是事件吗?这就是unity的事件系统,它将在特定时间的运行时触发事件,在我的例子中,它将在每个listofclips end迭代(强制播放的音频列表)上触发。偶数与什么有关?你是指事件吗?这就是unity的事件系统,它将在特定时间的运行时触发事件,在我的例子中,它将在每个listofclips end迭代中触发(强制播放的音频列表)