_InputKeyBoard.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. [AddComponentMenu("1Enwer/Input/_KeyBoard")]
  6. public class _InputKeyBoard : MonoBehaviour
  7. {
  8. public PressType pressType = PressType.GetKeyDown;
  9. public KeyCodeEvent[] keyBoardGetKey;
  10. public enum PressType
  11. {
  12. GetKey,
  13. GetKeyDown,
  14. GetKeyUp
  15. }
  16. [System.Serializable]
  17. public class KeyCodeEvent
  18. {
  19. public KeyCode keyCode;
  20. public UnityEvent function;
  21. }
  22. // Update is called once per frame
  23. void Update()
  24. {
  25. for (int i = 0; i < keyBoardGetKey.Length; i++)
  26. {
  27. if (KeyBoardIsPressed(pressType, keyBoardGetKey[i].keyCode))
  28. {
  29. keyBoardGetKey[i].function.Invoke();
  30. }
  31. }
  32. }
  33. public bool KeyBoardIsPressed(PressType _pressType, KeyCode keycode)
  34. {
  35. switch (_pressType)
  36. {
  37. case PressType.GetKey:
  38. return Input.GetKey(keycode);
  39. case PressType.GetKeyDown:
  40. return Input.GetKeyDown(keycode);
  41. case PressType.GetKeyUp:
  42. return Input.GetKeyUp(keycode);
  43. default:
  44. return false;
  45. }
  46. }
  47. }