refactor(UI):完善对于各个界面的管理逻辑,esc可以直接关闭所有的顶层UI

This commit is contained in:
2025-10-27 21:41:25 +08:00
parent 1462f4689e
commit 4951f25187
21 changed files with 18049 additions and 6374 deletions

View File

@@ -9,6 +9,7 @@ namespace Script.Gameplay.Input
public class InputManager : MonoSingleton<InputManager>, IInputManager
{
public PlayerInputActions Input; // 自动生成的输入类
private UIManager _uiManager;
// 当前输入值
public Vector2 Move { get; private set; }
@@ -58,7 +59,8 @@ namespace Script.Gameplay.Input
Input.Player.Edit.performed += ctx => EditPressed = true;
Input.Player.Edit.canceled += ctx => EditPressed = false;
UIManager.Instance.RegisterInputManager(this);
_uiManager = UIManager.Instance;
_uiManager.RegisterInputManager(this);
}
private void Update()

View File

@@ -154,6 +154,15 @@ namespace Script.Gameplay.Input
""processors"": """",
""interactions"": """",
""initialStateCheck"": false
},
{
""name"": ""Escape"",
""type"": ""Button"",
""id"": ""03c74252-1ecd-4fd7-af3e-d87cef62965f"",
""expectedControlType"": """",
""processors"": """",
""interactions"": """",
""initialStateCheck"": false
}
],
""bindings"": [
@@ -354,6 +363,17 @@ namespace Script.Gameplay.Input
""action"": ""Setting"",
""isComposite"": false,
""isPartOfComposite"": false
},
{
""name"": """",
""id"": ""a8cf95c0-2bad-4ed5-a737-e55f56e3306c"",
""path"": ""<Keyboard>/escape"",
""interactions"": """",
""processors"": """",
""groups"": """",
""action"": ""Escape"",
""isComposite"": false,
""isPartOfComposite"": false
}
]
}
@@ -376,6 +396,7 @@ namespace Script.Gameplay.Input
m_Player_ShowNextDialogue = m_Player.FindAction("ShowNextDialogue", throwIfNotFound: true);
m_Player_Read = m_Player.FindAction("Read", throwIfNotFound: true);
m_Player_Setting = m_Player.FindAction("Setting", throwIfNotFound: true);
m_Player_Escape = m_Player.FindAction("Escape", throwIfNotFound: true);
}
~@PlayerInputActions()
@@ -456,6 +477,7 @@ namespace Script.Gameplay.Input
private readonly InputAction m_Player_ShowNextDialogue;
private readonly InputAction m_Player_Read;
private readonly InputAction m_Player_Setting;
private readonly InputAction m_Player_Escape;
public struct PlayerActions
{
private @PlayerInputActions m_Wrapper;
@@ -474,6 +496,7 @@ namespace Script.Gameplay.Input
public InputAction @ShowNextDialogue => m_Wrapper.m_Player_ShowNextDialogue;
public InputAction @Read => m_Wrapper.m_Player_Read;
public InputAction @Setting => m_Wrapper.m_Player_Setting;
public InputAction @Escape => m_Wrapper.m_Player_Escape;
public InputActionMap Get() { return m_Wrapper.m_Player; }
public void Enable() { Get().Enable(); }
public void Disable() { Get().Disable(); }
@@ -525,6 +548,9 @@ namespace Script.Gameplay.Input
@Setting.started += instance.OnSetting;
@Setting.performed += instance.OnSetting;
@Setting.canceled += instance.OnSetting;
@Escape.started += instance.OnEscape;
@Escape.performed += instance.OnEscape;
@Escape.canceled += instance.OnEscape;
}
private void UnregisterCallbacks(IPlayerActions instance)
@@ -571,6 +597,9 @@ namespace Script.Gameplay.Input
@Setting.started -= instance.OnSetting;
@Setting.performed -= instance.OnSetting;
@Setting.canceled -= instance.OnSetting;
@Escape.started -= instance.OnEscape;
@Escape.performed -= instance.OnEscape;
@Escape.canceled -= instance.OnEscape;
}
public void RemoveCallbacks(IPlayerActions instance)
@@ -604,6 +633,7 @@ namespace Script.Gameplay.Input
void OnShowNextDialogue(InputAction.CallbackContext context);
void OnRead(InputAction.CallbackContext context);
void OnSetting(InputAction.CallbackContext context);
void OnEscape(InputAction.CallbackContext context);
}
}
}

View File

@@ -20,14 +20,15 @@ namespace Script.Gameplay.Player
public event Action OnPlayerEndDialogue;
public event Action<GameObject> OnGazeEnterDialogue;
public event Action<GameObject> OnGazeExitDialogue;
private InputManager inputManager;
private Queue<string> dialogueQueue = new Queue<string>();
private PlayerDialogueViewer playerDialogueViewer;
private IDialogue previousDialogue;
private IDialogue currentDialogueTarget;
public IDialogue CurrentDialogueTarget
{
get => currentDialogueTarget;
@@ -41,6 +42,7 @@ namespace Script.Gameplay.Player
{
OnGazeEnterDialogue?.Invoke((currentDialogueTarget as MonoBehaviour)?.gameObject);
}
if (previousDialogue != null)
{
OnGazeExitDialogue?.Invoke((previousDialogue as MonoBehaviour)?.gameObject);
@@ -63,10 +65,16 @@ namespace Script.Gameplay.Player
var input = inputManager.Input;
input.Player.Read.performed += OnReadOnperformed;
input.Player.ShowNextDialogue.performed += OnShowNextDialogueOnperformed;
input.Player.Escape.performed += OnEscapeOnperformed;
ControllerLocator.Instance.Register(this);
}
private void OnEscapeOnperformed(InputAction.CallbackContext ctx)
{
EndDialogue();
}
private void OnShowNextDialogueOnperformed(InputAction.CallbackContext ctx)
{
if (!isEnablePlayerDialogue) return;
@@ -104,8 +112,6 @@ namespace Script.Gameplay.Player
OnPlayerBeginDialogue?.Invoke();
CurrentDialogueTarget.OnBeginDialogue?.Invoke(true);
dialogueQueue = SplitDialogue(CurrentDialogueTarget.DialogueContent);
inputManager.SetInputForLook(false);
inputManager.SetInputForMove(false);
PassNextDialogue();
}
}
@@ -135,13 +141,7 @@ namespace Script.Gameplay.Player
isReadingDialogue = false;
OnPlayerEndDialogue?.Invoke();
CurrentDialogueTarget.OnEndDialogue?.Invoke(true);
inputManager.SetInputForLook(true);
inputManager.SetInputForMove(true);
if (playerDialogueViewer != null)
{
playerDialogueViewer.ClosePanel();
}
// 可扩展隐藏UI、恢复输入等
UIManager.Instance.CloseUI<PlayerDialogueViewer>();
}
}
@@ -174,6 +174,7 @@ namespace Script.Gameplay.Player
var input = inputManager.Input;
input.Player.Read.performed -= OnReadOnperformed;
input.Player.ShowNextDialogue.performed -= OnShowNextDialogueOnperformed;
input.Player.Escape.performed -= OnEscapeOnperformed;
}
}
}

View File

@@ -50,6 +50,7 @@ namespace Script.Gameplay.Player
{
inputManager = InputManager.Instance;
inputManager.Input.Player.Edit.performed += OnEditOnperformed;
inputManager.Input.Player.Escape.performed += OnEscapeOnperformed;
timePauseManager = TimePauseManager.Instance;
if (raycaster == null)
raycaster = GetComponent<FirstPersonRaycaster>() ?? GetComponentInChildren<FirstPersonRaycaster>();
@@ -62,7 +63,19 @@ namespace Script.Gameplay.Player
private void OnEditOnperformed(InputAction.CallbackContext context)
{
EditTarget();
if (!isEditing)
{
BeginEdit();
}
else
{
EndEdit();
}
}
private void OnEscapeOnperformed(InputAction.CallbackContext context)
{
EndEdit();
}
void Update()
@@ -85,34 +98,28 @@ namespace Script.Gameplay.Player
}
CurrentTarget = hitEditable;
}
private void EditTarget()
private void BeginEdit()
{
if (isEditing)
if (CurrentTarget == null) return;
if (!IsEnableEditing) return;
if(isEditing) return;
isEditing = true;
OnBeginEditTarget?.Invoke(CurrentTarget);
if (timePauseManager != null)
{
isEditing = false;
OnEndEditTarget?.Invoke(CurrentTarget);
inputManager.SetCursorState(false, CursorLockMode.Locked);
inputManager.SetInputForLook(true);
inputManager.SetInputForMove(true);
if (timePauseManager != null)
{
timePauseManager.SetPaused(false);
}
timePauseManager.SetPaused(true);
}
else
}
private void EndEdit()
{
if (!isEditing) return;
isEditing = false;
OnEndEditTarget?.Invoke(CurrentTarget);
if (timePauseManager != null)
{
if (CurrentTarget == null) return;
if (!IsEnableEditing) return;
isEditing = true;
OnBeginEditTarget?.Invoke(CurrentTarget);
inputManager.SetCursorState(true, CursorLockMode.Confined);
inputManager.SetInputForLook(false);
inputManager.SetInputForMove(false);
if (timePauseManager != null)
{
timePauseManager.SetPaused(true);
}
timePauseManager.SetPaused(false);
}
}
@@ -121,6 +128,7 @@ namespace Script.Gameplay.Player
if (inputManager != null)
{
inputManager.Input.Player.Edit.performed -= OnEditOnperformed;
inputManager.Input.Player.Escape.performed -= OnEscapeOnperformed;
}
ControllerLocator.Instance.Unregister(this);
}

View File

@@ -24,7 +24,7 @@ namespace Script.Gameplay.Player
private CharacterController characterController;
private Vector3 velocity;
private bool isGrounded;
private IInputManager inputManager;
private InputManager inputManager;
private float lastGroundY;
private bool wasGroundedLastFrame;

View File

@@ -12,7 +12,7 @@ namespace UI
public TMP_Text countdownText;
private GameCountdownManager _countdownManager;
private void Start()
protected override void Start()
{
_countdownManager = GameCountdownManager.Instance;
if (_countdownManager != null)

View File

@@ -13,7 +13,6 @@ namespace UI
{
[SerializeField] private TMP_Text dialogueText;
[SerializeField] private GameObject panel;
private bool isShowingPanel = false;
private PlayerDialogueController playerDialogueController;
private InputManager inputManager;
@@ -29,23 +28,12 @@ namespace UI
playerDialogueController = controller;
playerDialogueController.RegisterDialogueViewer(this);
}
public void ReceiveDialogue(string content)
{
if(!isShowingPanel) OpenPanel();
UIManager.Instance.OpenUI<PlayerDialogueViewer>();
dialogueText.text = content;
}
private void OpenPanel()
{
isShowingPanel = true;
Show();
}
public void ClosePanel()
{
isShowingPanel = false;
Hide();
}
}
}

View File

@@ -31,7 +31,7 @@ namespace UI
public void OnBeginEdit(GameObject target)
{
this.gameObject.SetActive(true);
UIManager.Instance.OpenUI<PlayerEditViewer>();
if (target == null)
{
if (noEditableTargetPanel != null)
@@ -46,8 +46,7 @@ namespace UI
public void OnEndEdit(GameObject target)
{
this.gameObject.SetActive(false);
UIManager.Instance.CloseUI<PlayerEditViewer>();
ClearComponentUI();
if (noEditableTargetPanel != null)
{

View File

@@ -12,7 +12,7 @@ namespace UI
private PlayerController _playerController;
public TMP_Text hpText;
private void Awake()
protected override void Awake()
{
ControllerLocator.Instance.TryGetWait<PlayerController>(OnGet);
}

View File

@@ -12,7 +12,7 @@ namespace UI
private PlayerWatchModeController watchModeController;
[SerializeField] private TMP_Text modeText;
private void Awake()
protected override void Awake()
{
ControllerLocator.Instance.TryGetWait<PlayerWatchModeController>(OnGet);
}

View File

@@ -16,14 +16,20 @@ namespace UI
[SerializeField] private Button reloadButton;
private InputManager inputManager;
protected override void Awake()
{
base.Awake();
inputManager = InputManager.Instance;
inputManager.Input.Player.Setting.performed += RegisterInput;
reloadButton.onClick.AddListener(OnReloadButtonClicked);
}
protected override void Start()
{
base.Start();
inputManager = InputManager.Instance;
inputManager.Input.Player.Setting.performed += (ctx) => { UIManager.Instance.SwitchUI<SettingViewer>(); };
}
private void OnReloadButtonClicked()
{
GameManager.Instance.ReStartGame();
@@ -32,7 +38,7 @@ namespace UI
inputManager.SetInputForLook(true);
inputManager.SetInputForMove(true);
}
private void RegisterInput(InputAction.CallbackContext ctx)
{
if (!isActiveAndEnabled)