InputM.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.EventSystems;
  5. using UnityEngine.Tilemaps;
  6. public class InputM : MonoBehaviour {
  7. public void Update() {
  8. // 检测键盘按下
  9. foreach (KeyCode keyCode in keyboardEvent.Keys) {
  10. if (Input.GetKeyUp(keyCode)) TriggerKeyboardEvent(keyCode);
  11. }
  12. // 检测鼠标按下
  13. int? mouseButton = null;
  14. if (Input.GetMouseButtonUp(0)) mouseButton = 0;
  15. if (Input.GetMouseButtonUp(1)) mouseButton = 1;
  16. if (mouseButton == null) return;
  17. }
  18. // 键盘按键绑定
  19. public delegate void KeyboardEventDelegate();
  20. private static Dictionary<KeyCode, KeyboardEventDelegate> keyboardEvent;
  21. public void Awake() {
  22. keyboardEvent = new();
  23. }
  24. public static void AddKeyboardEvent(KeyCode key, KeyboardEventDelegate onKeyClicked) {
  25. Debug.Log("添加按键绑定:" + key.ToString());
  26. keyboardEvent[key] = onKeyClicked;
  27. }
  28. public void TriggerKeyboardEvent(KeyCode key) {
  29. Debug.Log("触发按键:" + key.ToString());
  30. keyboardEvent[key].Invoke();
  31. }
  32. }