Skip to content

Commit d829d1c

Browse files
committed
Add ExitApplication and PauseApplication scripts
1 parent 7ee277d commit d829d1c

File tree

6 files changed

+257
-2
lines changed

6 files changed

+257
-2
lines changed

Runtime/ExitApplication.cs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
using UnityEngine;
2+
#if ENABLE_INPUT_SYSTEM
3+
using UnityEngine.InputSystem;
4+
#endif
5+
6+
namespace Zigurous.Debug
7+
{
8+
/// <summary>
9+
/// Quits the application via user input.
10+
/// </summary>
11+
[AddComponentMenu("Zigurous/Debug/Exit Application")]
12+
[HelpURL("https://docs.zigurous.com/com.zigurous.debug/api/Zigurous.Debug/ExitApplication")]
13+
public sealed class ExitApplication : MonoBehaviour
14+
{
15+
#if ENABLE_INPUT_SYSTEM
16+
/// <summary>
17+
/// The input action that quits the application.
18+
/// </summary>
19+
[Tooltip("The input action that quits the application.")]
20+
[Header("Input System")]
21+
public InputAction quitInput = new InputAction("ExitApplication", InputActionType.Button, "<Keyboard>/escape");
22+
#endif
23+
24+
#if ENABLE_LEGACY_INPUT_MANAGER
25+
/// <summary>
26+
/// The keycode that quits the application.
27+
/// </summary>
28+
[Tooltip("The keycode that quits the application.")]
29+
[Header("Legacy Input Manager")]
30+
public KeyCode quitKey = KeyCode.Escape;
31+
32+
/// <summary>
33+
/// The optional modifier key to be held down to quit the application.
34+
/// </summary>
35+
[Tooltip("The optional modifier key to be held down to quit the application.")]
36+
public KeyCode quitKeyModifier = KeyCode.None;
37+
#endif
38+
39+
#if ENABLE_INPUT_SYSTEM
40+
private void Awake()
41+
{
42+
quitInput.performed += OnQuit;
43+
}
44+
45+
private void OnDestroy()
46+
{
47+
quitInput.Dispose();
48+
}
49+
50+
private void OnEnable()
51+
{
52+
quitInput.Enable();
53+
}
54+
55+
private void OnDisable()
56+
{
57+
quitInput.Disable();
58+
}
59+
60+
private void OnQuit(InputAction.CallbackContext context)
61+
{
62+
if (context.performed) {
63+
Quit();
64+
}
65+
}
66+
#endif
67+
68+
#if ENABLE_LEGACY_INPUT_MANAGER
69+
private void Update()
70+
{
71+
if (Input.GetKeyDown(quitKey))
72+
{
73+
if (quitKeyModifier == KeyCode.None || Input.GetKey(quitKeyModifier)) {
74+
Quit();
75+
}
76+
}
77+
}
78+
#endif
79+
80+
/// <summary>
81+
/// Quits the application, or exits playmode if running in the editor.
82+
/// </summary>
83+
public void Quit()
84+
{
85+
#if UNITY_EDITOR
86+
if (Application.isEditor)
87+
{
88+
UnityEditor.EditorApplication.ExitPlaymode();
89+
return;
90+
}
91+
#endif
92+
93+
Application.Quit();
94+
}
95+
96+
}
97+
98+
}

Runtime/ExitApplication.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/PauseApplication.cs

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#if UNITY_EDITOR
2+
using UnityEditor;
3+
#endif
4+
using UnityEngine;
5+
#if ENABLE_INPUT_SYSTEM
6+
using UnityEngine.InputSystem;
7+
#endif
8+
9+
namespace Zigurous.Debug
10+
{
11+
/// <summary>
12+
/// Pauses the application via user input.
13+
/// </summary>
14+
[AddComponentMenu("Zigurous/Debug/Pause Application")]
15+
[HelpURL("https://docs.zigurous.com/com.zigurous.debug/api/Zigurous.Debug/PauseApplication")]
16+
public sealed class PauseApplication : MonoBehaviour
17+
{
18+
#if ENABLE_INPUT_SYSTEM
19+
/// <summary>
20+
/// The input action that pauses the application.
21+
/// </summary>
22+
[Tooltip("The input action that pauses the application.")]
23+
[Header("Input System")]
24+
public InputAction pauseInput = new InputAction("PauseApplication", InputActionType.Button, "<Keyboard>/pause");
25+
#endif
26+
27+
#if ENABLE_LEGACY_INPUT_MANAGER
28+
/// <summary>
29+
/// The keycode that pauses the application.
30+
/// </summary>
31+
[Tooltip("The keycode that pauses the application.")]
32+
[Header("Legacy Input Manager")]
33+
public KeyCode pauseKey = KeyCode.Break;
34+
35+
/// <summary>
36+
/// The optional modifier key to be held down to pause the application.
37+
/// </summary>
38+
[Tooltip("The optional modifier key to be held down to pause the application.")]
39+
public KeyCode pauseKeyModifier = KeyCode.None;
40+
#endif
41+
42+
/// <summary>
43+
/// Whether the application is currently paused or not (Read only).
44+
/// </summary>
45+
#if UNITY_EDITOR
46+
public bool paused => EditorApplication.isPaused;
47+
#else
48+
public bool paused { get; private set; }
49+
#endif
50+
51+
#if ENABLE_INPUT_SYSTEM
52+
private void Awake()
53+
{
54+
pauseInput.performed += OnPause;
55+
}
56+
57+
private void OnDestroy()
58+
{
59+
pauseInput.Dispose();
60+
}
61+
62+
private void OnEnable()
63+
{
64+
pauseInput.Enable();
65+
}
66+
67+
private void OnDisable()
68+
{
69+
pauseInput.Disable();
70+
}
71+
72+
private void OnPause(InputAction.CallbackContext context)
73+
{
74+
if (context.performed)
75+
{
76+
if (paused) {
77+
Resume();
78+
} else {
79+
Pause();
80+
}
81+
}
82+
}
83+
#endif
84+
85+
#if ENABLE_LEGACY_INPUT_MANAGER
86+
private void Update()
87+
{
88+
if (Input.GetKeyDown(pauseKey))
89+
{
90+
if (pauseKeyModifier == KeyCode.None || Input.GetKey(pauseKeyModifier))
91+
{
92+
if (paused) {
93+
Resume();
94+
} else {
95+
Pause();
96+
}
97+
}
98+
}
99+
}
100+
#endif
101+
102+
/// <summary>
103+
/// Pauses the application.
104+
/// </summary>
105+
public void Pause()
106+
{
107+
#if UNITY_EDITOR
108+
EditorApplication.isPaused = true;
109+
#else
110+
paused = true;
111+
Time.timeScale = 0f;
112+
#endif
113+
}
114+
115+
/// <summary>
116+
/// Resumes the application.
117+
/// </summary>
118+
public void Resume()
119+
{
120+
#if UNITY_EDITOR
121+
EditorApplication.isPaused = false;
122+
#else
123+
paused = false;
124+
Time.timeScale = 1f;
125+
#endif
126+
}
127+
128+
}
129+
130+
}

Runtime/PauseApplication.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Runtime/Zigurous.Debug.asmdef

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"name": "Zigurous.Debug",
3-
"references": [],
3+
"references": [
4+
"GUID:75469ad4d38634e559750d17036d5f7c"
5+
],
46
"includePlatforms": [],
57
"excludePlatforms": [],
68
"allowUnsafeCode": false,
@@ -10,4 +12,4 @@
1012
"defineConstraints": [],
1113
"versionDefines": [],
1214
"noEngineReferences": false
13-
}
15+
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
"documentationUrl": "https://docs.zigurous.com/com.zigurous.debug",
99
"changelogUrl": "https://docs.zigurous.com/com.zigurous.debug/changelog",
1010
"licensesUrl": "https://docs.zigurous.com/com.zigurous.debug/license",
11+
"dependencies": {
12+
"com.unity.inputsystem": "1.2.0"
13+
},
1114
"keywords": [
1215
"debug",
1316
"debugging",

0 commit comments

Comments
 (0)