|
| 1 | +#if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS |
| 2 | + |
| 3 | +namespace UnityEngine.InputSystem.Samples.ProjectWideActions |
| 4 | +{ |
| 5 | + public class ProjectWideActionsExample : MonoBehaviour |
| 6 | + { |
| 7 | + [SerializeField] public GameObject cube; |
| 8 | + |
| 9 | + InputAction move; |
| 10 | + InputAction look; |
| 11 | + InputAction attack; |
| 12 | + InputAction jump; |
| 13 | + InputAction interact; |
| 14 | + InputAction next; |
| 15 | + InputAction previous; |
| 16 | + InputAction sprint; |
| 17 | + InputAction crouch; |
| 18 | + |
| 19 | + // Start is called before the first frame update |
| 20 | + void Start() |
| 21 | + { |
| 22 | + // Project-Wide Actions |
| 23 | + move = InputSystem.actions.FindAction("Player/Move"); |
| 24 | + look = InputSystem.actions.FindAction("Player/Look"); |
| 25 | + attack = InputSystem.actions.FindAction("Player/Attack"); |
| 26 | + jump = InputSystem.actions.FindAction("Player/Jump"); |
| 27 | + interact = InputSystem.actions.FindAction("Player/Interact"); |
| 28 | + next = InputSystem.actions.FindAction("Player/Next"); |
| 29 | + previous = InputSystem.actions.FindAction("Player/Previous"); |
| 30 | + sprint = InputSystem.actions.FindAction("Player/Sprint"); |
| 31 | + crouch = InputSystem.actions.FindAction("Player/Crouch"); |
| 32 | + |
| 33 | + // Handle input by responding to callbacks |
| 34 | + attack.performed += ctx => cube.GetComponent<Renderer>().material.color = Color.red; |
| 35 | + attack.canceled += ctx => cube.GetComponent<Renderer>().material.color = Color.green; |
| 36 | + } |
| 37 | + |
| 38 | + // Update is called once per frame |
| 39 | + void Update() |
| 40 | + { |
| 41 | + // Handle input by polling each frame |
| 42 | + var moveVal = move.ReadValue<Vector2>() * 10.0f * Time.deltaTime; |
| 43 | + cube.transform.Translate(new Vector3(moveVal.x, moveVal.y, 0)); |
| 44 | + } |
| 45 | + } // class ProjectWideActionsExample |
| 46 | +} // namespace UnityEngine.InputSystem.Samples.ProjectWideActions |
| 47 | + |
| 48 | +#endif |
0 commit comments