一、目的
1、想知道:Unity中音乐操作
二、参考
1、
三、操作
1、MyAudio.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Audio;
/// <summary>
/// 目的:背景音乐、按钮音乐的操作
/// </summary>
public class MyAudio : MonoBehaviour
{
/// <summary>AudioSource:背景音乐</summary>
[Tooltip("AudioSource:背景音乐")]
[Header("AudioSource:背景音乐")]
public AudioSource as_bgm;
/// <summary>AudioSource:鼠标点击音乐</summary>
[Tooltip("AudioSource:鼠标点击音乐")]
[Header("AudioSource:鼠标点击音乐")]
public AudioSource as_mouseClick;
/// <summary>AudioClip:背景音乐</summary>
[Tooltip("AudioClip:背景音乐")]
[Header("AudioClip:背景音乐")]
public AudioClip ac_bgm;
/// <summary>AudioClip:鼠标点击音乐</summary>
[Tooltip("AudioClip:鼠标点击音乐")]
[Header("AudioClip:鼠标点击音乐")]
public AudioClip ac_mouseClick;
/// <summary>
/// 目的:MyAudio 单例
/// </summary>
[HideInInspector]
public static MyAudio instance_myAudio;
void Awake()
{
instance_myAudio = this;
}
// Use this for initialization
void Start()
{
SetAudio(EM_SetAudio_audioKinds.BGM, EM_SetAudio_action.CREATE);
SetAudio(EM_SetAudio_audioKinds.MOUSECLICK, EM_SetAudio_action.CREATE);
}
// Update is called once per frame
void Update()
{
}
/// <summary>
/// 目的:SetBGM 需要的枚举:audio操作
/// </summary>
public enum EM_SetAudio_action
{
/// <summary>空</summary>
NULL,
/// <summary>创建</summary>
CREATE,
/// <summary>播放</summary>
PLAY,
/// <summary>暂停</summary>
PAUSE,
/// <summary>清楚</summary>
CLEAR,
/// <summary>重置</summary>
RESET
}
/// <summary>
/// 目的:SetBGM 需要的枚举:audio种类
/// </summary>
public enum EM_SetAudio_audioKinds
{
/// <summary>空</summary>
NULL,
/// <summary>背景音乐</summary>
BGM,
/// <summary>鼠标点击音乐</summary>
MOUSECLICK
}
/// <summary>
/// 目的:设置 背景音乐
/// </summary>
public void SetAudio(EM_SetAudio_audioKinds _emSetAudio_audioKinds, EM_SetAudio_action _emSetAudio)
{
switch (_emSetAudio)
{
case EM_SetAudio_action.NULL:
break;
case EM_SetAudio_action.CREATE:
switch (_emSetAudio_audioKinds)
{
case EM_SetAudio_audioKinds.BGM:
as_bgm.clip = ac_bgm;
as_bgm.loop = true;
break;
case EM_SetAudio_audioKinds.MOUSECLICK:
as_mouseClick.clip = ac_mouseClick;
as_bgm.loop = false;
break;
}
break;
case EM_SetAudio_action.PLAY:
switch (_emSetAudio_audioKinds)
{
case EM_SetAudio_audioKinds.BGM:
if (as_bgm.clip!=null&&as_bgm.isPlaying==false)
{
as_bgm.Play();
}
break;
case EM_SetAudio_audioKinds.MOUSECLICK:
if (as_mouseClick.clip != null && as_mouseClick.isPlaying == false)
{
as_mouseClick.Play();
}
break;
}
break;
case EM_SetAudio_action.PAUSE:
switch (_emSetAudio_audioKinds)
{
case EM_SetAudio_audioKinds.BGM:
if (as_bgm.clip != null && as_bgm.isPlaying == true)
{
as_bgm.Pause();
}
break;
case EM_SetAudio_audioKinds.MOUSECLICK:
if (as_mouseClick.clip != null && as_mouseClick.isPlaying == true)
{
as_mouseClick.Pause();
}
break;
}
break;
case EM_SetAudio_action.CLEAR:
break;
case EM_SetAudio_action.RESET:
switch (_emSetAudio_audioKinds)
{
case EM_SetAudio_audioKinds.BGM:
if (as_bgm.clip != null && as_bgm.isPlaying == true)
{
as_bgm.PlayScheduled(0);
}
break;
case EM_SetAudio_audioKinds.MOUSECLICK:
if (as_mouseClick.clip != null && as_mouseClick.isPlaying == true)
{
as_bgm.PlayScheduled(0);
}
break;
}
break;
}
}
}
1、Unity设置
导入俩个audioclip
场景中新建俩个audio,名字分别是Audio Source-bgm Audio Source-mouseClick
将代码拖到名字、标签叫做 ScriptsHold 空物体上
1、别的地方调用这个音乐
GameObject.FindWithTag("ScriptsHold").GetComponent<MyAudio>().SetAudio(MyAudio.EM_SetAudio_audioKinds.MOUSECLICK, MyAudio.EM_SetAudio_action.PLAY);
GameObject.FindWithTag("ScriptsHold").GetComponent<MyAudio>().SetAudio(MyAudio.EM_SetAudio_audioKinds.BGM, MyAudio.EM_SetAudio_action.PLAY);