-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInputActionProcessor.cs
More file actions
108 lines (86 loc) · 2.86 KB
/
InputActionProcessor.cs
File metadata and controls
108 lines (86 loc) · 2.86 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
// Copyright 2025 U.S. Federal Government (in countries where recognized)
// Copyright 2025 Dakota Crouchelli dakota.h.crouchelli.civ@us.navy.mil
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.InputSystem;
namespace CREATIVE.Utility
{
/**
An enum only used for the ActionStage field.
*/
public enum InputActionStage
{
Started,
Performed,
Cancelled
}
/**
This component listens for a particular Input Action from the
project-wide Input Actions and links it to a Unity Event callback.
*/
public class InputActionProcessor : MonoBehaviour
{
/**
A reference to the InputAction that should be listened for.
*/
[field: SerializeField]
private InputActionReference Action;
private InputActionReference registeredAction;
/**
Which stage of the InputAction should be listened for.
Performed is the most commonly used stage. It happens once when a
button is pressed.
Started and Cancelled can be used to detect when a button starts and
stops being held down.
*/
[field: SerializeField]
private InputActionStage ActionStage = InputActionStage.Performed;
private InputActionStage registeredActionStage;
/**
The UnityEvent that is invoked by the InputAction
*/
[field: SerializeField]
private UnityEvent Callback;
private UnityEvent registeredCallback;
private bool registered = false;
void Start() => ReRegister();
void OnValidate() => ReRegister();
void OnEnable() => ReRegister();
void OnDisable() => UnRegister();
void OnDestroy() => UnRegister();
private void ReRegister()
{
UnRegister();
registeredAction = Action;
registeredActionStage = ActionStage;
registeredCallback = Callback;
if (Application.isPlaying && isActiveAndEnabled && registeredAction!=null && registeredCallback!=null)
{
registeredActionStage = ActionStage;
if (registeredActionStage == InputActionStage.Started)
registeredAction.action.started += Invoke;
if (registeredActionStage == InputActionStage.Performed)
registeredAction.action.performed += Invoke;
if (registeredActionStage == InputActionStage.Cancelled)
registeredAction.action.canceled += Invoke;
registered = true;
}
}
private void UnRegister()
{
if (registered)
{
if (registeredActionStage == InputActionStage.Started)
registeredAction.action.started -= Invoke;
if (registeredActionStage == InputActionStage.Performed)
registeredAction.action.performed -= Invoke;
if (registeredActionStage == InputActionStage.Cancelled)
registeredAction.action.canceled -= Invoke;
registered = false;
}
}
private void Invoke(InputAction.CallbackContext context) => registeredCallback.Invoke();
}
}