设置扩展

This commit is contained in:
SoulliesOfficial
2026-07-19 16:44:58 -04:00
parent dda354ebb9
commit 9fd5f098ab
110 changed files with 8386 additions and 2311 deletions

View File

@@ -1,78 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Localization.Settings;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
namespace Ichni
{
public class GameSettings
{
public int masterVolume = 50;
public int musicVolume = 50;
public int soundEffectVolume = 50;
public int uiVolume = 50;
public int targetFrame = 60;
public int resolutionLevel = 3;
public int beatmapOffset = 0;
public int languageIndex = 0;
public bool debugMode = false;
public bool judgeType = false;
public bool autoPlay = false;
public bool ShouldUseFullScreenJudge()
{
return judgeType || InformationTransistor.instance?.difficulty?.ForceFullScreenJudge == true;
}
public GameSettings()
{
}
public GameSettings(int masterVolume, int musicVolume, int soundEffectVolume, int uiVolume,
int beatmapOffset, int targetFrame, int resolutionLevel, int languageIndex, bool debugMode,
bool judgeType, bool autoPlay)
{
this.masterVolume = masterVolume;
this.musicVolume = musicVolume;
this.soundEffectVolume = soundEffectVolume;
this.uiVolume = uiVolume;
this.beatmapOffset = beatmapOffset;
this.targetFrame = targetFrame;
this.resolutionLevel = resolutionLevel;
this.languageIndex = languageIndex;
this.debugMode = debugMode;
this.judgeType = judgeType;
this.autoPlay = autoPlay;
}
public void ApplyVolume()
{
AkSoundEngine.SetRTPCValue("MasterVolume", masterVolume);
AkSoundEngine.SetRTPCValue("MusicVolume", musicVolume);
AkSoundEngine.SetRTPCValue("SoundFXVolume", soundEffectVolume);
AkSoundEngine.SetRTPCValue("UIVolume", uiVolume);
}
public void ApplyGraphic()
{
Application.targetFrameRate = targetFrame;
UniversalRenderPipelineAsset currentUrpAsset = GraphicsSettings.defaultRenderPipeline as UniversalRenderPipelineAsset;
currentUrpAsset.renderScale = 0.5f + 0.1f * resolutionLevel;
}
public void ApplyLanguage()
{
I2.Loc.LocalizationManager.CurrentLanguage = MenuManager.instance.languageList[languageIndex];
LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[languageIndex];
I2.Loc.LocalizationManager.UpdateSources();
}
}
}

View File

@@ -1,17 +1,34 @@
using Ichni.Menu;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;
using UnityEngine.Localization.Settings;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
using UnityEngine.SceneManagement;
using UnityEngine.Serialization;
namespace Ichni
{
public class SettingsManager : SerializedMonoBehaviour
{
private const string SettingsSaveFileName = "SettingsSave.json";
private const string SettingsSaveKey = "SettingsSave";
private const string PerformantQualityName = "Performant";
private const string BalancedQualityName = "Balanced";
private const string HighFidelityQualityName = "High Fidelity";
public static SettingsManager instance;
[FormerlySerializedAs("settingsPage")]
public SettingsUIPage settingsUIPage;
public GameSettings gameSettings;
[FormerlySerializedAs("gameSettings")]
public SettingsSaveData settingsSaveData;
// 记录相机在玩家关闭后处理前的原始状态。重新开启时必须恢复原始值,不能无条件将所有相机改为开启。
private readonly Dictionary<int, bool> postProcessingCameraStates = new Dictionary<int, bool>();
private void Awake()
{
@@ -19,6 +36,8 @@ namespace Ichni
{
instance = this;
DontDestroyOnLoad(gameObject);
SceneManager.sceneLoaded += OnSceneLoaded;
RenderPipelineManager.beginCameraRendering += ApplyPostProcessingSettingBeforeCameraRender;
}
else
{
@@ -26,6 +45,18 @@ namespace Ichni
}
}
private void OnDestroy()
{
if (instance != this)
{
return;
}
SceneManager.sceneLoaded -= OnSceneLoaded;
RenderPipelineManager.beginCameraRendering -= ApplyPostProcessingSettingBeforeCameraRender;
instance = null;
}
private void Start()
{
LoadGameSettings();
@@ -34,27 +65,269 @@ namespace Ichni
[Button]
public void SaveGameSettings()
{
string savePath = Application.persistentDataPath + "/GameData/GameSettings.json";
ES3.Save("GameSettings", gameSettings, savePath);
string savePath = Application.persistentDataPath + "/GameData/" + SettingsSaveFileName;
ES3.Save(SettingsSaveKey, settingsSaveData, savePath);
}
[Button]
public void LoadGameSettings()
{
string savePath = Application.persistentDataPath + "/GameData/GameSettings.json";
string savePath = Application.persistentDataPath + "/GameData/" + SettingsSaveFileName;
if (ES3.FileExists(savePath))
{
gameSettings = ES3.Load<GameSettings>("GameSettings", savePath);
// 保留原有 ES3 Key确保本次数据模型重命名不会让已有本地设置失效。
settingsSaveData = ES3.Load<SettingsSaveData>(SettingsSaveKey, savePath);
}
else
{
gameSettings = new GameSettings(
50, 50, 50, 50,
0, 60, 3, 0, false, false, false);
settingsSaveData = CreateDefaultSettingsSaveData();
}
gameSettings.ApplyVolume();
gameSettings.ApplyGraphic();
ApplyAudioSettings();
ApplyGraphicSettings();
}
/// <summary>
/// 将音频分类的当前设置写入 Wwise RTPC。
/// </summary>
public void ApplyAudioSettings()
{
AkSoundEngine.SetRTPCValue("MasterVolume", settingsSaveData.masterVolume);
AkSoundEngine.SetRTPCValue("MusicVolume", settingsSaveData.musicVolume);
AkSoundEngine.SetRTPCValue("SoundFXVolume", settingsSaveData.soundEffectVolume);
AkSoundEngine.SetRTPCValue("UIVolume", settingsSaveData.uiVolume);
ApplyInGameAudioEffectsSettings();
}
/// <summary>
/// 应用游戏内音乐特效开关。关闭时立即清除歌曲正在使用的高通与低通滤波,
/// 防止设置变更后残留上一帧的 RTPC 值。
/// </summary>
public void ApplyInGameAudioEffectsSettings()
{
if (settingsSaveData.enableInGameAudioEffects)
{
return;
}
GameManager.Instance?.songPlayer?.ResetInGameAudioEffects();
}
/// <summary>
/// 将现有图形设置应用到运行时。更细的 Quality、VSync 与平台策略会在 Graphics 分类重构时接入。
/// </summary>
public void ApplyGraphicSettings()
{
int qualityLevelIndex = GetQualityLevelIndex(settingsSaveData.graphicsQualityPreset);
if (QualitySettings.GetQualityLevel() != qualityLevelIndex)
{
QualitySettings.SetQualityLevel(qualityLevelIndex, true);
}
Application.targetFrameRate = settingsSaveData.targetFrame;
UniversalRenderPipelineAsset currentUrpAsset = GraphicsSettings.currentRenderPipeline as UniversalRenderPipelineAsset;
currentUrpAsset.renderScale = 0.5f + 0.1f * settingsSaveData.renderScaleLevel;
ApplyDisplaySettings();
ApplyPostProcessingSettings();
}
/// <summary>
/// 应用后处理总开关与 Bloom 档位。
/// 关闭总开关时,已存在的 URP 相机会直接关闭 <c>renderPostProcessing</c>,并关闭 GameScene 的 Global Volume。
/// 这样既不会执行原生后处理,也不会执行 ScriptablePostProcessor 的自定义 Render Pass。
/// </summary>
public void ApplyPostProcessingSettings()
{
bool enablePostProcessing = settingsSaveData.enablePostProcessing;
UniversalAdditionalCameraData[] cameraDataList =
FindObjectsByType<UniversalAdditionalCameraData>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);
foreach (UniversalAdditionalCameraData cameraData in cameraDataList)
{
int instanceId = cameraData.GetInstanceID();
if (!postProcessingCameraStates.ContainsKey(instanceId))
{
postProcessingCameraStates.Add(instanceId, cameraData.renderPostProcessing);
}
cameraData.renderPostProcessing = enablePostProcessing && postProcessingCameraStates[instanceId];
}
if (PostProcessingManager.Instance != null)
{
PostProcessingManager.Instance.ApplySettings(enablePostProcessing, settingsSaveData.bloomQuality);
}
}
/// <summary>
/// 处理场景切换后新加载的相机与 Global Volume。SettingsManager 会跨场景保留,
/// 因此不能只在首次读取存档时应用一次图形设置。
/// </summary>
private void OnSceneLoaded(Scene scene, LoadSceneMode loadSceneMode)
{
if (settingsSaveData != null)
{
ApplyPostProcessingSettings();
}
}
/// <summary>
/// 兜底处理运行时晚于场景加载创建的相机(例如 Theme 或演出对象中的相机)。
/// 仅在玩家关闭后处理时执行,保证该相机在首次渲染前也不会运行后处理。
/// </summary>
private void ApplyPostProcessingSettingBeforeCameraRender(ScriptableRenderContext context, Camera camera)
{
if (settingsSaveData == null || settingsSaveData.enablePostProcessing ||
!camera.TryGetComponent(out UniversalAdditionalCameraData cameraData))
{
return;
}
int instanceId = cameraData.GetInstanceID();
if (!postProcessingCameraStates.ContainsKey(instanceId))
{
postProcessingCameraStates.Add(instanceId, cameraData.renderPostProcessing);
}
cameraData.renderPostProcessing = false;
}
/// <summary>
/// 将 VSync、窗口模式和分辨率立即应用到 Standalone / Unity Editor。
/// 移动端由系统管理显示模式与分辨率,因此不执行这些设置。
/// </summary>
public void ApplyDisplaySettings()
{
#if UNITY_STANDALONE || UNITY_EDITOR
QualitySettings.vSyncCount = settingsSaveData.vSyncEnabled ? 1 : 0;
if (settingsSaveData.displayResolutionWidth <= 0 || settingsSaveData.displayResolutionHeight <= 0)
{
settingsSaveData.displayResolutionWidth = Screen.width;
settingsSaveData.displayResolutionHeight = Screen.height;
}
Screen.SetResolution(settingsSaveData.displayResolutionWidth, settingsSaveData.displayResolutionHeight,
GetFullScreenMode(settingsSaveData.displayMode));
#endif
}
/// <summary>
/// 首次创建设置存档时,继承 Project Settings 为当前平台指定的默认 Quality Level。
/// 之后玩家的选择由 SettingsSaveData 持久化,不会在下次启动时被平台默认值覆盖。
/// </summary>
private SettingsSaveData CreateDefaultSettingsSaveData()
{
SettingsSaveData defaultSettings = new SettingsSaveData();
defaultSettings.graphicsQualityPreset = GetGraphicsQualityPreset(QualitySettings.GetQualityLevel());
#if UNITY_STANDALONE || UNITY_EDITOR
defaultSettings.displayMode = GetGraphicsDisplayMode(Screen.fullScreenMode);
defaultSettings.displayResolutionWidth = Screen.width;
defaultSettings.displayResolutionHeight = Screen.height;
#endif
return defaultSettings;
}
private FullScreenMode GetFullScreenMode(GraphicsDisplayMode displayMode)
{
return displayMode switch
{
GraphicsDisplayMode.Fullscreen => FullScreenMode.ExclusiveFullScreen,
GraphicsDisplayMode.Windowed => FullScreenMode.Windowed,
_ => FullScreenMode.FullScreenWindow
};
}
private GraphicsDisplayMode GetGraphicsDisplayMode(FullScreenMode fullScreenMode)
{
return fullScreenMode switch
{
FullScreenMode.ExclusiveFullScreen => GraphicsDisplayMode.Fullscreen,
FullScreenMode.Windowed => GraphicsDisplayMode.Windowed,
_ => GraphicsDisplayMode.BorderlessFullscreen
};
}
/// <summary>
/// 将存档枚举映射到 Project Settings 中同名的 Quality Level。
/// 若配置缺失,回退到 Balanced这是移动端的首发默认档位。
/// </summary>
private int GetQualityLevelIndex(GraphicsQualityPreset qualityPreset)
{
string qualityName = GetQualityName(qualityPreset);
string[] qualityNames = QualitySettings.names;
for (int index = 0; index < qualityNames.Length; index++)
{
if (qualityNames[index] == qualityName)
{
return index;
}
}
Debug.LogWarning($"[SettingsManager] 未找到 Quality Level '{qualityName}',回退至 Balanced。");
return FindQualityLevelOrFallback(BalancedQualityName);
}
/// <summary>
/// 将当前 Quality Level 索引转换为可保存的枚举。未知的自定义档位统一按 Balanced 保存。
/// </summary>
private GraphicsQualityPreset GetGraphicsQualityPreset(int qualityLevelIndex)
{
string[] qualityNames = QualitySettings.names;
if (qualityLevelIndex < 0 || qualityLevelIndex >= qualityNames.Length)
{
return GraphicsQualityPreset.Balanced;
}
return qualityNames[qualityLevelIndex] switch
{
PerformantQualityName => GraphicsQualityPreset.Performant,
HighFidelityQualityName => GraphicsQualityPreset.HighFidelity,
_ => GraphicsQualityPreset.Balanced
};
}
private int FindQualityLevelOrFallback(string qualityName)
{
string[] qualityNames = QualitySettings.names;
for (int index = 0; index < qualityNames.Length; index++)
{
if (qualityNames[index] == qualityName)
{
return index;
}
}
return Mathf.Clamp(QualitySettings.GetQualityLevel(), 0, qualityNames.Length - 1);
}
private string GetQualityName(GraphicsQualityPreset qualityPreset)
{
return qualityPreset switch
{
GraphicsQualityPreset.Performant => PerformantQualityName,
GraphicsQualityPreset.HighFidelity => HighFidelityQualityName,
_ => BalancedQualityName
};
}
/// <summary>
/// 将当前语言索引应用到既有的 I2 与 Unity Localization 管线。
/// 语言将于 Localization 重构时改为稳定的 Locale ID本阶段只迁移数据承载方式。
/// </summary>
public void ApplyLanguageSettings()
{
I2.Loc.LocalizationManager.CurrentLanguage = MenuManager.instance.languageList[settingsSaveData.languageIndex];
LocalizationSettings.SelectedLocale = LocalizationSettings.AvailableLocales.Locales[settingsSaveData.languageIndex];
I2.Loc.LocalizationManager.UpdateSources();
}
/// <summary>
/// 判断本谱面是否启用全屏判定。谱面强制要求的配置优先于玩家开关。
/// </summary>
public bool ShouldUseFullScreenJudge()
{
return settingsSaveData.judgeType || InformationTransistor.instance?.difficulty?.ForceFullScreenJudge == true;
}
}
}
}

View File

@@ -0,0 +1,129 @@
using System;
using UnityEngine.Serialization;
namespace Ichni
{
/// <summary>
/// 与 Project Settings 中同名 Quality Level 对应的玩家图形档位。
/// 枚举值仅用于设置存档与 UI 顺序;实际应用时由 SettingsManager 按名称查找 Quality Level
/// 因此调整 Project Settings 的档位顺序不会错误切换到其他渲染 Asset。
/// </summary>
public enum GraphicsQualityPreset
{
Performant = 0,
Balanced = 1,
HighFidelity = 2
}
/// <summary>
/// PC 显示模式。移动端不使用此设置,相关 UI 也不会显示。
/// </summary>
public enum GraphicsDisplayMode
{
Fullscreen = 0,
BorderlessFullscreen = 1,
Windowed = 2
}
/// <summary>
/// Anime Bloom 的质量档位。
/// Off 会保持其他后处理可用,但不执行 BloomPerformance 与 Standard 分别使用预先确定的
/// Diffusion 值。具体数值由 <see cref="PostProcessingManager"/> 统一应用,避免散落在 UI 或谱面代码中。
/// </summary>
public enum BloomQuality
{
Off = 0,
Performance = 1,
Standard = 2
}
/// <summary>
/// Note 与其他演出特效将使用的全局质量档位。
/// 本阶段先完成存档、UI 与运行时读取入口;各特效物体后续按自身开销实现 Low / Medium / High 的实际降级策略。
/// </summary>
public enum VfxQuality
{
Low = 0,
Medium = 1,
High = 2
}
/// <summary>
/// 玩家设置的唯一持久化数据模型。
///
/// 此类只保存设置值,不负责将音量、图形或语言应用到运行时;
/// 运行时应用逻辑统一由 <see cref="SettingsManager"/> 处理。
/// 后续按 Audio、Gameplay、Graphics 等类别拆分字段时,必须保留
/// <see cref="schemaVersion"/>,并在 SettingsManager 中补充对应的迁移逻辑。
/// </summary>
[Serializable]
public class SettingsSaveData
{
/// <summary>
/// 设置存档结构版本。当前为首次建立 SettingsSaveData 的 V1。
/// </summary>
public const int CurrentSchemaVersion = 1;
public int schemaVersion = CurrentSchemaVersion;
// Audio
public int masterVolume = 50;
public int musicVolume = 50;
public int soundEffectVolume = 50;
public int uiVolume = 50;
/// <summary>
/// 是否启用游戏内音乐特效。当前控制 HighPassFilterEffect 与 LowPassFilterEffect
/// 关闭时会立即清除正在歌曲上的滤波 RTPC后续同类特效也应复用此开关。
/// </summary>
public bool enableInGameAudioEffects = true;
// Graphics
/// <summary>
/// 玩家选择的 Quality Preset。新存档会读取当前平台在 Project Settings 中配置的默认档位。
/// </summary>
public GraphicsQualityPreset graphicsQualityPreset = GraphicsQualityPreset.Balanced;
public int targetFrame = 60;
/// <summary>
/// URP Render Scale 档位05 分别对应 0.51.0。
/// 保留 FormerlySerializedAs 以兼容场景中旧的字段名;预发布 ES3 存档可直接重置。
/// </summary>
[FormerlySerializedAs("resolutionLevel")]
public int renderScaleLevel = 3;
/// <summary>
/// 后处理总开关。关闭后,所有 URP 相机都会停用 post-processingGlobal Volume 也不会参与计算;
/// 因此原生与自定义后处理都不会产生渲染开销。
/// </summary>
public bool enablePostProcessing = true;
/// <summary>
/// Anime Bloom 的质量档位。默认 Standard对应 Diffusion 8。
/// </summary>
public BloomQuality bloomQuality = BloomQuality.Standard;
/// <summary>
/// 后续 Note / 演出特效读取的质量档位。默认 High以保持当前未分级特效的视觉基线。
/// </summary>
public VfxQuality vfxQuality = VfxQuality.High;
// Desktop display settings. These values are saved on every platform but only applied on PC and in the Unity Editor.
public bool vSyncEnabled = false;
public GraphicsDisplayMode displayMode = GraphicsDisplayMode.BorderlessFullscreen;
public int displayResolutionWidth = 0;
public int displayResolutionHeight = 0;
// Gameplay
public int beatmapOffset = 0;
// Localization
public int languageIndex = 0;
// Developer settings. 这些字段会在后续分类重构时迁出正式 Release 设置。
public bool debugMode = false;
public bool judgeType = false;
public bool autoPlay = false;
}
}

View File

@@ -1,5 +1,5 @@
fileFormatVersion: 2
guid: c2ca67619927cac4a91d5b0612d6f7eb
guid: a52b8dd62f9e43b69cd3fa97ad8ed71c
MonoImporter:
externalObjects: {}
serializedVersion: 2