feat():大改前的提交,这个版本保存了所有冗余的代码
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.InputSystem;
|
||||
using Core;
|
||||
|
||||
namespace Input
|
||||
namespace Script.Gameplay.Input
|
||||
{
|
||||
public class InputManager : MonoSingleton<InputManager>, IInputManager
|
||||
{
|
||||
private PlayerInputActions _input; // 自动生成的输入类
|
||||
public PlayerInputActions Input; // 自动生成的输入类
|
||||
|
||||
// 当前输入值
|
||||
public Vector2 Move { get; private set; }
|
||||
@@ -14,36 +15,43 @@ namespace Input
|
||||
public bool JumpPressed { get; private set; }
|
||||
public bool PausePressed { get; private set; }
|
||||
public bool InteractPressed { get; private set; }
|
||||
public bool EditPressed { get; private set; }
|
||||
|
||||
private bool _hasFocus = true;
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_input = new PlayerInputActions();
|
||||
Input = new PlayerInputActions();
|
||||
}
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_input.Enable();
|
||||
Input.Enable();
|
||||
|
||||
// 注册事件
|
||||
_input.Player.Move.performed += ctx => Move = ctx.ReadValue<Vector2>();
|
||||
_input.Player.Move.canceled += ctx => Move = Vector2.zero;
|
||||
Input.Player.Move.performed += ctx => Move = ctx.ReadValue<Vector2>();
|
||||
Input.Player.Move.canceled += ctx => Move = Vector2.zero;
|
||||
|
||||
_input.Player.Look.performed += ctx => Look = ctx.ReadValue<Vector2>();
|
||||
_input.Player.Look.canceled += ctx => Look = Vector2.zero;
|
||||
Input.Player.Look.performed += ctx => Look = ctx.ReadValue<Vector2>();
|
||||
Input.Player.Look.canceled += ctx => Look = Vector2.zero;
|
||||
|
||||
_input.Player.Jump.performed += ctx => JumpPressed = true;
|
||||
_input.Player.Jump.canceled += ctx => JumpPressed = false;
|
||||
Input.Player.Jump.performed += ctx => JumpPressed = true;
|
||||
Input.Player.Jump.canceled += ctx => JumpPressed = false;
|
||||
|
||||
Input.Player.Pause.performed += ctx => PausePressed = true;
|
||||
Input.Player.Pause.canceled += ctx => PausePressed = false;
|
||||
|
||||
// _input.Player.Pause.performed += ctx => PausePressed = true;
|
||||
// _input.Player.Pause.canceled += ctx => PausePressed = false;
|
||||
Input.Player.Interact.performed += ctx => InteractPressed = true;
|
||||
Input.Player.Interact.canceled += ctx => InteractPressed = false;
|
||||
|
||||
_input.Player.Interact.performed += ctx => InteractPressed = true;
|
||||
_input.Player.Interact.canceled += ctx => InteractPressed = false;
|
||||
Input.Player.Edit.performed += ctx => EditPressed = true;
|
||||
Input.Player.Edit.canceled += ctx => EditPressed = false;
|
||||
}
|
||||
|
||||
private void OnDisable()
|
||||
{
|
||||
_input.Disable();
|
||||
// 可选:取消注册以防止重复订阅(简单项目可不解除)
|
||||
Input.Disable();
|
||||
}
|
||||
|
||||
private void Start()
|
||||
@@ -57,15 +65,15 @@ namespace Input
|
||||
// if (PausePressed)
|
||||
// {
|
||||
// Debug.Log("Pause Pressed!");
|
||||
// PausePressed = false; // 手动清除
|
||||
// PausePressed = false; // 手动清除(如果需要瞬时)
|
||||
// }
|
||||
}
|
||||
|
||||
// 🔧 示例方法:允许外部模块手动启用/禁用输入(比如暂停菜单)
|
||||
public void SetInputEnabled(bool enabled)
|
||||
{
|
||||
if (enabled) _input.Enable();
|
||||
else _input.Disable();
|
||||
if (enabled) Input.Enable();
|
||||
else Input.Disable();
|
||||
}
|
||||
|
||||
public void SetCursorState(bool visible, CursorLockMode lockMode)
|
||||
@@ -78,11 +86,11 @@ namespace Input
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
_input.Player.Look.Enable();
|
||||
Input.Player.Look.Enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
_input.Player.Look.Disable();
|
||||
Input.Player.Look.Disable();
|
||||
Look = Vector2.zero; // 禁用时清除Look值
|
||||
}
|
||||
}
|
||||
@@ -91,13 +99,28 @@ namespace Input
|
||||
{
|
||||
if (enabled)
|
||||
{
|
||||
_input.Player.Move.Enable();
|
||||
Input.Player.Move.Enable();
|
||||
}
|
||||
else
|
||||
{
|
||||
_input.Player.Move.Disable();
|
||||
Input.Player.Move.Disable();
|
||||
Move = Vector2.zero; // 禁用时清除Move值
|
||||
}
|
||||
}
|
||||
|
||||
// 当窗口获得/失去焦点时调用(玩家是否在注视游戏窗口)
|
||||
private void OnApplicationFocus(bool hasFocus)
|
||||
{
|
||||
_hasFocus = hasFocus;
|
||||
// 失去焦点视为系统级暂停
|
||||
if (_hasFocus)
|
||||
{
|
||||
SetCursorState(_hasFocus, CursorLockMode.Locked);
|
||||
}
|
||||
else
|
||||
{
|
||||
SetCursorState(_hasFocus, CursorLockMode.None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -15,7 +15,7 @@ using System.Collections.Generic;
|
||||
using UnityEngine.InputSystem;
|
||||
using UnityEngine.InputSystem.Utilities;
|
||||
|
||||
namespace Input
|
||||
namespace Script.Gameplay.Input
|
||||
{
|
||||
public partial class @PlayerInputActions: IInputActionCollection2, IDisposable
|
||||
{
|
||||
@@ -64,6 +64,33 @@ namespace Input
|
||||
""processors"": """",
|
||||
""interactions"": """",
|
||||
""initialStateCheck"": false
|
||||
},
|
||||
{
|
||||
""name"": ""Pause"",
|
||||
""type"": ""Button"",
|
||||
""id"": ""822c2284-94e3-4636-894c-adfe85ac467d"",
|
||||
""expectedControlType"": """",
|
||||
""processors"": """",
|
||||
""interactions"": """",
|
||||
""initialStateCheck"": false
|
||||
},
|
||||
{
|
||||
""name"": ""SwitchWatchMode"",
|
||||
""type"": ""Button"",
|
||||
""id"": ""73ffcbfd-15ae-4580-99c4-d7d06ff13bb6"",
|
||||
""expectedControlType"": """",
|
||||
""processors"": """",
|
||||
""interactions"": """",
|
||||
""initialStateCheck"": false
|
||||
},
|
||||
{
|
||||
""name"": ""Edit"",
|
||||
""type"": ""Button"",
|
||||
""id"": ""20a9157a-918b-42f1-80b1-9e014426068e"",
|
||||
""expectedControlType"": """",
|
||||
""processors"": """",
|
||||
""interactions"": """",
|
||||
""initialStateCheck"": false
|
||||
}
|
||||
],
|
||||
""bindings"": [
|
||||
@@ -165,6 +192,39 @@ namespace Input
|
||||
""action"": ""Interact"",
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""943ba523-b024-410e-a250-735a5ffdf786"",
|
||||
""path"": ""<Keyboard>/escape"",
|
||||
""interactions"": """",
|
||||
""processors"": """",
|
||||
""groups"": """",
|
||||
""action"": ""Pause"",
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""47af0242-9835-4b1a-82c8-e3fc32489870"",
|
||||
""path"": ""<Keyboard>/tab"",
|
||||
""interactions"": """",
|
||||
""processors"": """",
|
||||
""groups"": """",
|
||||
""action"": ""SwitchWatchMode"",
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
},
|
||||
{
|
||||
""name"": """",
|
||||
""id"": ""9e9bf8ec-8b3e-4230-a10c-ae874395711a"",
|
||||
""path"": ""<Keyboard>/x"",
|
||||
""interactions"": """",
|
||||
""processors"": """",
|
||||
""groups"": """",
|
||||
""action"": ""Edit"",
|
||||
""isComposite"": false,
|
||||
""isPartOfComposite"": false
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -177,6 +237,9 @@ namespace Input
|
||||
m_Player_Look = m_Player.FindAction("Look", throwIfNotFound: true);
|
||||
m_Player_Jump = m_Player.FindAction("Jump", throwIfNotFound: true);
|
||||
m_Player_Interact = m_Player.FindAction("Interact", throwIfNotFound: true);
|
||||
m_Player_Pause = m_Player.FindAction("Pause", throwIfNotFound: true);
|
||||
m_Player_SwitchWatchMode = m_Player.FindAction("SwitchWatchMode", throwIfNotFound: true);
|
||||
m_Player_Edit = m_Player.FindAction("Edit", throwIfNotFound: true);
|
||||
}
|
||||
|
||||
~@PlayerInputActions()
|
||||
@@ -247,6 +310,9 @@ namespace Input
|
||||
private readonly InputAction m_Player_Look;
|
||||
private readonly InputAction m_Player_Jump;
|
||||
private readonly InputAction m_Player_Interact;
|
||||
private readonly InputAction m_Player_Pause;
|
||||
private readonly InputAction m_Player_SwitchWatchMode;
|
||||
private readonly InputAction m_Player_Edit;
|
||||
public struct PlayerActions
|
||||
{
|
||||
private @PlayerInputActions m_Wrapper;
|
||||
@@ -255,6 +321,9 @@ namespace Input
|
||||
public InputAction @Look => m_Wrapper.m_Player_Look;
|
||||
public InputAction @Jump => m_Wrapper.m_Player_Jump;
|
||||
public InputAction @Interact => m_Wrapper.m_Player_Interact;
|
||||
public InputAction @Pause => m_Wrapper.m_Player_Pause;
|
||||
public InputAction @SwitchWatchMode => m_Wrapper.m_Player_SwitchWatchMode;
|
||||
public InputAction @Edit => m_Wrapper.m_Player_Edit;
|
||||
public InputActionMap Get() { return m_Wrapper.m_Player; }
|
||||
public void Enable() { Get().Enable(); }
|
||||
public void Disable() { Get().Disable(); }
|
||||
@@ -276,6 +345,15 @@ namespace Input
|
||||
@Interact.started += instance.OnInteract;
|
||||
@Interact.performed += instance.OnInteract;
|
||||
@Interact.canceled += instance.OnInteract;
|
||||
@Pause.started += instance.OnPause;
|
||||
@Pause.performed += instance.OnPause;
|
||||
@Pause.canceled += instance.OnPause;
|
||||
@SwitchWatchMode.started += instance.OnSwitchWatchMode;
|
||||
@SwitchWatchMode.performed += instance.OnSwitchWatchMode;
|
||||
@SwitchWatchMode.canceled += instance.OnSwitchWatchMode;
|
||||
@Edit.started += instance.OnEdit;
|
||||
@Edit.performed += instance.OnEdit;
|
||||
@Edit.canceled += instance.OnEdit;
|
||||
}
|
||||
|
||||
private void UnregisterCallbacks(IPlayerActions instance)
|
||||
@@ -292,6 +370,15 @@ namespace Input
|
||||
@Interact.started -= instance.OnInteract;
|
||||
@Interact.performed -= instance.OnInteract;
|
||||
@Interact.canceled -= instance.OnInteract;
|
||||
@Pause.started -= instance.OnPause;
|
||||
@Pause.performed -= instance.OnPause;
|
||||
@Pause.canceled -= instance.OnPause;
|
||||
@SwitchWatchMode.started -= instance.OnSwitchWatchMode;
|
||||
@SwitchWatchMode.performed -= instance.OnSwitchWatchMode;
|
||||
@SwitchWatchMode.canceled -= instance.OnSwitchWatchMode;
|
||||
@Edit.started -= instance.OnEdit;
|
||||
@Edit.performed -= instance.OnEdit;
|
||||
@Edit.canceled -= instance.OnEdit;
|
||||
}
|
||||
|
||||
public void RemoveCallbacks(IPlayerActions instance)
|
||||
@@ -315,6 +402,9 @@ namespace Input
|
||||
void OnLook(InputAction.CallbackContext context);
|
||||
void OnJump(InputAction.CallbackContext context);
|
||||
void OnInteract(InputAction.CallbackContext context);
|
||||
void OnPause(InputAction.CallbackContext context);
|
||||
void OnSwitchWatchMode(InputAction.CallbackContext context);
|
||||
void OnEdit(InputAction.CallbackContext context);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user