特别提示: 本系列基于Unity 2019.4.8,框架版本GameFramework 2021.05.31
本系列博客地址: 传送门
Sound组件提供管理声音和声音组的功能,用户可以自定义一个声音的音量、是 2D 声音还是 3D 声音,甚至是直接绑定到某个实体上跟随实体移动。
一、常见用法
获取声音组件
SoundComponent soundComponent = GameEntry.GetComponent<SoundComponent>();
获取指定声音组
public ISoundGroup GetSoundGroup(string soundGroupName)
增加声音组
public bool AddSoundGroup(string soundGroupName, int soundAgentHelperCount)
播放声音
public int PlaySound(string soundAssetName, string soundGroupName)
public int PlaySound(string soundAssetName, string soundGroupName, int priority)
public int PlaySound(string soundAssetName, string soundGroupName, PlaySoundParams playSoundParams)
public int PlaySound(string soundAssetName, string soundGroupName, Entity bindingEntity)
public int PlaySound(string soundAssetName, string soundGroupName, Vector3 worldPosition)
public int PlaySound(string soundAssetName, string soundGroupName, object userData)
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams)
public int PlaySound(string soundAssetName, string soundGroupName, int priority, PlaySoundParams playSoundParams, object userData)
停止播放声音
public bool StopSound(int serialId)
public bool StopSound(int serialId, float fadeOutSeconds)
暂停播放声音
public void PauseSound(int serialId)
public void PauseSound(int serialId, float fadeOutSeconds)
暂恢复播放声音
public void ResumeSound(int serialId)
public void ResumeSound(int serialId, float fadeInSeconds)
二、配置
1、需要配置SoundGroup分组,其中AgentHelperCount是声音代理辅助器数量,可以理解为生成AudioSource的个数,最少一个不然没声音
2、指定一个声音混合器,混合器中的分组要跟SoundGroup的对应
三、实践
先看一下demo实现效果
- 写了一个SoundComponent拓展,用来设置音量大小,声音开关,播放不同类型声音
using GameFramework;
using GameFramework.Sound;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityGameFramework.Runtime;
public static class Sound
{
public static void PlayMusic(this SoundComponent soundComponent, string assetsName)
{
PlaySoundParams playSoundParams = PlaySoundParams.Create();
playSoundParams.MuteInSoundGroup = false;
playSoundParams.Priority = 64;
playSoundParams.Loop = true;
playSoundParams.VolumeInSoundGroup = 1f;
playSoundParams.SpatialBlend = 0f;
Component.Sound.PlaySound($"Assets/Audio/{assetsName}.mp3", "Music", playSoundParams);
}
public static void PlaySound(this SoundComponent soundComponent, string assetsName)
{
PlaySoundParams playSoundParams = PlaySoundParams.Create();
playSoundParams.Priority = 30;
playSoundParams.Loop = false;
playSoundParams.VolumeInSoundGroup = 1f;
playSoundParams.SpatialBlend = 0f;
Component.Sound.PlaySound($"Assets/Audio/{assetsName}.wav", "SoundEffect", playSoundParams);
}
public static void PlayUISound(this SoundComponent soundComponent, string assetsName)
{
PlaySoundParams playSoundParams = PlaySoundParams.Create();
playSoundParams.Priority = 30;
playSoundParams.Loop = false;
playSoundParams.VolumeInSoundGroup = 1f;
playSoundParams.SpatialBlend = 0f;
Component.Sound.PlaySound($"Assets/Audio/{assetsName}.wav", "UISoundEffect", playSoundParams);
}
public static void SetVolume(this SoundComponent soundComponent, string soundGroupName, float volume)
{
if (string.IsNullOrEmpty(soundGroupName))
{
Log.Error("Sound group is invalid.");
return;
}
ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
if (soundGroup == null)
{
Log.Error("Sound group '{0}' is invalid.", soundGroupName);
return;
}
soundGroup.Volume = volume;
// Component.Setting.SetFloat(string.Format("Setting.{0}Volume", soundGroupName), volume);
}
public static void Mute(this SoundComponent soundComponent, string soundGroupName, bool mute)
{
if (string.IsNullOrEmpty(soundGroupName))
{
Log.Error("Sound group is invalid.");
return;
}
ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
if (soundGroup == null)
{
Log.Error("Sound group '{0}' is invalid.", soundGroupName);
return;
}
soundGroup.Mute = mute;
// Component.Setting.SetBool(string.Format("Setting.{0}Muted", soundGroupName), mute);
}
public static bool IsMuted(this SoundComponent soundComponent, string soundGroupName)
{
if (string.IsNullOrEmpty(soundGroupName))
{
Log.Error("Sound group is invalid.");
return true;
}
ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
if (soundGroup == null)
{
Log.Error("Sound group '{0}' is invalid.", soundGroupName);
return true;
}
return soundGroup.Mute;
}
public static float GetVolume(this SoundComponent soundComponent, string soundGroupName)
{
if (string.IsNullOrEmpty(soundGroupName))
{
Log.Error("Sound group is invalid.");
return 0f;
}
ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
if (soundGroup == null)
{
Log.Error("Sound group '{0}' is invalid.", soundGroupName);
return 0f;
}
return soundGroup.Volume;
}
}
- 完善UIForm_Setting脚本的Toggle,Slider,Btn的绑定事件,完整代码可以去demo里看
- 在ProcedureExample流程初始化声音设置
Demo地址: 传送门