本帖最后由 冰玥 于 2023-6-5 17:09 编辑
一.SoundService音效管理器介绍
SoundService是编辑器提供给开发者的一个管理音乐,音效的工具类。SoundService本身是一个单例,可在函数中通过SoundService.getInstance()调用类中方法播放或停止音乐,音效。
二. 一分钟上手指南
/**播放音效,参数:音效资源Id,播放次数,音量 */
this.playBtn.onClicked.add(() => {
SoundService.getInstance().playSound("12344", 1, 1);
})
/**播放BGM,参数:音效资源Id,音量 */
this.playBGMBtn.onClicked.add(() => {
SoundService.getInstance().playBGM("12721");
})
/**停止播放BGM */
this.stopBGMBtn.onClicked.add(() => {
SoundService.getInstance().stopBGM();
})
/**播放3D音效,只有在指定范围才能听见的音效 参数:资源Id,播放目标,播放次数,音量,3D播放半径*/
this.sound3DBtn.onClicked.add(() => {
SoundService.getInstance().play3DSound("12721", new Vector(100, 0, 0), 1, 1, 100);
})
看了上方的示例后,你可能会有疑惑,我从哪里能获取到音乐资源呢?我在这提供给各位开发者本地资源库中的一个分类,里面有许多音乐资源,任你挑选。
三.进阶用法
SoundService类还有中volumScale和BGMVolumScale两个分别控制音效和BGM音量API,我们可以将他们与UI的进度条组件绑定,制作设置音量的界面。
/**设置音效的音量*/
this.volumeProgressBar.sliderButtonReleaseDelegate.add(() => {
SoundService.getInstance().BGMVolumeScale = this.volumeProgressBar.percent;
})
/**设置BGM的音量*/
this.bgmProgressBar.sliderButtonReleaseDelegate.add(() => {
SoundService.getInstance().volumeScale = this.bgmProgressBar.percent;
})
|