using CustomUtility; using DataTable; using Definition; using GameFramework.DataTable; using GameFramework.Sound; using UnityEngine; using UnityGameFramework.Runtime; namespace Sound { public static class SoundExtension { private const float FadeVolumeDuration = 1f; private static int? s_MusicSerialId = null; private static int? s_CurrentBgmId = null; private static float s_CurrentBgmStartOffsetSeconds = 0f; private static float s_CurrentBgmStartRealtime = 0f; public static int? PlayBGM(this SoundComponent soundComponent, int musicId, object userData = null) { return PlayBGM(soundComponent, musicId, 0f, userData); } public static int? PlayBGM(this SoundComponent soundComponent, int musicId, float startTimeSeconds, object userData = null) { soundComponent.StopMusic(); IDataTable dtMusic = GameEntry.DataTable.GetDataTable(); 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; playSoundParams.Time = Mathf.Max(0f, startTimeSeconds); s_MusicSerialId = soundComponent.PlaySound(AssetUtility.GetMusicAsset(drBgm.AssetName), "BGM", Constant.AssetPriority.MusicAsset, playSoundParams, null, userData); s_CurrentBgmId = musicId; s_CurrentBgmStartOffsetSeconds = playSoundParams.Time; s_CurrentBgmStartRealtime = Time.realtimeSinceStartup; return s_MusicSerialId; } public static void StopMusic(this SoundComponent soundComponent) { if (!s_MusicSerialId.HasValue) { return; } soundComponent.StopSound(s_MusicSerialId.Value, FadeVolumeDuration); s_MusicSerialId = null; s_CurrentBgmId = null; s_CurrentBgmStartOffsetSeconds = 0f; s_CurrentBgmStartRealtime = 0f; } public static bool TryGetCurrentBgmSnapshot(this SoundComponent soundComponent, out int bgmId, out float timeSeconds) { bgmId = 0; timeSeconds = 0f; if (!s_MusicSerialId.HasValue || !s_CurrentBgmId.HasValue) { return false; } bgmId = s_CurrentBgmId.Value; timeSeconds = s_CurrentBgmStartOffsetSeconds + Mathf.Max(0f, Time.realtimeSinceStartup - s_CurrentBgmStartRealtime); return true; } public static int? PlaySE(this SoundComponent soundComponent, int uiSoundId, object userData = null) { IDataTable dtUISound = GameEntry.DataTable.GetDataTable(); 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(); } } }