Unity中游戲的聲音管理
阿新 • • 發佈:2019-02-05
從來沒寫過部落格,作為一名菜鳥也不知道寫些什麼。特地偷同事點料/
using UnityEngine; using System.Collections; using System.Collections.Generic; /// <summary> /// 用來管理音訊的各種播放方式 /// --------------修改----------------- /// 修改播放音量會根據gamedata裡面的設定進行變化 /// </summary> public class AudioManager : Singleton<AudioManager> { /// <summary> /// 在指定位置播放剪輯。播放完成後自動消除音訊源。 /// </summary> /// <param name="clip">音訊剪輯</param> /// <param name="position">播放音效的3D空間位置</param> /// <param name="volume">音量大小</param> public void PlayAudioClip(AudioClip clip, Vector3 position, float volume) { AudioSource.PlayClipAtPoint(clip, position, volume); } /// <summary> /// 在指定位置播放剪輯 /// </summary> /// <param name="clip"></param> /// <param name="emitter"></param> /// <returns></returns> public AudioSource PlayAudioClip(AudioClip clip) { //Debug.Log(this.name + "音量大小為" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100); return PlayAudioClip(clip, Camera.mainCamera.transform, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, 1f, false); } public AudioSource PlayAudioClip(AudioClip clip, bool isLoop) { //Debug.Log(this.name + "音量大小為" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100); return PlayAudioClip(clip, Camera.mainCamera.transform, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, 1f, isLoop); } public AudioSource PlayAudioClip(AudioClip clip, Transform emitter) { //Debug.Log(this.name + "音量大小為" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100); return PlayAudioClip(clip, emitter, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, 1f, false); } public AudioSource PlayAudioClip(AudioClip clip, Transform emitter, float volume) { //Debug.Log(this.name + "音量大小為" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100); return PlayAudioClip(clip, emitter, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, 1f, false); } public AudioSource PlayAudioClip(AudioClip clip, Transform emitter, float volume, float pitch) { //Debug.Log(this.name + "音量大小為" + 1f * GameData.instance.GetGameSettingData().gameVolum / 100); return PlayAudioClip(clip, emitter, 1f * GameData.instance.GetGameSettingData().gameVolum / 100, pitch, false); } /// <summary> /// 建立一個空物體並且附加一個AudioSource元件來播放音效, /// 並且需要給它傳遞一個transform給他作為播放的位置, /// 播放完畢後自動刪除 /// </summary> /// <param name="clip">音訊剪輯引用</param> /// <param name="emitter"></param> /// <param name="volume"></param> /// <param name="pitch"></param> /// <param name="isLoop">是否迴圈</param> /// <returns>音訊源</returns> public AudioSource PlayAudioClip(AudioClip clip, Transform emitter, float volume, float pitch, bool isLoop) { //Create an empty game object GameObject go = new GameObject("Audio: " + clip.name); go.transform.position = emitter.position; go.transform.parent = emitter; //Create the source AudioSource source = go.AddComponent<AudioSource>(); source.clip = clip; source.volume = 1f * GameData.instance.GetGameSettingData().waitVolum / 100; source.pitch = pitch; source.loop = isLoop; source.Play(); if (!isLoop) Destroy(go, clip.length); return source; } /// <summary> /// 停止播放當前剪輯,並且刪除gameObject /// </summary> /// <param name="audioSource"></param> public void StopAudioClip(AudioSource audioSource) { if (audioSource == null) return; audioSource.Stop(); audioSource.clip = null; //Debug.Log("刪除" + audioSource.gameObject.name); Destroy(audioSource.gameObject); } /// <summary> /// 指定位置播放剪輯。播放完成後自動消除音訊源。 /// 進一步控制播放 /// </summary> /// <param name="clip">音訊剪輯</param> /// <param name="position">播放音效的3D空間位置</param> /// <param name="volume">音量大小</param> /// <returns></returns> public AudioSource PlayAudioClip(AudioClip clip, Vector3 position, float volume, float pitch, bool isLoop) { //Create an empty game object GameObject go = new GameObject("Audio: " + clip.name); go.transform.position = position; AudioSource source = go.AddComponent<AudioSource>(); source.clip = clip; source.volume = volume; source.pitch = pitch; source.Play(); source.loop = isLoop; if (!isLoop) Destroy(go, clip.length); return source; }
}