Geen omschrijving
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectWideActionsExample.cs 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #if UNITY_INPUT_SYSTEM_PROJECT_WIDE_ACTIONS
  2. namespace UnityEngine.InputSystem.Samples.ProjectWideActions
  3. {
  4. public class ProjectWideActionsExample : MonoBehaviour
  5. {
  6. [SerializeField] public GameObject cube;
  7. InputAction move;
  8. InputAction look;
  9. InputAction attack;
  10. InputAction jump;
  11. InputAction interact;
  12. InputAction next;
  13. InputAction previous;
  14. InputAction sprint;
  15. InputAction crouch;
  16. // Start is called before the first frame update
  17. void Start()
  18. {
  19. // Project-Wide Actions
  20. if (InputSystem.actions)
  21. {
  22. move = InputSystem.actions.FindAction("Player/Move");
  23. look = InputSystem.actions.FindAction("Player/Look");
  24. attack = InputSystem.actions.FindAction("Player/Attack");
  25. jump = InputSystem.actions.FindAction("Player/Jump");
  26. interact = InputSystem.actions.FindAction("Player/Interact");
  27. next = InputSystem.actions.FindAction("Player/Next");
  28. previous = InputSystem.actions.FindAction("Player/Previous");
  29. sprint = InputSystem.actions.FindAction("Player/Sprint");
  30. crouch = InputSystem.actions.FindAction("Player/Crouch");
  31. }
  32. else
  33. {
  34. Debug.Log("Setup Project Wide Input Actions in the Player Settings, Input System section");
  35. }
  36. // Handle input by responding to callbacks
  37. if (attack != null)
  38. {
  39. attack.performed += OnAttack;
  40. attack.canceled += OnCancel;
  41. }
  42. }
  43. private void OnAttack(InputAction.CallbackContext ctx)
  44. {
  45. cube.GetComponent<Renderer>().material.color = Color.red;
  46. }
  47. private void OnCancel(InputAction.CallbackContext ctx)
  48. {
  49. cube.GetComponent<Renderer>().material.color = Color.green;
  50. }
  51. void OnDestroy()
  52. {
  53. if (attack != null)
  54. {
  55. attack.performed -= OnAttack;
  56. attack.canceled -= OnCancel;
  57. }
  58. }
  59. // Update is called once per frame
  60. void Update()
  61. {
  62. // Handle input by polling each frame
  63. if (move != null)
  64. {
  65. var moveVal = move.ReadValue<Vector2>() * 10.0f * Time.deltaTime;
  66. cube.transform.Translate(new Vector3(moveVal.x, moveVal.y, 0));
  67. }
  68. }
  69. } // class ProjectWideActionsExample
  70. } // namespace UnityEngine.InputSystem.Samples.ProjectWideActions
  71. #endif