2
0

UIExtensionsInputManager.cs 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /// Credit SimonDarksideJ
  2. /// Sourced from: https://bitbucket.org/UnityUIExtensions/unity-ui-extensions/issues/348/menu-manager-does-not-work-with-the-new
  3. #if UNITY_2019_1_OR_NEWER && !ENABLE_LEGACY_INPUT_MANAGER
  4. #define NEW_INPUT_SYSTEM
  5. #endif
  6. using System;
  7. using System.Collections.Generic;
  8. #if NEW_INPUT_SYSTEM
  9. using UnityEngine.InputSystem;
  10. using UnityEngine.InputSystem.Controls;
  11. #endif
  12. namespace UnityEngine.UI.Extensions
  13. {
  14. public static class UIExtensionsInputManager
  15. {
  16. #if NEW_INPUT_SYSTEM
  17. private static bool[] mouseButtons = new bool[3] { false, false, false };
  18. private static Dictionary<KeyCode, bool> keys = new Dictionary<KeyCode, bool>();
  19. private static Dictionary<String, bool> buttons = new Dictionary<String, bool>();
  20. #endif
  21. public static bool GetMouseButton(int button)
  22. {
  23. #if NEW_INPUT_SYSTEM
  24. if (Mouse.current == null)
  25. {
  26. return false;
  27. }
  28. return Mouse.current.leftButton.isPressed;
  29. #else
  30. return Input.GetMouseButton(button);
  31. #endif
  32. }
  33. public static bool GetMouseButtonDown(int button)
  34. {
  35. #if NEW_INPUT_SYSTEM
  36. if (Mouse.current == null)
  37. {
  38. return false;
  39. }
  40. if (Mouse.current.leftButton.isPressed)
  41. {
  42. if (!mouseButtons[button])
  43. {
  44. mouseButtons[button] = true;
  45. return true;
  46. }
  47. }
  48. return false;
  49. #else
  50. return Input.GetMouseButtonDown(button);
  51. #endif
  52. }
  53. public static bool GetMouseButtonUp(int button)
  54. {
  55. #if NEW_INPUT_SYSTEM
  56. if (Mouse.current == null)
  57. {
  58. return false;
  59. }
  60. if (mouseButtons[button] && !Mouse.current.leftButton.isPressed)
  61. {
  62. mouseButtons[button] = false;
  63. return true;
  64. }
  65. return false;
  66. #else
  67. return Input.GetMouseButtonUp(button);
  68. #endif
  69. }
  70. public static bool GetButton(string input)
  71. {
  72. #if NEW_INPUT_SYSTEM
  73. ButtonControl buttonPressed = GetButtonControlFromString(input);
  74. if (!buttons.ContainsKey(input))
  75. {
  76. buttons.Add(input, false);
  77. }
  78. return buttonPressed != null ? buttonPressed.isPressed : false;
  79. #else
  80. return Input.GetButton(input);
  81. #endif
  82. }
  83. #if NEW_INPUT_SYSTEM
  84. private static ButtonControl GetButtonControlFromString(string input)
  85. {
  86. if (Gamepad.current == null)
  87. {
  88. return null;
  89. }
  90. switch (input)
  91. {
  92. case "Submit":
  93. return Gamepad.current.aButton;
  94. case "Cancel":
  95. return Gamepad.current.bButton;
  96. default:
  97. return null;
  98. }
  99. }
  100. #endif
  101. public static bool GetButtonDown(string input)
  102. {
  103. #if NEW_INPUT_SYSTEM
  104. ButtonControl buttonPressed = GetButtonControlFromString(input);
  105. if (buttonPressed.isPressed)
  106. {
  107. if (!buttons.ContainsKey(input))
  108. {
  109. buttons.Add(input, false);
  110. }
  111. if (!buttons[input])
  112. {
  113. buttons[input] = true;
  114. return true;
  115. }
  116. }
  117. else
  118. {
  119. buttons[input] = false;
  120. }
  121. return false;
  122. #else
  123. return Input.GetButtonDown(input);
  124. #endif
  125. }
  126. public static bool GetButtonUp(string input)
  127. {
  128. #if NEW_INPUT_SYSTEM
  129. ButtonControl buttonPressed = GetButtonControlFromString(input);
  130. if (buttons[input] && !buttonPressed.isPressed)
  131. {
  132. buttons[input] = false;
  133. return true;
  134. }
  135. return false;
  136. #else
  137. return Input.GetButtonUp(input);
  138. #endif
  139. }
  140. public static bool GetKey(KeyCode key)
  141. {
  142. #if NEW_INPUT_SYSTEM
  143. KeyControl keyPressed = GetKeyControlFromKeyCode(key);
  144. if (!keys.ContainsKey(key))
  145. {
  146. keys.Add(key, false);
  147. }
  148. return keyPressed != null ? keyPressed.isPressed : false;
  149. #else
  150. return Input.GetKey(key);
  151. #endif
  152. }
  153. #if NEW_INPUT_SYSTEM
  154. private static KeyControl GetKeyControlFromKeyCode(KeyCode key)
  155. {
  156. if (Keyboard.current == null)
  157. {
  158. return null;
  159. }
  160. switch (key)
  161. {
  162. case KeyCode.Escape:
  163. return Keyboard.current.escapeKey;
  164. case KeyCode.KeypadEnter:
  165. return Keyboard.current.numpadEnterKey;
  166. case KeyCode.UpArrow:
  167. return Keyboard.current.upArrowKey;
  168. case KeyCode.DownArrow:
  169. return Keyboard.current.downArrowKey;
  170. case KeyCode.RightArrow:
  171. return Keyboard.current.rightArrowKey;
  172. case KeyCode.LeftArrow:
  173. return Keyboard.current.leftArrowKey;
  174. case KeyCode.LeftShift:
  175. return Keyboard.current.leftShiftKey;
  176. case KeyCode.Tab:
  177. return Keyboard.current.tabKey;
  178. default:
  179. return null;
  180. }
  181. }
  182. #endif
  183. public static bool GetKeyDown(KeyCode key)
  184. {
  185. #if NEW_INPUT_SYSTEM
  186. KeyControl keyPressed = GetKeyControlFromKeyCode(key);
  187. if (keyPressed.isPressed)
  188. {
  189. if (!keys.ContainsKey(key))
  190. {
  191. keys.Add(key, false);
  192. }
  193. if (!keys[key])
  194. {
  195. keys[key] = true;
  196. return true;
  197. }
  198. }
  199. else
  200. {
  201. keys[key] = false;
  202. }
  203. return false;
  204. #else
  205. return Input.GetKeyDown(key);
  206. #endif
  207. }
  208. public static bool GetKeyUp(KeyCode key)
  209. {
  210. #if NEW_INPUT_SYSTEM
  211. KeyControl keyPressed = GetKeyControlFromKeyCode(key);
  212. if (keys[key] && !keyPressed.isPressed)
  213. {
  214. keys[key] = false;
  215. return true;
  216. }
  217. return false;
  218. #else
  219. return Input.GetKeyUp(key);
  220. #endif
  221. }
  222. public static float GetAxisRaw(string axis)
  223. {
  224. #if NEW_INPUT_SYSTEM
  225. if (Gamepad.current == null)
  226. {
  227. return 0f;
  228. }
  229. switch (axis)
  230. {
  231. case "Horizontal":
  232. return Gamepad.current.leftStick.x.ReadValue();
  233. case "Vertical":
  234. return Gamepad.current.leftStick.y.ReadValue();
  235. }
  236. return 0f;
  237. #else
  238. return Input.GetAxisRaw(axis);
  239. #endif
  240. }
  241. public static Vector3 MousePosition
  242. {
  243. get
  244. {
  245. #if NEW_INPUT_SYSTEM
  246. return Mouse.current.position.ReadValue();
  247. #else
  248. return Input.mousePosition;
  249. #endif
  250. }
  251. }
  252. public static Vector3 MouseScrollDelta
  253. {
  254. get
  255. {
  256. #if NEW_INPUT_SYSTEM
  257. return Mouse.current.position.ReadValue();
  258. #else
  259. return Input.mouseScrollDelta;
  260. #endif
  261. }
  262. }
  263. }
  264. }