GFramework/GFramework.Godot/setting/GodotAudioSettings.cs
GeWuYou 31045f305c refactor(core): 调整命名空间结构以优化模块组织
- 将 IRegistry 接口从 GFramework.Game.Abstractions.registry 移至 GFramework.Core.Abstractions.registries
- 移除 KeyValueRegistryBase 中对旧命名空间的引用
- 将 IsExternalInit 类从 System.Runtime.CompilerServices 移至 GFramework.Game.Abstractions.internals
- 更新 IGameSceneRegistry 对新注册表命名空间的引用
- 将音频和图形设置类移至 GFramework.Game.Abstractions.setting.data 子命名空间
- 将资产注册表接口更新为使用新的核心注册表命名空间
- 调整 Godot 模块中的音频总线映射命名空间至 data 子目录
- 更新 Godot 音频和图形设置类以引用正确的设置数据命名空间
2026-01-27 22:58:37 +08:00

54 lines
1.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using GFramework.Game.Abstractions.setting;
using GFramework.Game.Abstractions.setting.data;
using GFramework.Godot.setting.data;
using Godot;
namespace GFramework.Godot.setting;
/// <summary>
/// Godot音频设置实现类用于应用音频配置到Godot音频系统
/// </summary>
/// <param name="settings">音频设置对象,包含主音量、背景音乐音量和音效音量</param>
/// <param name="audioBusMap">音频总线映射对象,定义了不同音频类型的总线名称</param>
public class GodotAudioSettings(AudioSettings settings, AudioBusMap audioBusMap)
: IPersistentApplyAbleSettings
{
/// <summary>
/// 应用音频设置到Godot音频系统
/// </summary>
/// <returns>表示异步操作的任务</returns>
public Task Apply()
{
SetBus(audioBusMap.Master, settings.MasterVolume);
SetBus(audioBusMap.Bgm, settings.BgmVolume);
SetBus(audioBusMap.Sfx, settings.SfxVolume);
return Task.CompletedTask;
}
public void Reset()
{
settings.Reset();
}
/// <summary>
/// 设置指定音频总线的音量
/// </summary>
/// <param name="busName">音频总线名称</param>
/// <param name="linear">线性音量值0-1之间</param>
private static void SetBus(string busName, float linear)
{
// 获取音频总线索引
var idx = AudioServer.GetBusIndex(busName);
if (idx < 0)
{
GD.PushWarning($"Audio bus not found: {busName}");
return;
}
// 将线性音量转换为分贝并设置到音频总线
AudioServer.SetBusVolumeDb(
idx,
Mathf.LinearToDb(Mathf.Clamp(linear, 0.0001f, 1f))
);
}
}