using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundPlay : MonoBehaviour {
//将准备好的音频拖入此处
public AudioClip audioClip;
//用于控制声音的AudioSource组件
private AudioSource _audioSource;
void Awake ()
{
//再添加此脚本的对象中添加AudioSource组件
_audioSource = this.gameObject.AddComponent();
//设置循环播放
_audioSource.loop = true;
//设置音量为最大 区间在0-1之间
_audioSource.volume = 1.0f;
//设置audioClip
_audioSource.clip = audioClip;
}
void OnGUI ()
{
//绘制播放按钮并设置其点击后的处理
if(GUI.Button (new Rect(10, 10, 80, 30), “Play”))
{
//播放声音
_audioSource.Play();
}
//绘制暂停按钮并设置其点击后的处理
if (GUI.Button(new Rect(10, 50, 80, 30), “Pause”))
{
//暂停声音 暂停后再播放 则声音为继续播放
_audioSource.Pause();
}
//绘制停止按钮并设置其点击后的处理
if (GUI.Button(new Rect(10, 90, 80, 30), “Stop”))
{
//停止播放 停止后在播放 则声音为从头播放
_audioSource.Stop();
}
}
}