本帖最后由 俊记牛腩面 于 2023-12-4 16:08 编辑
这个是我自己写的一个音量管理器,将音效分为普通音效和3d音效以及bgm,它们的音量全部通过Volume这个字段全局控制了,如下图所示,编辑器自带的接口里面volumeScale可以控制音效音量的大小,BGMVolumeScale可以控制bgm的大小。
/**
* 音效管理器,控制所有音效bgm的播放与停止
*/
export class SoundManager {
private static _instance: SoundManager = null
/**外部调用单例 */
public static get Instance() {
if (!this._instance) {
this._instance = new SoundManager()
}
return this._instance
}
/**2d音效 */
public static soundList: string[] = []
/**3d音效 */
public static sound3DList: number[] = []
/**背景音乐 */
public static curBgm: string = ''
/**全局音量 */
public static get Volume() {
return Number(SoundService.volumeScale.toFixed(1))
}
public static set Volume(val: number) {
if (val < 0)
val = 0
val = Number(val.toFixed(1))
SoundService.BGMVolumeScale = val
SoundService.volumeScale = val
}
/**
* 播放音效接口
* @param assetId 资源id
* @param target 目标
* @param loopCount 播放次数
* @param volume 音量 (0-1)
* @param params 其他参数 (3d音效的范围,音量衰减半径)
*/
playSound(assetId: string, target: string | mw.GameObject | mw.Vector, loopCount?: number, volume?: number, params?: {
radius?: number;
falloffDistance?: number;
}) {
if (StringUtil.isEmpty(assetId))
return
SoundManager.Volume = volume
if (target && params && params.radius > 0 && params.falloffDistance > 0) {
const pid = SoundService.play3DSound(assetId, target, loopCount, volume, params)
SoundManager.sound3DList.push(pid)
} else {
if (SoundManager.soundList.indexOf(assetId) != -1) {
return
}
const pid = SoundService.playSound(assetId, loopCount, volume)
SoundManager.soundList.push(pid)
}
}
/**
*播放bgm
* @param assetId 资源id
* @param volume 音量 (0-1)
*/
playBGM(assetId: string, volume?: number) {
if (StringUtil.isEmpty(assetId) || assetId == SoundManager.curBgm)
return
SoundManager.curBgm = assetId
SoundManager.Volume = volume
SoundService.playBGM(assetId, volume)
}
/**
* 停止所有声音,音效
*/
clearAllSound() {
SoundService.stopAll3DSound()
SoundService.stopAllSound()
this.stopBgm()
SoundManager.curBgm = ''
SoundManager.soundList.length = 0
SoundManager.sound3DList.length = 0
}
/**
* 停止bgm
*/
stopBgm() {
SoundService.stopBGM()
}
/**
* 停止单个声音
* @param soundID 音效唯一标识 or 音效资源id
* @param is3D 是否是3d音效
*/
stopSound(soundID: number | string, is3D: boolean) {
if (is3D) {
SoundService.stop3DSound(soundID as number)
} else {
SoundService.stopSound(soundID as string)
}
}
}
/**
* 测试脚本
*/
@Component
export default class SoundTest extends mw.Script {
onStart() {
if (SystemUtil.isServer()) return
let vol = 1
let index = -1
let bgmIndex = -1
InputUtil.onKeyDown(Keys.Q, () => {
if (index < vec.length - 1) {
index += 1
} else {
index = 0
}
SoundManager.Instance.playSound(vec[index], null, 0, vol)
this.print()
})
InputUtil.onKeyDown(Keys.R, () => {
if (bgmIndex < vec2.length - 1) {
bgmIndex += 1
} else {
bgmIndex = 0
}
SoundManager.Instance.playBGM(vec2[bgmIndex], vol)
this.print()
})
InputUtil.onKeyDown(Keys.W, () => {
vol += 0.1
SoundManager.Volume = vol
this.print()
})
InputUtil.onKeyDown(Keys.E, () => {
vol -= 0.1
SoundManager.Volume = vol
this.print()
})
}
print() {
console.log("当前音量:" + SoundManager.Volume)
console.log("当前音效列表:" + SoundManager.soundList)
console.log("当前bgm:" + SoundManager.curBgm)
}
}
const vec = ["12512", "12520", "12585"]
const vec2 = ["135657", "133051", "132949"]
实际效果:
Your browser does not support video tags.