修改设置界面
This commit is contained in:
300
Assets/Scripts/Common/Services/Settings/GameSettings.cs
Normal file
300
Assets/Scripts/Common/Services/Settings/GameSettings.cs
Normal file
@@ -0,0 +1,300 @@
|
||||
using Newtonsoft.Json;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Rendering.Universal;
|
||||
|
||||
namespace NBF
|
||||
{
|
||||
public class GameSettings : MonoService<GameSettings>
|
||||
{
|
||||
public enum QualityEnum
|
||||
{
|
||||
Low = 0,
|
||||
Medium = 1,
|
||||
High = 2,
|
||||
Ultra = 3
|
||||
}
|
||||
|
||||
public enum VSyncEnum
|
||||
{
|
||||
Off = 0,
|
||||
On = 1,
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 抗锯齿等级
|
||||
/// </summary>
|
||||
public enum AntiAliasLevelEnum
|
||||
{
|
||||
Off = 1,
|
||||
x2 = 2,
|
||||
x4 = 4,
|
||||
x8 = 8
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 贴图质量
|
||||
/// </summary>
|
||||
public enum TextureQualityEnum
|
||||
{
|
||||
FullRes = 0,
|
||||
HalfRes = 1,
|
||||
QuarterRes = 2,
|
||||
EighthRes = 3
|
||||
}
|
||||
|
||||
public enum AnisotropicLevelEnum
|
||||
{
|
||||
Off = -1,
|
||||
x2 = 2,
|
||||
x4 = 4,
|
||||
x8 = 8,
|
||||
x16 = 16
|
||||
}
|
||||
|
||||
public enum ShadowmapResolutionEnum
|
||||
{
|
||||
Low = 0,
|
||||
Medium = 1,
|
||||
High = 2,
|
||||
}
|
||||
|
||||
public const string SaveKey = "PlayerGameSetting";
|
||||
|
||||
[System.Serializable]
|
||||
public class GameSettingsData
|
||||
{
|
||||
/// <summary>
|
||||
/// 质量等级 0 1 2 3
|
||||
/// </summary>
|
||||
[Title("Settings_Title_quality")] [Description("Settings_Description_quality")]
|
||||
public QualityEnum QualityLevel = QualityEnum.High;
|
||||
|
||||
/// <summary>
|
||||
/// 显示分辨率,有任意一个为0,则为全屏
|
||||
/// </summary>
|
||||
[Title("Settings_Title_Resolution")] [Description("Settings_Description_Resolution")]
|
||||
public Vector2Int Resolution = new Vector2Int(0, 0);
|
||||
|
||||
/// <summary>
|
||||
/// 渲染比例
|
||||
/// </summary>
|
||||
[Title("Settings_Title_RenderScale")] [Description("Settings_Description_RenderScale")] [Range(0.1f, 2f)]
|
||||
public float RenderScale = 1f;
|
||||
|
||||
/// <summary>
|
||||
/// 窗口模式
|
||||
/// </summary>
|
||||
[Title("Settings_Title_WindowedMode")] [Description("Settings_Description_WindowedMode")]
|
||||
public FullScreenMode WindowedMode = FullScreenMode.MaximizedWindow;
|
||||
|
||||
/// <summary>
|
||||
/// 垂直同步个数
|
||||
/// </summary>
|
||||
[Title("Settings_Title_VSync")] [Description("Settings_Description_VSync")]
|
||||
public VSyncEnum VSync = VSyncEnum.Off;
|
||||
|
||||
/// <summary>
|
||||
///抗锯齿等级
|
||||
/// </summary>
|
||||
[Title("Settings_Title_AntiAliasLevel")] [Description("Settings_Description_AntiAliasLevel")]
|
||||
public AntiAliasLevelEnum AntiAliasLevel = AntiAliasLevelEnum.Off;
|
||||
|
||||
/// <summary>
|
||||
/// 纹理质量
|
||||
/// </summary>
|
||||
[Title("Settings_Title_TextureQuality")] [Description("Settings_Description_TextureQuality")]
|
||||
public TextureQualityEnum TextureQuality = TextureQualityEnum.FullRes;
|
||||
|
||||
/// <summary>
|
||||
/// 全局各向异性纹理过滤模式
|
||||
/// </summary>
|
||||
[Title("Settings_Title_AnisotropicMode")] [Description("Settings_Description_AnisotropicMode")]
|
||||
public AnisotropicFiltering AnisotropicMode = AnisotropicFiltering.Enable;
|
||||
|
||||
/// <summary>
|
||||
/// 全局各向异性过滤限制
|
||||
/// </summary>
|
||||
[Title("Settings_Description_AnisotropicLevel")] [Description("Settings_Description_AnisotropicLevel")]
|
||||
public AnisotropicLevelEnum AnisotropicLevel = AnisotropicLevelEnum.x4;
|
||||
|
||||
/// <summary>
|
||||
/// 阴影等级
|
||||
/// </summary>
|
||||
[Title("Settings_Description_ShadowmapResolution")]
|
||||
[Description("Settings_Description_ShadowmapResolution")]
|
||||
public ShadowmapResolutionEnum ShadowmapResolution = ShadowmapResolutionEnum.High;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 默认配置
|
||||
/// </summary>
|
||||
private GameSettingsData DefaultSettingsConverted = new GameSettingsData();
|
||||
|
||||
/// <summary>
|
||||
/// 当前正在调整的配置
|
||||
/// </summary>
|
||||
private GameSettingsData CurrentSettings = new GameSettingsData();
|
||||
|
||||
/// <summary>
|
||||
/// 当前使用配置
|
||||
/// </summary>
|
||||
public readonly GameSettingsData UseSettings = new GameSettingsData();
|
||||
|
||||
protected override void OnAwake()
|
||||
{
|
||||
LoadSettings();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 重置
|
||||
/// </summary>
|
||||
public void ResetSettings()
|
||||
{
|
||||
CurrentSettings.QualityLevel = DefaultSettingsConverted.QualityLevel;
|
||||
CurrentSettings.Resolution = DefaultSettingsConverted.Resolution;
|
||||
CurrentSettings.RenderScale = DefaultSettingsConverted.RenderScale;
|
||||
CurrentSettings.WindowedMode = DefaultSettingsConverted.WindowedMode;
|
||||
CurrentSettings.VSync = DefaultSettingsConverted.VSync;
|
||||
CurrentSettings.AntiAliasLevel = DefaultSettingsConverted.AntiAliasLevel;
|
||||
CurrentSettings.TextureQuality = DefaultSettingsConverted.TextureQuality;
|
||||
CurrentSettings.AnisotropicMode = DefaultSettingsConverted.AnisotropicMode;
|
||||
CurrentSettings.AnisotropicLevel = DefaultSettingsConverted.AnisotropicLevel;
|
||||
CurrentSettings.ShadowmapResolution = DefaultSettingsConverted.ShadowmapResolution;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始进入设置模式,将当前使用的配置缓存一份
|
||||
/// </summary>
|
||||
public void BeginChangeSettings()
|
||||
{
|
||||
UseSettings.QualityLevel = CurrentSettings.QualityLevel;
|
||||
UseSettings.Resolution = CurrentSettings.Resolution;
|
||||
UseSettings.RenderScale = CurrentSettings.RenderScale;
|
||||
UseSettings.WindowedMode = CurrentSettings.WindowedMode;
|
||||
UseSettings.VSync = CurrentSettings.VSync;
|
||||
UseSettings.AntiAliasLevel = CurrentSettings.AntiAliasLevel;
|
||||
UseSettings.TextureQuality = CurrentSettings.TextureQuality;
|
||||
UseSettings.AnisotropicMode = CurrentSettings.AnisotropicMode;
|
||||
UseSettings.AnisotropicLevel = CurrentSettings.AnisotropicLevel;
|
||||
UseSettings.ShadowmapResolution = CurrentSettings.ShadowmapResolution;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存设置
|
||||
/// </summary>
|
||||
public void SaveSettings()
|
||||
{
|
||||
CurrentSettings.QualityLevel = UseSettings.QualityLevel;
|
||||
CurrentSettings.Resolution = UseSettings.Resolution;
|
||||
CurrentSettings.RenderScale = UseSettings.RenderScale;
|
||||
CurrentSettings.WindowedMode = UseSettings.WindowedMode;
|
||||
CurrentSettings.VSync = UseSettings.VSync;
|
||||
CurrentSettings.AntiAliasLevel = UseSettings.AntiAliasLevel;
|
||||
CurrentSettings.TextureQuality = UseSettings.TextureQuality;
|
||||
CurrentSettings.AnisotropicMode = UseSettings.AnisotropicMode;
|
||||
CurrentSettings.AnisotropicLevel = UseSettings.AnisotropicLevel;
|
||||
CurrentSettings.ShadowmapResolution = UseSettings.ShadowmapResolution;
|
||||
|
||||
|
||||
var json = JsonConvert.SerializeObject(CurrentSettings);
|
||||
PlayerPrefs.SetString(SaveKey, json);
|
||||
}
|
||||
|
||||
private void LoadSettings()
|
||||
{
|
||||
ResetSettings();
|
||||
if (PlayerPrefs.HasKey(SaveKey))
|
||||
{
|
||||
var json = PlayerPrefs.GetString(SaveKey, string.Empty);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
{
|
||||
var settings = JsonConvert.DeserializeObject<GameSettingsData>(json);
|
||||
if (settings != null)
|
||||
{
|
||||
CurrentSettings.QualityLevel = settings.QualityLevel;
|
||||
CurrentSettings.Resolution = settings.Resolution;
|
||||
CurrentSettings.RenderScale = settings.RenderScale;
|
||||
CurrentSettings.WindowedMode = settings.WindowedMode;
|
||||
CurrentSettings.VSync = settings.VSync;
|
||||
CurrentSettings.AntiAliasLevel = settings.AntiAliasLevel;
|
||||
CurrentSettings.TextureQuality = settings.TextureQuality;
|
||||
CurrentSettings.AnisotropicMode = settings.AnisotropicMode;
|
||||
CurrentSettings.AnisotropicLevel = settings.AnisotropicLevel;
|
||||
CurrentSettings.ShadowmapResolution = settings.ShadowmapResolution;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (CurrentSettings.Resolution.x < 1 || CurrentSettings.Resolution.y < 1)
|
||||
{
|
||||
CurrentSettings.WindowedMode = FullScreenMode.ExclusiveFullScreen;
|
||||
CurrentSettings.Resolution = new Vector2Int(Screen.width, Screen.height);
|
||||
}
|
||||
|
||||
ApplySettings();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 应用设置
|
||||
/// </summary>
|
||||
private void ApplySettings()
|
||||
{
|
||||
// // 设置当前质量等级
|
||||
QualitySettings.SetQualityLevel((int)CurrentSettings.QualityLevel);
|
||||
|
||||
switch (CurrentSettings.WindowedMode)
|
||||
{
|
||||
case FullScreenMode.ExclusiveFullScreen:
|
||||
Screen.SetResolution(Screen.width, Screen.width, FullScreenMode.ExclusiveFullScreen);
|
||||
break;
|
||||
case FullScreenMode.FullScreenWindow:
|
||||
Screen.SetResolution(CurrentSettings.Resolution.x, CurrentSettings.Resolution.y,
|
||||
FullScreenMode.FullScreenWindow);
|
||||
break;
|
||||
case FullScreenMode.MaximizedWindow:
|
||||
Screen.SetResolution(CurrentSettings.Resolution.x, CurrentSettings.Resolution.y,
|
||||
FullScreenMode.MaximizedWindow);
|
||||
break;
|
||||
case FullScreenMode.Windowed:
|
||||
Screen.SetResolution(CurrentSettings.Resolution.x, CurrentSettings.Resolution.y,
|
||||
FullScreenMode.Windowed);
|
||||
break;
|
||||
default:
|
||||
Screen.SetResolution(Screen.width, Screen.width, FullScreenMode.ExclusiveFullScreen);
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
// 获取当前URP Asset
|
||||
UniversalRenderPipelineAsset URPAsset =
|
||||
QualitySettings.GetRenderPipelineAssetAt(QualitySettings.GetQualityLevel()) as
|
||||
UniversalRenderPipelineAsset;
|
||||
if (URPAsset)
|
||||
{
|
||||
//渲染比例
|
||||
URPAsset.renderScale = CurrentSettings.RenderScale;
|
||||
//抗锯齿等级
|
||||
URPAsset.msaaSampleCount = (int)CurrentSettings.AntiAliasLevel;
|
||||
URPAsset.supportsHDR = true;
|
||||
//纹理质量
|
||||
QualitySettings.globalTextureMipmapLimit = (int)CurrentSettings.TextureQuality;
|
||||
|
||||
QualitySettings.anisotropicFiltering = CurrentSettings.AnisotropicMode;
|
||||
if (CurrentSettings.AnisotropicMode == AnisotropicFiltering.Disable ||
|
||||
CurrentSettings.AnisotropicMode == AnisotropicFiltering.Enable)
|
||||
{
|
||||
Texture.SetGlobalAnisotropicFilteringLimits(-1, -1);
|
||||
}
|
||||
else if (CurrentSettings.AnisotropicMode == AnisotropicFiltering.ForceEnable)
|
||||
{
|
||||
Texture.SetGlobalAnisotropicFilteringLimits((int)CurrentSettings.AnisotropicLevel,
|
||||
(int)CurrentSettings.AnisotropicLevel);
|
||||
}
|
||||
|
||||
//垂直同步
|
||||
QualitySettings.vSyncCount = (int)CurrentSettings.VSync;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ae00ae2fd4474db3af9dc52692900f94
|
||||
timeCreated: 1748489275
|
||||
Reference in New Issue
Block a user