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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user