mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 10:34:30 +08:00
- 调整文档文件中的缩进格式一致性 - 修正所有C#接口和类定义中的注释缩进 - 移除测试代码中不必要的构造函数参数 - 重构条件语句减少嵌套层级 - 规范方法体的大括号使用风格 - 整理全局命名空间声明格式 - 优化方法实现的代码结构和可读性 - [release ci]
58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
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="model">设置模型对象,提供音频设置数据访问</param>
|
||
/// <param name="audioBusMap">音频总线映射对象,定义了不同音频类型的总线名称</param>
|
||
public class GodotAudioSettings(ISettingsModel model, AudioBusMap audioBusMap)
|
||
: IPersistentApplyAbleSettings
|
||
{
|
||
/// <summary>
|
||
/// 应用音频设置到Godot音频系统
|
||
/// </summary>
|
||
/// <returns>表示异步操作的任务</returns>
|
||
public Task Apply()
|
||
{
|
||
var settings = model.GetData<AudioSettings>();
|
||
SetBus(audioBusMap.Master, settings.MasterVolume);
|
||
SetBus(audioBusMap.Bgm, settings.BgmVolume);
|
||
SetBus(audioBusMap.Sfx, settings.SfxVolume);
|
||
return Task.CompletedTask;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 重置音频设置为默认值
|
||
/// </summary>
|
||
public void Reset()
|
||
{
|
||
model.GetData<AudioSettings>().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))
|
||
);
|
||
}
|
||
} |