这篇文章主要介绍了Unity常用音频操作类,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
下面通过代码给大家介绍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();
}
}
/***将这个脚本挂载到一个空物体上,并且给它添加 AudioSource 组件*/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音频内容请搜索得得之家以前的文章希望大家以后多多支持得得之家!
沃梦达教程
本文标题为:Unity常用音频操作类示例代码
基础教程推荐
猜你喜欢
- ZooKeeper的安装及部署教程 2023-01-22
- 一个读写csv文件的C#类 2022-11-06
- C# 调用WebService的方法 2023-03-09
- C#控制台实现飞行棋小游戏 2023-04-22
- C#类和结构详解 2023-05-30
- C# List实现行转列的通用方案 2022-11-02
- unity实现动态排行榜 2023-04-27
- C# windows语音识别与朗读实例 2023-04-27
- linux – 如何在Debian Jessie中安装dotnet core sdk 2023-09-26
- winform把Office转成PDF文件 2023-06-14