-
Notifications
You must be signed in to change notification settings - Fork 333
Expand file tree
/
Copy pathProjectWideActionsExample.cs
More file actions
124 lines (111 loc) · 3.89 KB
/
ProjectWideActionsExample.cs
File metadata and controls
124 lines (111 loc) · 3.89 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
namespace UnityEngine.InputSystem.Samples.ProjectWideActions
{
public class ProjectWideActionsExample : MonoBehaviour
{
[SerializeField] public GameObject cube;
InputAction move;
InputAction look;
InputAction attack;
InputAction jump;
InputAction interact;
InputAction next;
InputAction previous;
InputAction sprint;
InputAction crouch;
InputAction b;
InputAction shiftB;
// Start is called before the first frame update
void Start()
{
// Project-Wide Actions
if (InputSystem.actions)
{
move = InputSystem.actions.FindAction("Player/Move");
look = InputSystem.actions.FindAction("Player/Look");
attack = InputSystem.actions.FindAction("Player/Attack");
jump = InputSystem.actions.FindAction("Player/Jump");
interact = InputSystem.actions.FindAction("Player/Interact");
next = InputSystem.actions.FindAction("Player/Next");
previous = InputSystem.actions.FindAction("Player/Previous");
sprint = InputSystem.actions.FindAction("Player/Sprint");
crouch = InputSystem.actions.FindAction("Player/Crouch");
b = InputSystem.actions.FindAction("Player/B");
shiftB = InputSystem.actions.FindAction("Player/Shift B");
}
else
{
Debug.Log("Setup Project Wide Input Actions in the Player Settings, Input System section");
}
// Handle input by responding to callbacks
if (attack != null)
{
attack.performed += OnAttack;
attack.canceled += OnCancel;
}
if (b != null)
{
b.performed += OnB;
b.canceled += OnCancel;
}
if (shiftB != null)
{
shiftB.performed += OnShiftB;
shiftB.canceled += OnCancel;
}
}
private void OnAttack(InputAction.CallbackContext ctx)
{
cube.GetComponent<Renderer>().material.color = Color.red;
}
private void OnCancel(InputAction.CallbackContext ctx)
{
cube.GetComponent<Renderer>().material.color = Color.green;
}
private void OnB(InputAction.CallbackContext ctx)
{
Debug.Log("B WAS PRESSED");
cube.GetComponent<Renderer>().material.color = Color.yellow;
}
private void OnShiftB(InputAction.CallbackContext ctx)
{
Debug.Log("SHIFT + B WAS PRESSED");
cube.GetComponent<Renderer>().material.color = Color.blue;
}
void OnDestroy()
{
if (attack != null)
{
attack.performed -= OnAttack;
attack.canceled -= OnCancel;
}
if (b != null)
{
b.performed -= OnB;
b.canceled -= OnCancel;
}
if (shiftB != null)
{
shiftB.performed -= OnShiftB;
shiftB.canceled -= OnCancel;
}
}
// Update is called once per frame
void Update()
{
// Handle input by polling each frame
if (move != null)
{
var moveVal = move.ReadValue<Vector2>() * 10.0f * Time.deltaTime;
cube.transform.Translate(new Vector3(moveVal.x, moveVal.y, 0));
}
if (shiftB.IsPressed())
{
Debug.Log("SHIFT + B WAS PRESSED");
}
if (b.IsPressed())
{
Debug.Log("B WAS PRESSED");
}
}
} // class ProjectWideActionsExample
} // namespace UnityEngine.InputSystem.Samples.ProjectWideActions