Unity常用音频操作类示例代码
短信预约 -IT技能 免费直播动态提醒
下面通过代码给大家介绍Unity常用音频操作类,具体代码如下所示:
using UnityEngine;
using System.Collections;
public class AudioPlay : MonoBehaviour {
public static AudioPlay Instance;
public AudioClip[] FuChuAudio;
public AudioSource FCAudio;
// public AudioSource BabyAudio;
// Use this for initialization
void Awake()
{
Instance = this;
}
void Start () {
}
// Update is called once per frame
void Update () {
}
/// <summary>
/// 播放某个音频
/// </summary>
/// <param name="i"></param>
public void playAudio(int i)
{
FCAudio.clip = FuChuAudio[i];
FCAudio.Play();
}
/// <summary>
/// 停止播放所有的音频
/// </summary>
public void StopplayAll()
{
for (int i= 0; i< FuChuAudio.Length; i++)
{
FCAudio.clip = FuChuAudio[i];
FCAudio.Stop();
}
}
/// <summary>
/// 停止播放某个音频
/// </summary>
/// <param name="i"></param>
public void StopplayOne(int i)
{
FCAudio.clip = FuChuAudio[i];
FCAudio.Stop();
}
/// <summary>
/// 循环播放某个音频
/// </summary>
/// <param name="i"></param>
public void playLoop(int i)
{
FCAudio.clip = FuChuAudio[i];
FCAudio.loop = true;//设置声音为循环播放 ;
FCAudio.Play();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 控制所有音乐的管理类
/// </summary>
public class AudioManager : MonoBehaviour
{
//将要轮流播放的音乐组
public AudioClip[] audioGroup;
//当前播放的是谁
private int playingIndex;
//是否允许播放音乐
private bool canPlayAudio;
//AudioSource组件
private AudioSource audioSource;
//-----------------------------------------------------
void Start()
{
audioSource = this.GetComponent<AudioSource>();
canPlayAudio = true;
playingIndex = 0;
}
//-----------------------------------------------------
void Update()
{
if (canPlayAudio)
{
PlayAudio();
canPlayAudio = false;
}
if (!audioSource.isPlaying)
{
playingIndex++;
if (playingIndex >= audioGroup.Length)
{
playingIndex = 0;
}
canPlayAudio = true;
}
}
//-----------------------------------------------------
private void PlayAudio()
{
audioSource.clip = audioGroup[playingIndex];
audioSource.Play();
}
//-----------------------------------------------------
}
到此这篇关于Unity常用音频操作类示例代码的文章就介绍到这了,更多相关Unity音频内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341