修改设置界面

This commit is contained in:
bob
2025-05-29 18:03:24 +08:00
parent cdcb007d6d
commit f421288244
306 changed files with 41744 additions and 612 deletions

View File

@@ -0,0 +1,51 @@
using System;
namespace NBF
{
public abstract class BaseAttribute : Attribute
{
}
[AttributeUsage(AttributeTargets.Field)]
public class InputIconAttribute : BaseAttribute
{
public string KeyBoardIcon;
public string ControllerIcon;
/// <summary>
///
/// </summary>
/// <param name="keyBoardIcon">键盘图标</param>
/// <param name="controllerIcon">控制器图标</param>
public InputIconAttribute(string keyBoardIcon, string controllerIcon)
{
KeyBoardIcon = keyBoardIcon;
ControllerIcon = controllerIcon;
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true)]
public class TitleAttribute : BaseAttribute
{
public string Title;
public TitleAttribute(string title)
{
Title = title;
}
}
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Class, AllowMultiple = true)]
public class DescriptionAttribute : BaseAttribute
{
public string Description;
public DescriptionAttribute(string description)
{
Description = description;
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 647174a4726c4c4c92193aa7dcd3ddad
timeCreated: 1748509835

View File

@@ -1,22 +0,0 @@
using System;
namespace NBF
{
[AttributeUsage(AttributeTargets.Field)]
public class InputIconAttribute : Attribute
{
public string KeyBoardIcon;
public string ControllerIcon;
/// <summary>
///
/// </summary>
/// <param name="keyBoardIcon">键盘图标</param>
/// <param name="controllerIcon">控制器图标</param>
public InputIconAttribute(string keyBoardIcon, string controllerIcon)
{
KeyBoardIcon = keyBoardIcon;
ControllerIcon = controllerIcon;
}
}
}

View File

@@ -1,3 +0,0 @@
fileFormatVersion: 2
guid: 5fb17097160c43ee9840411aded17084
timeCreated: 1748180773

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 011d59a9e9454122bc76461e942de184
timeCreated: 1748489400

View File

@@ -13,10 +13,8 @@ namespace NBF
GamePad = 1
}
public class InputManager : MonoBehaviour
public class InputManager : MonoService<InputManager>
{
public static InputManager Instance { get; private set; }
public static bool IsOp1;
public static bool IsOp2;
@@ -62,9 +60,8 @@ namespace NBF
public static ControllerType ControllerType = ControllerType.KeyboardMouse;
private void Awake()
protected override void OnAwake()
{
Instance = this;
InputCursorExtension.InputInit();
DontDestroyOnLoad(gameObject);
}

View File

@@ -0,0 +1,24 @@
using NBC;
using UnityEngine;
namespace NBF
{
public abstract class MonoService : MonoBehaviour
{
}
public abstract class MonoService<T> : MonoService where T : MonoService
{
public static T Instance { get; private set; }
protected void Awake()
{
Instance = this as T;
OnAwake();
}
protected virtual void OnAwake()
{
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 62c58708f36041db9bb11a7e4d16c62c
timeCreated: 1748491579

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 6111c0ab89e74c9fada884e4e4051549
timeCreated: 1748489421

View 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;
}
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: ae00ae2fd4474db3af9dc52692900f94
timeCreated: 1748489275

View File

@@ -0,0 +1,133 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using UnityEngine;
namespace NBF
{
public static class TypeCache
{
private static readonly Dictionary<Type, List<Type>> _subTypeCache = new Dictionary<Type, List<Type>>();
private static readonly Dictionary<Type, List<Type>> _attributeCache = new Dictionary<Type, List<Type>>();
private static readonly Dictionary<string, Type> _typeNameCache = new Dictionary<string, Type>();
private static Type[] _filteredTypes;
// 目标命名空间
private static readonly string[] TargetNamespaces = { "NBF", "NBC" };
// 初始化缓存
static TypeCache()
{
InitializeCache();
}
// 初始化缓存(仅缓存指定命名空间的类型)
private static void InitializeCache()
{
// 获取Unity默认的主程序集Assembly-CSharp
var mainAssembly = AppDomain.CurrentDomain.GetAssemblies()
.FirstOrDefault(a => a.GetName().Name == "Assembly-CSharp");
if (mainAssembly == null)
{
Debug.LogError("找不到Assembly-CSharp程序集");
_filteredTypes = Array.Empty<Type>();
return;
}
// 过滤出NBF和NBC命名空间的类型
_filteredTypes = mainAssembly.GetTypes()
.Where(t => TargetNamespaces.Any(ns => t.Namespace != null && t.Namespace.StartsWith(ns)))
.ToArray();
// 预缓存类型名
foreach (var type in _filteredTypes)
{
if (type.FullName != null) _typeNameCache[type.FullName] = type;
}
Debug.Log($"TypeCache初始化完成缓存了{_filteredTypes.Length}个类型仅限NBF和NBC命名空间");
}
/// <summary>
/// 获取所有已缓存的类型
/// </summary>
/// <returns></returns>
public static Type[] GetAllTypes()
{
if (_filteredTypes == null)
{
InitializeCache();
}
return _filteredTypes;
}
/// <summary>
/// 通过基类或接口获取所有子类/实现类
/// </summary>
/// <typeparam name="TBase"></typeparam>
/// <returns></returns>
public static List<Type> GetTypesWithBaseType<TBase>() where TBase : class
{
return GetTypesWithBaseType(typeof(TBase));
}
public static List<Type> GetTypesWithBaseType(Type baseType)
{
if (_subTypeCache.TryGetValue(baseType, out var cachedTypes))
{
return cachedTypes;
}
var types = GetAllTypes()
.Where(t => baseType.IsAssignableFrom(t) && t != baseType && !t.IsAbstract && !t.IsInterface)
.ToList();
_subTypeCache[baseType] = types;
return types;
}
/// <summary>
/// 通过特性获取所有类型
/// </summary>
/// <typeparam name="TAttribute"></typeparam>
/// <returns></returns>
public static List<Type> GetTypesWithAttribute<TAttribute>() where TAttribute : Attribute
{
var attributeType = typeof(TAttribute);
if (_attributeCache.TryGetValue(attributeType, out var cachedTypes))
{
return cachedTypes;
}
var types = GetAllTypes()
.Where(t => t.IsDefined(attributeType, false))
.ToList();
_attributeCache[attributeType] = types;
return types;
}
/// <summary>
/// 通过类型名获取类型
/// </summary>
/// <param name="typeName"></param>
/// <returns></returns>
public static Type GetTypeByName(string typeName)
{
return _typeNameCache.GetValueOrDefault(typeName);
}
// 清除缓存
public static void ClearCache()
{
_subTypeCache.Clear();
_attributeCache.Clear();
_typeNameCache.Clear();
_filteredTypes = null;
Debug.Log("TypeCache已清除");
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 12e6673112ac446b8d4658b32d38a5b3
timeCreated: 1748510780