biography-of-lijie/Assets/GameMain/Scripts/Sound/SoundExtension.cs

108 lines
3.8 KiB
C#

using CustomUtility;
using DataTable;
using Definition;
using Entity;
using GameFramework.DataTable;
using GameFramework.Sound;
using UnityGameFramework.Runtime;
namespace Sound
{
public static class SoundExtension
{
private const float FadeVolumeDuration = 1f;
private static int? s_MusicSerialId = null;
public static int? PlayBGM(this SoundComponent soundComponent, int musicId, object userData = null)
{
soundComponent.StopMusic();
IDataTable<DRBGM> dtMusic = GameEntry.DataTable.GetDataTable<DRBGM>();
DRBGM drBgm = dtMusic.GetDataRow(musicId);
if (drBgm == null)
{
Log.Warning("Can not load music '{0}' from data table.", musicId.ToString());
return null;
}
PlaySoundParams playSoundParams = PlaySoundParams.Create();
playSoundParams.Priority = 64;
playSoundParams.Loop = true;
playSoundParams.VolumeInSoundGroup = 1f;
playSoundParams.FadeInSeconds = FadeVolumeDuration;
playSoundParams.SpatialBlend = 0f;
s_MusicSerialId = soundComponent.PlaySound(AssetUtility.GetMusicAsset(drBgm.AssetName), "BGM",
Constant.AssetPriority.MusicAsset, playSoundParams, null, userData);
return s_MusicSerialId;
}
public static void StopMusic(this SoundComponent soundComponent)
{
if (!s_MusicSerialId.HasValue)
{
return;
}
soundComponent.StopSound(s_MusicSerialId.Value, FadeVolumeDuration);
s_MusicSerialId = null;
}
public static int? PlaySE(this SoundComponent soundComponent, int uiSoundId, object userData = null)
{
IDataTable<DRSE> dtUISound = GameEntry.DataTable.GetDataTable<DRSE>();
DRSE drUISound = dtUISound.GetDataRow(uiSoundId);
if (drUISound == null)
{
Log.Warning("Can not load UI sound '{0}' from data table.", uiSoundId.ToString());
return null;
}
PlaySoundParams playSoundParams = PlaySoundParams.Create();
playSoundParams.Priority = drUISound.Priority;
playSoundParams.Loop = false;
playSoundParams.VolumeInSoundGroup = drUISound.Volume;
playSoundParams.SpatialBlend = 0f;
return soundComponent.PlaySound(AssetUtility.GetUISoundAsset(drUISound.AssetName), "SE",
Constant.AssetPriority.UISoundAsset, playSoundParams, userData);
}
public static float GetVolume(this SoundComponent soundComponent, string soundGroupName)
{
if (string.IsNullOrEmpty(soundGroupName))
{
Log.Warning("Sound group is invalid.");
return 0f;
}
ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
if (soundGroup == null)
{
Log.Warning("Sound group '{0}' is invalid.", soundGroupName);
return 0f;
}
return soundGroup.Volume;
}
public static void SetVolume(this SoundComponent soundComponent, string soundGroupName, float volume)
{
if (string.IsNullOrEmpty(soundGroupName))
{
Log.Warning("Sound group is invalid.");
return;
}
ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
if (soundGroup == null)
{
Log.Warning("Sound group '{0}' is invalid.", soundGroupName);
return;
}
soundGroup.Volume = volume;
GameEntry.Setting.SetFloat($"Setting.{soundGroup}Volume", volume);
GameEntry.Setting.Save();
}
}
}