Skip to content

Commit 86598f6

Browse files
authored
Add Pause button as a reference point for C# events usage (#29)
* Add initial example for pause action * Be able to pause and unpause the game in a dummy implementation * Get rid of notes
1 parent 9ae5c20 commit 86598f6

7 files changed

+223
-3
lines changed

Assets/PlayerInputActions.cs

+41
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,15 @@ public @PlayerInputActions()
5353
""processors"": """",
5454
""interactions"": """",
5555
""initialStateCheck"": false
56+
},
57+
{
58+
""name"": ""Pause"",
59+
""type"": ""Button"",
60+
""id"": ""72a3400b-8f1f-407c-88a9-a2d1c0cb7e1b"",
61+
""expectedControlType"": ""Button"",
62+
""processors"": """",
63+
""interactions"": """",
64+
""initialStateCheck"": false
5665
}
5766
],
5867
""bindings"": [
@@ -253,6 +262,28 @@ public @PlayerInputActions()
253262
""action"": ""LookAround"",
254263
""isComposite"": false,
255264
""isPartOfComposite"": true
265+
},
266+
{
267+
""name"": """",
268+
""id"": ""0bdf96af-90a7-4005-8fd6-35f4417f8fcd"",
269+
""path"": ""<Keyboard>/p"",
270+
""interactions"": """",
271+
""processors"": """",
272+
""groups"": ""KeyboardMouse"",
273+
""action"": ""Pause"",
274+
""isComposite"": false,
275+
""isPartOfComposite"": false
276+
},
277+
{
278+
""name"": """",
279+
""id"": ""dc1dd3c9-e01a-45c6-bd59-2ecf583701b0"",
280+
""path"": ""<Gamepad>/start"",
281+
""interactions"": """",
282+
""processors"": """",
283+
""groups"": ""Gamepad"",
284+
""action"": ""Pause"",
285+
""isComposite"": false,
286+
""isPartOfComposite"": false
256287
}
257288
]
258289
}
@@ -292,6 +323,7 @@ public @PlayerInputActions()
292323
m_Player_Move = m_Player.FindAction("Move", throwIfNotFound: true);
293324
m_Player_LookAround = m_Player.FindAction("LookAround", throwIfNotFound: true);
294325
m_Player_Run = m_Player.FindAction("Run", throwIfNotFound: true);
326+
m_Player_Pause = m_Player.FindAction("Pause", throwIfNotFound: true);
295327
}
296328

297329
public void Dispose()
@@ -354,13 +386,15 @@ public int FindBinding(InputBinding bindingMask, out InputAction action)
354386
private readonly InputAction m_Player_Move;
355387
private readonly InputAction m_Player_LookAround;
356388
private readonly InputAction m_Player_Run;
389+
private readonly InputAction m_Player_Pause;
357390
public struct PlayerActions
358391
{
359392
private @PlayerInputActions m_Wrapper;
360393
public PlayerActions(@PlayerInputActions wrapper) { m_Wrapper = wrapper; }
361394
public InputAction @Move => m_Wrapper.m_Player_Move;
362395
public InputAction @LookAround => m_Wrapper.m_Player_LookAround;
363396
public InputAction @Run => m_Wrapper.m_Player_Run;
397+
public InputAction @Pause => m_Wrapper.m_Player_Pause;
364398
public InputActionMap Get() { return m_Wrapper.m_Player; }
365399
public void Enable() { Get().Enable(); }
366400
public void Disable() { Get().Disable(); }
@@ -379,6 +413,9 @@ public void SetCallbacks(IPlayerActions instance)
379413
@Run.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnRun;
380414
@Run.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnRun;
381415
@Run.canceled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnRun;
416+
@Pause.started -= m_Wrapper.m_PlayerActionsCallbackInterface.OnPause;
417+
@Pause.performed -= m_Wrapper.m_PlayerActionsCallbackInterface.OnPause;
418+
@Pause.canceled -= m_Wrapper.m_PlayerActionsCallbackInterface.OnPause;
382419
}
383420
m_Wrapper.m_PlayerActionsCallbackInterface = instance;
384421
if (instance != null)
@@ -392,6 +429,9 @@ public void SetCallbacks(IPlayerActions instance)
392429
@Run.started += instance.OnRun;
393430
@Run.performed += instance.OnRun;
394431
@Run.canceled += instance.OnRun;
432+
@Pause.started += instance.OnPause;
433+
@Pause.performed += instance.OnPause;
434+
@Pause.canceled += instance.OnPause;
395435
}
396436
}
397437
}
@@ -419,5 +459,6 @@ public interface IPlayerActions
419459
void OnMove(InputAction.CallbackContext context);
420460
void OnLookAround(InputAction.CallbackContext context);
421461
void OnRun(InputAction.CallbackContext context);
462+
void OnPause(InputAction.CallbackContext context);
422463
}
423464
}

Assets/PlayerInputActions.inputactions

+31
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
"processors": "",
3232
"interactions": "",
3333
"initialStateCheck": false
34+
},
35+
{
36+
"name": "Pause",
37+
"type": "Button",
38+
"id": "72a3400b-8f1f-407c-88a9-a2d1c0cb7e1b",
39+
"expectedControlType": "Button",
40+
"processors": "",
41+
"interactions": "",
42+
"initialStateCheck": false
3443
}
3544
],
3645
"bindings": [
@@ -231,6 +240,28 @@
231240
"action": "LookAround",
232241
"isComposite": false,
233242
"isPartOfComposite": true
243+
},
244+
{
245+
"name": "",
246+
"id": "0bdf96af-90a7-4005-8fd6-35f4417f8fcd",
247+
"path": "<Keyboard>/p",
248+
"interactions": "",
249+
"processors": "",
250+
"groups": "KeyboardMouse",
251+
"action": "Pause",
252+
"isComposite": false,
253+
"isPartOfComposite": false
254+
},
255+
{
256+
"name": "",
257+
"id": "dc1dd3c9-e01a-45c6-bd59-2ecf583701b0",
258+
"path": "<Gamepad>/start",
259+
"interactions": "",
260+
"processors": "",
261+
"groups": "Gamepad",
262+
"action": "Pause",
263+
"isComposite": false,
264+
"isPartOfComposite": false
234265
}
235266
]
236267
}

Assets/Scenes/Game.unity

+91
Original file line numberDiff line numberDiff line change
@@ -695,9 +695,56 @@ Transform:
695695
m_Children:
696696
- {fileID: 1149604753}
697697
- {fileID: 297956313}
698+
- {fileID: 1129948433}
699+
- {fileID: 913615976}
698700
m_Father: {fileID: 0}
699701
m_RootOrder: 2
700702
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
703+
--- !u!1 &913615975
704+
GameObject:
705+
m_ObjectHideFlags: 0
706+
m_CorrespondingSourceObject: {fileID: 0}
707+
m_PrefabInstance: {fileID: 0}
708+
m_PrefabAsset: {fileID: 0}
709+
serializedVersion: 6
710+
m_Component:
711+
- component: {fileID: 913615976}
712+
- component: {fileID: 913615977}
713+
m_Layer: 0
714+
m_Name: Pause Manager
715+
m_TagString: Untagged
716+
m_Icon: {fileID: 0}
717+
m_NavMeshLayer: 0
718+
m_StaticEditorFlags: 0
719+
m_IsActive: 1
720+
--- !u!4 &913615976
721+
Transform:
722+
m_ObjectHideFlags: 0
723+
m_CorrespondingSourceObject: {fileID: 0}
724+
m_PrefabInstance: {fileID: 0}
725+
m_PrefabAsset: {fileID: 0}
726+
m_GameObject: {fileID: 913615975}
727+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
728+
m_LocalPosition: {x: 0, y: 0, z: 0}
729+
m_LocalScale: {x: 1, y: 1, z: 1}
730+
m_ConstrainProportionsScale: 0
731+
m_Children: []
732+
m_Father: {fileID: 807670057}
733+
m_RootOrder: 3
734+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
735+
--- !u!114 &913615977
736+
MonoBehaviour:
737+
m_ObjectHideFlags: 0
738+
m_CorrespondingSourceObject: {fileID: 0}
739+
m_PrefabInstance: {fileID: 0}
740+
m_PrefabAsset: {fileID: 0}
741+
m_GameObject: {fileID: 913615975}
742+
m_Enabled: 1
743+
m_EditorHideFlags: 0
744+
m_Script: {fileID: 11500000, guid: a896424a9cdda4349bb372d1b269d617, type: 3}
745+
m_Name:
746+
m_EditorClassIdentifier:
747+
gameInput: {fileID: 297956314}
701748
--- !u!1 &996116153
702749
GameObject:
703750
m_ObjectHideFlags: 0
@@ -909,6 +956,50 @@ Transform:
909956
m_Father: {fileID: 0}
910957
m_RootOrder: 0
911958
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
959+
--- !u!1 &1129948432
960+
GameObject:
961+
m_ObjectHideFlags: 0
962+
m_CorrespondingSourceObject: {fileID: 0}
963+
m_PrefabInstance: {fileID: 0}
964+
m_PrefabAsset: {fileID: 0}
965+
serializedVersion: 6
966+
m_Component:
967+
- component: {fileID: 1129948433}
968+
- component: {fileID: 1129948434}
969+
m_Layer: 0
970+
m_Name: CSharp Sandbox
971+
m_TagString: Untagged
972+
m_Icon: {fileID: 0}
973+
m_NavMeshLayer: 0
974+
m_StaticEditorFlags: 0
975+
m_IsActive: 1
976+
--- !u!4 &1129948433
977+
Transform:
978+
m_ObjectHideFlags: 0
979+
m_CorrespondingSourceObject: {fileID: 0}
980+
m_PrefabInstance: {fileID: 0}
981+
m_PrefabAsset: {fileID: 0}
982+
m_GameObject: {fileID: 1129948432}
983+
m_LocalRotation: {x: 0, y: 0, z: 0, w: 1}
984+
m_LocalPosition: {x: 0, y: 0, z: 0}
985+
m_LocalScale: {x: 1, y: 1, z: 1}
986+
m_ConstrainProportionsScale: 0
987+
m_Children: []
988+
m_Father: {fileID: 807670057}
989+
m_RootOrder: 2
990+
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
991+
--- !u!114 &1129948434
992+
MonoBehaviour:
993+
m_ObjectHideFlags: 0
994+
m_CorrespondingSourceObject: {fileID: 0}
995+
m_PrefabInstance: {fileID: 0}
996+
m_PrefabAsset: {fileID: 0}
997+
m_GameObject: {fileID: 1129948432}
998+
m_Enabled: 1
999+
m_EditorHideFlags: 0
1000+
m_Script: {fileID: 11500000, guid: fff7322295d3e4ec6aa9cd1544b63dce, type: 3}
1001+
m_Name:
1002+
m_EditorClassIdentifier:
9121003
--- !u!1 &1149604751
9131004
GameObject:
9141005
m_ObjectHideFlags: 0

Assets/Scripts/GameInput.cs

+18-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,31 @@
11
using UnityEngine;
2+
using System;
3+
using UnityEngine.InputSystem;
24

35
public class GameInput : MonoBehaviour
46
{
57
private PlayerInputActions playerInputActions;
68

9+
public event EventHandler<float> OnPauseAction;
10+
711
private void Awake()
812
{
13+
// Enable the "Player" action map and attach listeners.
914
playerInputActions = new PlayerInputActions();
10-
11-
// Enable the "Player" action map.
1215
playerInputActions.Player.Enable();
16+
playerInputActions.Player.Pause.performed += Pause_performed;
17+
}
18+
19+
private void OnDestroy()
20+
{
21+
// Detach listeners and dispose of held-onto state.
22+
playerInputActions.Player.Pause.performed -= Pause_performed;
23+
playerInputActions.Dispose();
24+
}
25+
26+
private void Pause_performed(InputAction.CallbackContext context)
27+
{
28+
OnPauseAction?.Invoke(this, Time.time);
1329
}
1430

1531
public Vector3 GetMovement()

Assets/Scripts/PauseManager.cs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using UnityEngine;
2+
3+
public class PauseManager : MonoBehaviour
4+
{
5+
[SerializeField]
6+
[NotNull]
7+
GameInput gameInput;
8+
9+
private bool isPaused = false;
10+
11+
private void Start()
12+
{
13+
gameInput.OnPauseAction += GameInput_OnPauseAction;
14+
}
15+
16+
private void OnDestroy()
17+
{
18+
gameInput.OnPauseAction -= GameInput_OnPauseAction;
19+
}
20+
21+
private void GameInput_OnPauseAction(object sender, float timeOfPause)
22+
{
23+
TogglePause();
24+
Debug.Log(isPaused ? "Game is paused." : "Game is active.");
25+
}
26+
27+
private void TogglePause()
28+
{
29+
isPaused = !isPaused;
30+
}
31+
}

Assets/Scripts/PauseManager.cs.meta

+11
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/Scripts/TileUI.cs

-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public void OnPointerUp(PointerEventData eventData)
5252
tileEventManager.ReleaseTile(tileQuantity);
5353
}
5454

55-
5655
void Start()
5756
{
5857
var pos = transform.position;

0 commit comments

Comments
 (0)