来自于:伊凡晴天
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ChangeBGM : MonoBehaviour
{
/* 音频组件 */
private AudioSource audioSource;
/* 被判断物体对象,需手动拖入对象 */
public GameObject BGStorePanel;
/* 音频数组,需手动添加数组长度及音乐*/
public AudioClip[] BgmList;
void Start()
{
/* 开始获取音频组件,并播放一个音乐 */
audioSource = this.GetComponent<AudioSource>();
audioSource.clip = BgmList[0];
audioSource.Play();
}
void Update()
{
/* 每帧判断是否正在播放 */
if (audioSource.isPlaying) {
/* 判断游戏物体对象的状态如果是true显示的 */
if (BGStorePanel.activeInHierarchy == true) {
/* 将正在播放的音乐暂停 */
audioSource.Pause();
/* 切换音乐 */
audioSource.clip = BgmList[1];
/* 可选参数循环播放 */
audioSource.loop = false;
/* 播放音乐 */
audioSource.Play();
}
}
}
}