修改设置界面

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,3 @@
fileFormatVersion: 2
guid: d846463d130b4191a5a4a9d2e2dfe45b
timeCreated: 1748180579

View File

@@ -0,0 +1,46 @@
using NBC;
namespace NBF
{
public static class InputCursorExtension
{
public static void InputInit()
{
UI.Inst.On(UIEvents.UIShow, UIShow, null, 1);
UI.Inst.On(UIEvents.UIHide, UIHide, null, 1);
}
public static void Dispose()
{
}
private static void UIShow(EventArgs ev)
{
CheckUICursor();
}
private static void UIHide(EventArgs ev)
{
CheckUICursor();
}
private static void CheckUICursor()
{
var uis = UI.Inst.GetAllUI();
bool showCursor = false;
foreach (var ui in uis)
{
if (!ui.IsShowing) continue;
if (ui.IsShowCursor)
{
showCursor = true;
break;
}
}
Log.Error($"showCursor={showCursor}");
InputManager.IsUIStopInput = showCursor;
InputManager.SetMouseCursor(showCursor);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 0644b8eb12e44351ab75b467f90d4c32
timeCreated: 1748185434

View File

@@ -0,0 +1,206 @@
using System;
using System.Runtime.InteropServices;
using NBC;
// using Rewired;
using UnityEngine;
using UnityEngine.InputSystem;
namespace NBF
{
public enum ControllerType
{
KeyboardMouse = 0,
GamePad = 1
}
public class InputManager : MonoService<InputManager>
{
public static bool IsOp1;
public static bool IsOp2;
public static event Action<bool> OnOp1Action;
public static event Action<bool> OnOp2Action;
/// <summary>
/// 执行输入事件
/// </summary>
public static event Action<string> OnUIPerformed;
/// <summary>
/// 执行输入事件完毕
/// </summary>
public static event Action<string> OnUICanceled;
/// <summary>
/// 执行输入事件
/// </summary>
public static event Action<string> OnPlayerPerformed;
/// <summary>
/// 执行输入事件完毕
/// </summary>
public static event Action<string> OnPlayerCanceled;
/// <summary>
/// 触发交互游戏对象
/// </summary>
public static event Action<InteractiveObject> OnInteractiveObjectAction;
public static PlayerInputControl PlayerInputControl { get; private set; }
/// <summary>
/// 手柄输入
/// </summary>
public static bool IsControllerInput;
/// <summary>
/// ui阻止游戏输入
/// </summary>
public static bool IsUIStopInput;
public static ControllerType ControllerType = ControllerType.KeyboardMouse;
protected override void OnAwake()
{
InputCursorExtension.InputInit();
DontDestroyOnLoad(gameObject);
}
private void Start()
{
PlayerInputControl = new PlayerInputControl();
PlayerInputControl.Enable();
AddEvent();
}
private void OnDestroy()
{
RemoveEvent();
InputCursorExtension.Dispose();
}
public static void SetMouseCursor(bool val)
{
if (val)
{
if (ControllerType == ControllerType.KeyboardMouse)
{
Cursor.visible = true;
}
Cursor.lockState = CursorLockMode.None;
}
else if (ControllerType == ControllerType.KeyboardMouse)
{
Cursor.visible = false;
}
Cursor.visible = val;
if (!val)
{
Cursor.lockState = CursorLockMode.Confined;
}
}
public static Vector2 GetMovementInput()
{
if (IsUIStopInput) return Vector2.zero;
return PlayerInputControl.Player.Move?.ReadValue<Vector2>() ?? Vector2.zero;
}
public static Vector2 GetLookInput()
{
if (IsUIStopInput) return Vector2.zero;
return PlayerInputControl.Player.Look?.ReadValue<Vector2>() ?? Vector2.zero;
}
private void AddEvent()
{
foreach (var actionMap in PlayerInputControl.asset.actionMaps)
{
actionMap.Enable();
if (actionMap.name == "UI")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed += OnUIButtonPerformed;
action.canceled += OnUIButtonCanceled;
}
}
}
else if (actionMap.name == "Player")
{
foreach (var action in actionMap.actions)
{
if (action.type == InputActionType.Button)
{
action.performed += OnPlayerButtonPerformed;
action.canceled += OnPlayerButtonCanceled;
}
}
}
}
}
private void RemoveEvent()
{
OnUIPerformed = null;
OnUICanceled = null;
OnPlayerPerformed = null;
OnPlayerCanceled = null;
}
private void OnUIButtonPerformed(InputAction.CallbackContext context)
{
OnUIPerformed?.Invoke(context.action.name);
}
private void OnUIButtonCanceled(InputAction.CallbackContext context)
{
OnUICanceled?.Invoke(context.action.name);
}
private void OnPlayerButtonPerformed(InputAction.CallbackContext context)
{
if (IsUIStopInput) return;
var actionName = context.action.name;
if (actionName == "Op1")
{
OnOp1Action?.Invoke(true);
}
else if (actionName == "Op2")
{
OnOp2Action?.Invoke(true);
}
OnPlayerPerformed?.Invoke(actionName);
}
private void OnPlayerButtonCanceled(InputAction.CallbackContext context)
{
if (IsUIStopInput) return;
var actionName = context.action.name;
if (actionName == "Op1")
{
OnOp1Action?.Invoke(false);
}
else if (actionName == "Op2")
{
OnOp2Action?.Invoke(false);
}
OnPlayerCanceled?.Invoke(actionName);
}
public void OnInteractiveObject(InteractiveObject interactiveObject)
{
Debug.LogError($"OnInteractiveObject {interactiveObject != null}");
OnInteractiveObjectAction?.Invoke(interactiveObject);
}
}
}

View File

@@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: fecc50928be248b896f6ee0a17e7b76d
timeCreated: 1746892722

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