-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathRebindUIGameManager.cs
More file actions
131 lines (111 loc) · 4.62 KB
/
RebindUIGameManager.cs
File metadata and controls
131 lines (111 loc) · 4.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using UnityEngine.EventSystems;
using UnityEngine.Pool;
namespace UnityEngine.InputSystem.Samples.RebindUI
{
/// <summary>
/// Simple game manager that manages enabling/disabling in-game and UI actions.
/// </summary>
/// <remarks>State transitions happens per frame and hence handles throttling.</remarks>
public class RebindUIGameManager : MonoBehaviour
{
[Tooltip("The in-game menu object to be activated and deactivated when menu is toggled (Required).")]
public GameObject menu;
[Tooltip("The actions asset that holds Gameplay, Common and UI action maps to be used. (Required).")]
public InputActionAsset actions;
[Tooltip("Whether UI actions should be disabled during gameplay.")]
public bool enableUIActionsDuringGameplay = true;
[Tooltip("The gameplay manager responsible for managing gameplay.")]
public GameplayManager gameplayManager;
[Tooltip("The gameplay UI")]
public GameObject gameUI;
private GameState m_CurrentState = GameState.Initializing;
private GameState m_NextState = GameState.Playing;
private InputActionMap gameplayActions;
private InputActionMap uiActions;
private InputAction toggleMenuAction;
private void Awake()
{
gameplayActions = actions.FindActionMap("Gameplay");
uiActions = actions.FindActionMap("UI");
toggleMenuAction = actions.FindAction("Common/Menu");
}
/// <summary>
/// Toggles between game state and rebinding menu state.
/// </summary>
public void ToggleMenu()
{
switch (m_CurrentState)
{
case GameState.Playing:
m_NextState = GameState.RebindingMenu;
break;
case GameState.RebindingMenu:
// Only allow transition back to the game if game menu is interactable.
// This is to avoid e.g. pressing menu toggle action while in rebind mode.
// Essentially this is equivalent "if NOT currently rebinding".
if (menu.GetComponent<CanvasGroup>().interactable)
m_NextState = GameState.Playing;
break;
}
}
private enum GameState
{
Initializing,
Playing,
RebindingMenu
}
private void OnToggleMenu(InputAction.CallbackContext obj)
{
ToggleMenu();
}
private void OnEnable()
{
toggleMenuAction.performed += OnToggleMenu;
toggleMenuAction.Enable();
}
private void OnDisable()
{
toggleMenuAction.performed -= OnToggleMenu;
toggleMenuAction.Disable();
}
private void Update()
{
// Abort if there is no change to state
if (m_CurrentState == m_NextState)
return;
// Update current state
m_CurrentState = m_NextState;
// Handle state transition
switch (m_NextState)
{
// Entering game mode: enable in-game actions, show menu
case GameState.Playing:
gameplayActions.Enable();
gameplayManager.enabled = true;
if (enableUIActionsDuringGameplay)
uiActions.Enable();
else
uiActions.Disable();
gameUI.SetActive(true);
menu.SetActive(false);
break;
// Entering menu: disable in-game actions, hide menu, make sure we have selection.
// Also make sure or toggle menu action is enabled in case its part of gameplay actions.
case GameState.RebindingMenu:
gameplayActions.Disable();
gameplayManager.enabled = false;
if (!enableUIActionsDuringGameplay)
uiActions.Enable();
gameUI.SetActive(false);
menu.SetActive(true);
// Workaround: Make sure we always have a select game object since Unity UI might otherwise show
// without a selection which might prevent gamepad navigation.
var eventSystem = EventSystem.current;
if (eventSystem.currentSelectedGameObject == null)
eventSystem.SetSelectedGameObject(eventSystem.firstSelectedGameObject);
break;
}
}
}
}