Files
vampire-like/Assets/GameMain/Scripts/Runtime/BuiltinComponent/SoundExtension.cs
T

145 lines
5.4 KiB
C#

using GameFramework;
using SepCore.Definition;
using SepCore.Entity;
using GameFramework.Sound;
using UnityGameFramework.Runtime;
using SepCore.CustomUtility;
namespace SepCore.Sound
{
public static class SoundExtension
{
private const float FadeVolumeDuration = 1f;
private static int? s_MusicSerialId = null;
public static int? PlayMusic(this SoundComponent soundComponent, int musicId, object userData = null)
{
soundComponent.StopMusic();
MusicConfig musicConfig = GameEntry.Luban.Get<MusicConfig>(musicId);
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(musicConfig.AssetName), "Music", 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? PlaySound(this SoundComponent soundComponent, int soundId, EntityBase bindingEntity = null, object userData = null)
{
SoundConfig soundConfig = GameEntry.Luban.Get<SoundConfig>(soundId);
PlaySoundParams playSoundParams = PlaySoundParams.Create();
playSoundParams.Priority = soundConfig.Priority;
playSoundParams.Loop = soundConfig.Loop;
playSoundParams.VolumeInSoundGroup = soundConfig.Volume;
playSoundParams.SpatialBlend = soundConfig.SpatialBlend;
return soundComponent.PlaySound(AssetUtility.GetSoundAsset(soundConfig.AssetName), "Sound", Constant.AssetPriority.SoundAsset, playSoundParams, bindingEntity != null ? bindingEntity.Entity : null, userData);
}
public static int? PlayUISound(this SoundComponent soundComponent, int uiSoundId, object userData = null)
{
UISoundConfig uiSoundConfig = GameEntry.Luban.Get<UISoundConfig>(uiSoundId);
PlaySoundParams playSoundParams = PlaySoundParams.Create();
playSoundParams.Priority = uiSoundConfig.Priority;
playSoundParams.Loop = false;
playSoundParams.VolumeInSoundGroup = uiSoundConfig.Volume;
playSoundParams.SpatialBlend = 0f;
return soundComponent.PlaySound(AssetUtility.GetUISoundAsset(uiSoundConfig.AssetName), "UISound", Constant.AssetPriority.UISoundAsset, playSoundParams, userData);
}
public static bool IsMuted(this SoundComponent soundComponent, string soundGroupName)
{
if (string.IsNullOrEmpty(soundGroupName))
{
Log.Warning("Sound group is invalid.");
return true;
}
ISoundGroup soundGroup = soundComponent.GetSoundGroup(soundGroupName);
if (soundGroup == null)
{
Log.Warning("Sound group '{0}' is invalid.", soundGroupName);
return true;
}
return soundGroup.Mute;
}
public static void Mute(this SoundComponent soundComponent, string soundGroupName, bool mute)
{
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.Mute = mute;
GameEntry.Setting.SetBool(Utility.Text.Format(Constant.Setting.SoundGroupMuted, soundGroupName), mute);
GameEntry.Setting.Save();
}
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(Utility.Text.Format(Constant.Setting.SoundGroupVolume, soundGroupName), volume);
GameEntry.Setting.Save();
}
}
}