暫無描述
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.

DebugManager.Actions.cs 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. #if ENABLE_INPUT_SYSTEM && ENABLE_INPUT_SYSTEM_PACKAGE
  2. #define USE_INPUT_SYSTEM
  3. using UnityEngine.InputSystem;
  4. using UnityEngine.InputSystem.EnhancedTouch;
  5. #endif
  6. using System;
  7. using System.Collections.Generic;
  8. namespace UnityEngine.Rendering
  9. {
  10. internal enum DebugAction
  11. {
  12. EnableDebugMenu,
  13. PreviousDebugPanel,
  14. NextDebugPanel,
  15. Action,
  16. MakePersistent,
  17. MoveVertical,
  18. MoveHorizontal,
  19. Multiplier,
  20. ResetAll,
  21. DebugActionCount
  22. }
  23. enum DebugActionRepeatMode
  24. {
  25. Never,
  26. Delay
  27. }
  28. public sealed partial class DebugManager
  29. {
  30. const string kEnableDebugBtn1 = "Enable Debug Button 1";
  31. const string kEnableDebugBtn2 = "Enable Debug Button 2";
  32. const string kDebugPreviousBtn = "Debug Previous";
  33. const string kDebugNextBtn = "Debug Next";
  34. const string kValidateBtn = "Debug Validate";
  35. const string kPersistentBtn = "Debug Persistent";
  36. const string kDPadVertical = "Debug Vertical";
  37. const string kDPadHorizontal = "Debug Horizontal";
  38. const string kMultiplierBtn = "Debug Multiplier";
  39. const string kResetBtn = "Debug Reset";
  40. const string kEnableDebug = "Enable Debug";
  41. DebugActionDesc[] m_DebugActions;
  42. DebugActionState[] m_DebugActionStates;
  43. #if USE_INPUT_SYSTEM
  44. InputActionMap debugActionMap = new InputActionMap("Debug Menu");
  45. #endif
  46. void RegisterActions()
  47. {
  48. m_DebugActions = new DebugActionDesc[(int)DebugAction.DebugActionCount];
  49. m_DebugActionStates = new DebugActionState[(int)DebugAction.DebugActionCount];
  50. var enableDebugMenu = new DebugActionDesc();
  51. #if USE_INPUT_SYSTEM
  52. enableDebugMenu.buttonAction = debugActionMap.FindAction(kEnableDebug);
  53. #else
  54. enableDebugMenu.buttonTriggerList.Add(new[] { kEnableDebugBtn1, kEnableDebugBtn2 });
  55. enableDebugMenu.keyTriggerList.Add(new[] { KeyCode.LeftControl, KeyCode.Backspace });
  56. #endif
  57. enableDebugMenu.repeatMode = DebugActionRepeatMode.Never;
  58. AddAction(DebugAction.EnableDebugMenu, enableDebugMenu);
  59. var resetDebugMenu = new DebugActionDesc();
  60. #if USE_INPUT_SYSTEM
  61. resetDebugMenu.buttonAction = debugActionMap.FindAction(kResetBtn);
  62. #else
  63. resetDebugMenu.keyTriggerList.Add(new[] { KeyCode.LeftAlt, KeyCode.Backspace });
  64. resetDebugMenu.buttonTriggerList.Add(new[] { kResetBtn, kEnableDebugBtn2 });
  65. #endif
  66. resetDebugMenu.repeatMode = DebugActionRepeatMode.Never;
  67. AddAction(DebugAction.ResetAll, resetDebugMenu);
  68. var nextDebugPanel = new DebugActionDesc();
  69. #if USE_INPUT_SYSTEM
  70. nextDebugPanel.buttonAction = debugActionMap.FindAction(kDebugNextBtn);
  71. #else
  72. nextDebugPanel.buttonTriggerList.Add(new[] { kDebugNextBtn });
  73. #endif
  74. nextDebugPanel.repeatMode = DebugActionRepeatMode.Never;
  75. AddAction(DebugAction.NextDebugPanel, nextDebugPanel);
  76. var previousDebugPanel = new DebugActionDesc();
  77. #if USE_INPUT_SYSTEM
  78. previousDebugPanel.buttonAction = debugActionMap.FindAction(kDebugPreviousBtn);
  79. #else
  80. previousDebugPanel.buttonTriggerList.Add(new[] { kDebugPreviousBtn });
  81. #endif
  82. previousDebugPanel.repeatMode = DebugActionRepeatMode.Never;
  83. AddAction(DebugAction.PreviousDebugPanel, previousDebugPanel);
  84. var validate = new DebugActionDesc();
  85. #if USE_INPUT_SYSTEM
  86. validate.buttonAction = debugActionMap.FindAction(kValidateBtn);
  87. #else
  88. validate.buttonTriggerList.Add(new[] { kValidateBtn });
  89. #endif
  90. validate.repeatMode = DebugActionRepeatMode.Never;
  91. AddAction(DebugAction.Action, validate);
  92. var persistent = new DebugActionDesc();
  93. #if USE_INPUT_SYSTEM
  94. persistent.buttonAction = debugActionMap.FindAction(kPersistentBtn);
  95. #else
  96. persistent.buttonTriggerList.Add(new[] { kPersistentBtn });
  97. #endif
  98. persistent.repeatMode = DebugActionRepeatMode.Never;
  99. AddAction(DebugAction.MakePersistent, persistent);
  100. var multiplier = new DebugActionDesc();
  101. #if USE_INPUT_SYSTEM
  102. multiplier.buttonAction = debugActionMap.FindAction(kMultiplierBtn);
  103. #else
  104. multiplier.buttonTriggerList.Add(new[] { kMultiplierBtn });
  105. #endif
  106. multiplier.repeatMode = DebugActionRepeatMode.Delay;
  107. validate.repeatDelay = 0f;
  108. AddAction(DebugAction.Multiplier, multiplier);
  109. var moveVertical = new DebugActionDesc();
  110. #if USE_INPUT_SYSTEM
  111. moveVertical.buttonAction = debugActionMap.FindAction(kDPadVertical);
  112. #else
  113. moveVertical.axisTrigger = kDPadVertical;
  114. #endif
  115. moveVertical.repeatMode = DebugActionRepeatMode.Delay;
  116. moveVertical.repeatDelay = 0.16f;
  117. AddAction(DebugAction.MoveVertical, moveVertical);
  118. var moveHorizontal = new DebugActionDesc();
  119. #if USE_INPUT_SYSTEM
  120. moveHorizontal.buttonAction = debugActionMap.FindAction(kDPadHorizontal);
  121. #else
  122. moveHorizontal.axisTrigger = kDPadHorizontal;
  123. #endif
  124. moveHorizontal.repeatMode = DebugActionRepeatMode.Delay;
  125. moveHorizontal.repeatDelay = 0.16f;
  126. AddAction(DebugAction.MoveHorizontal, moveHorizontal);
  127. }
  128. internal void EnableInputActions()
  129. {
  130. #if USE_INPUT_SYSTEM
  131. foreach (var action in debugActionMap)
  132. action.Enable();
  133. #endif
  134. }
  135. void AddAction(DebugAction action, DebugActionDesc desc)
  136. {
  137. int index = (int)action;
  138. m_DebugActions[index] = desc;
  139. m_DebugActionStates[index] = new DebugActionState();
  140. }
  141. void SampleAction(int actionIndex)
  142. {
  143. var desc = m_DebugActions[actionIndex];
  144. var state = m_DebugActionStates[actionIndex];
  145. // Disable all input events if we're using the new input system
  146. #if USE_INPUT_SYSTEM
  147. if (state.runningAction == false)
  148. {
  149. if (desc.buttonAction != null)
  150. {
  151. var value = desc.buttonAction.ReadValue<float>();
  152. if (!Mathf.Approximately(value, 0))
  153. state.TriggerWithButton(desc.buttonAction, value);
  154. }
  155. }
  156. #elif ENABLE_LEGACY_INPUT_MANAGER
  157. //bool canSampleAction = (state.actionTriggered == false) || (desc.repeatMode == DebugActionRepeatMode.Delay && state.timer > desc.repeatDelay);
  158. if (state.runningAction == false)
  159. {
  160. // Check button triggers
  161. for (int buttonListIndex = 0; buttonListIndex < desc.buttonTriggerList.Count; ++buttonListIndex)
  162. {
  163. var buttons = desc.buttonTriggerList[buttonListIndex];
  164. bool allButtonPressed = true;
  165. try
  166. {
  167. foreach (var button in buttons)
  168. {
  169. allButtonPressed = Input.GetButton(button);
  170. if (!allButtonPressed)
  171. break;
  172. }
  173. }
  174. catch (ArgumentException)
  175. {
  176. // Exception thrown if the input mapping gets removed while in play mode (UUM-37148)
  177. allButtonPressed = false;
  178. }
  179. if (allButtonPressed)
  180. {
  181. state.TriggerWithButton(buttons, 1f);
  182. break;
  183. }
  184. }
  185. // Check axis triggers
  186. if (desc.axisTrigger != "")
  187. {
  188. try
  189. {
  190. float axisValue = Input.GetAxis(desc.axisTrigger);
  191. if (axisValue != 0f)
  192. state.TriggerWithAxis(desc.axisTrigger, axisValue);
  193. }
  194. catch (ArgumentException)
  195. {
  196. // Exception thrown if the input mapping gets removed while in play mode (UUM-37148)
  197. }
  198. }
  199. // Check key triggers
  200. for (int keyListIndex = 0; keyListIndex < desc.keyTriggerList.Count; ++keyListIndex)
  201. {
  202. bool allKeyPressed = true;
  203. var keys = desc.keyTriggerList[keyListIndex];
  204. try
  205. {
  206. foreach (var key in keys)
  207. {
  208. allKeyPressed = Input.GetKey(key);
  209. if (!allKeyPressed)
  210. break;
  211. }
  212. }
  213. catch (ArgumentException)
  214. {
  215. // Exception thrown if the input mapping gets removed while in play mode (UUM-37148)
  216. allKeyPressed = false;
  217. }
  218. if (allKeyPressed)
  219. {
  220. state.TriggerWithKey(keys, 1f);
  221. break;
  222. }
  223. }
  224. }
  225. #endif
  226. }
  227. void UpdateAction(int actionIndex)
  228. {
  229. var desc = m_DebugActions[actionIndex];
  230. var state = m_DebugActionStates[actionIndex];
  231. if (state.runningAction)
  232. state.Update(desc);
  233. }
  234. internal void UpdateActions()
  235. {
  236. for (int actionIndex = 0; actionIndex < m_DebugActions.Length; ++actionIndex)
  237. {
  238. UpdateAction(actionIndex);
  239. SampleAction(actionIndex);
  240. }
  241. }
  242. internal float GetAction(DebugAction action)
  243. {
  244. return m_DebugActionStates[(int)action].actionState;
  245. }
  246. internal bool GetActionToggleDebugMenuWithTouch()
  247. {
  248. #if USE_INPUT_SYSTEM
  249. if (!EnhancedTouchSupport.enabled)
  250. return false;
  251. var touches = InputSystem.EnhancedTouch.Touch.activeTouches;
  252. var touchCount = touches.Count;
  253. InputSystem.TouchPhase? expectedTouchPhase = null;
  254. #else
  255. var touchCount = Input.touchCount;
  256. TouchPhase? expectedTouchPhase = TouchPhase.Began;
  257. #endif
  258. if (touchCount == 3)
  259. {
  260. #if !USE_INPUT_SYSTEM
  261. var touches = Input.touches; // Causes an allocation, which is why this is inside the condition
  262. #endif
  263. foreach (var touch in touches)
  264. {
  265. // Gesture: 3-finger double-tap
  266. if ((!expectedTouchPhase.HasValue || touch.phase == expectedTouchPhase.Value) && touch.tapCount == 2)
  267. return true;
  268. }
  269. }
  270. return false;
  271. }
  272. internal bool GetActionReleaseScrollTarget()
  273. {
  274. #if USE_INPUT_SYSTEM
  275. bool mouseWheelActive = Mouse.current != null && Mouse.current.scroll.ReadValue() != Vector2.zero;
  276. bool touchSupported = Touchscreen.current != null;
  277. #else
  278. bool mouseWheelActive = Input.mouseScrollDelta != Vector2.zero;
  279. bool touchSupported = Input.touchSupported;
  280. #endif
  281. return mouseWheelActive || touchSupported; // Touchscreens have general problems with scrolling, so it's disabled.
  282. }
  283. void RegisterInputs()
  284. {
  285. #if UNITY_EDITOR && !USE_INPUT_SYSTEM
  286. var inputEntries = new List<InputManagerEntry>
  287. {
  288. new InputManagerEntry { name = kEnableDebugBtn1, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left ctrl", altBtnPositive = "joystick button 8" },
  289. new InputManagerEntry { name = kEnableDebugBtn2, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "backspace", altBtnPositive = "joystick button 9" },
  290. new InputManagerEntry { name = kResetBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left alt", altBtnPositive = "joystick button 1" },
  291. new InputManagerEntry { name = kDebugNextBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page down", altBtnPositive = "joystick button 5" },
  292. new InputManagerEntry { name = kDebugPreviousBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "page up", altBtnPositive = "joystick button 4" },
  293. new InputManagerEntry { name = kValidateBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "return", altBtnPositive = "joystick button 0" },
  294. new InputManagerEntry { name = kPersistentBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "right shift", altBtnPositive = "joystick button 2" },
  295. new InputManagerEntry { name = kMultiplierBtn, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "left shift", altBtnPositive = "joystick button 3" },
  296. new InputManagerEntry { name = kDPadHorizontal, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "right", btnNegative = "left", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f },
  297. new InputManagerEntry { name = kDPadVertical, kind = InputManagerEntry.Kind.KeyOrButton, btnPositive = "up", btnNegative = "down", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f },
  298. new InputManagerEntry { name = kDPadVertical, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Seventh, btnPositive = "up", btnNegative = "down", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f },
  299. new InputManagerEntry { name = kDPadHorizontal, kind = InputManagerEntry.Kind.Axis, axis = InputManagerEntry.Axis.Sixth, btnPositive = "right", btnNegative = "left", gravity = 1000f, deadZone = 0.001f, sensitivity = 1000f },
  300. };
  301. InputRegistering.RegisterInputs(inputEntries);
  302. #endif
  303. #if USE_INPUT_SYSTEM
  304. // Register input system actions
  305. var enableAction = debugActionMap.AddAction(kEnableDebug, type: InputActionType.Button);
  306. enableAction.AddCompositeBinding("ButtonWithOneModifier")
  307. .With("Modifier", "<Gamepad>/rightStickPress")
  308. .With("Button", "<Gamepad>/leftStickPress")
  309. .With("Modifier", "<Keyboard>/leftCtrl")
  310. .With("Button", "<Keyboard>/backspace");
  311. var resetAction = debugActionMap.AddAction(kResetBtn, type: InputActionType.Button);
  312. resetAction.AddCompositeBinding("ButtonWithOneModifier")
  313. .With("Modifier", "<Gamepad>/rightStickPress")
  314. .With("Button", "<Gamepad>/b")
  315. .With("Modifier", "<Keyboard>/leftAlt")
  316. .With("Button", "<Keyboard>/backspace");
  317. var next = debugActionMap.AddAction(kDebugNextBtn, type: InputActionType.Button);
  318. next.AddBinding("<Keyboard>/pageDown");
  319. next.AddBinding("<Gamepad>/rightShoulder");
  320. var previous = debugActionMap.AddAction(kDebugPreviousBtn, type: InputActionType.Button);
  321. previous.AddBinding("<Keyboard>/pageUp");
  322. previous.AddBinding("<Gamepad>/leftShoulder");
  323. var validateAction = debugActionMap.AddAction(kValidateBtn, type: InputActionType.Button);
  324. validateAction.AddBinding("<Keyboard>/enter");
  325. validateAction.AddBinding("<Gamepad>/a");
  326. var persistentAction = debugActionMap.AddAction(kPersistentBtn, type: InputActionType.Button);
  327. persistentAction.AddBinding("<Keyboard>/rightShift");
  328. persistentAction.AddBinding("<Gamepad>/x");
  329. var multiplierAction = debugActionMap.AddAction(kMultiplierBtn, type: InputActionType.Value);
  330. multiplierAction.AddBinding("<Keyboard>/leftShift");
  331. multiplierAction.AddBinding("<Gamepad>/y");
  332. var moveVerticalAction = debugActionMap.AddAction(kDPadVertical);
  333. moveVerticalAction.AddCompositeBinding("1DAxis")
  334. .With("Positive", "<Gamepad>/dpad/up")
  335. .With("Negative", "<Gamepad>/dpad/down")
  336. .With("Positive", "<Keyboard>/upArrow")
  337. .With("Negative", "<Keyboard>/downArrow");
  338. var moveHorizontalAction = debugActionMap.AddAction(kDPadHorizontal);
  339. moveHorizontalAction.AddCompositeBinding("1DAxis")
  340. .With("Positive", "<Gamepad>/dpad/right")
  341. .With("Negative", "<Gamepad>/dpad/left")
  342. .With("Positive", "<Keyboard>/rightArrow")
  343. .With("Negative", "<Keyboard>/leftArrow");
  344. #endif
  345. }
  346. }
  347. class DebugActionDesc
  348. {
  349. #if USE_INPUT_SYSTEM
  350. public InputAction buttonAction = null;
  351. #else
  352. public string axisTrigger = "";
  353. public List<string[]> buttonTriggerList = new List<string[]>();
  354. public List<KeyCode[]> keyTriggerList = new List<KeyCode[]>();
  355. #endif
  356. public DebugActionRepeatMode repeatMode = DebugActionRepeatMode.Never;
  357. public float repeatDelay;
  358. }
  359. class DebugActionState
  360. {
  361. enum DebugActionKeyType
  362. {
  363. Button,
  364. Axis,
  365. Key
  366. }
  367. DebugActionKeyType m_Type;
  368. #if USE_INPUT_SYSTEM
  369. InputAction inputAction;
  370. #else
  371. string[] m_PressedButtons;
  372. string m_PressedAxis = "";
  373. KeyCode[] m_PressedKeys;
  374. #endif
  375. bool[] m_TriggerPressedUp;
  376. float m_Timer;
  377. internal bool runningAction { get; private set; }
  378. internal float actionState { get; private set; }
  379. void Trigger(int triggerCount, float state)
  380. {
  381. actionState = state;
  382. runningAction = true;
  383. m_Timer = 0f;
  384. m_TriggerPressedUp = new bool[triggerCount];
  385. for (int i = 0; i < m_TriggerPressedUp.Length; ++i)
  386. m_TriggerPressedUp[i] = false;
  387. }
  388. #if USE_INPUT_SYSTEM
  389. public void TriggerWithButton(InputAction action, float state)
  390. {
  391. inputAction = action;
  392. Trigger(action.bindings.Count, state);
  393. }
  394. #else
  395. public void TriggerWithButton(string[] buttons, float state)
  396. {
  397. m_Type = DebugActionKeyType.Button;
  398. m_PressedButtons = buttons;
  399. m_PressedAxis = "";
  400. Trigger(buttons.Length, state);
  401. }
  402. public void TriggerWithAxis(string axis, float state)
  403. {
  404. m_Type = DebugActionKeyType.Axis;
  405. m_PressedAxis = axis;
  406. Trigger(1, state);
  407. }
  408. public void TriggerWithKey(KeyCode[] keys, float state)
  409. {
  410. m_Type = DebugActionKeyType.Key;
  411. m_PressedKeys = keys;
  412. m_PressedAxis = "";
  413. Trigger(keys.Length, state);
  414. }
  415. #endif
  416. void Reset()
  417. {
  418. runningAction = false;
  419. m_Timer = 0f;
  420. m_TriggerPressedUp = null;
  421. }
  422. public void Update(DebugActionDesc desc)
  423. {
  424. // Always reset this so that the action can only be caught once until repeat/reset
  425. actionState = 0f;
  426. if (m_TriggerPressedUp != null)
  427. {
  428. m_Timer += Time.deltaTime;
  429. for (int i = 0; i < m_TriggerPressedUp.Length; ++i)
  430. {
  431. #if USE_INPUT_SYSTEM
  432. if (inputAction != null)
  433. m_TriggerPressedUp[i] |= Mathf.Approximately(inputAction.ReadValue<float>(), 0f);
  434. #else
  435. if (m_Type == DebugActionKeyType.Button)
  436. m_TriggerPressedUp[i] |= Input.GetButtonUp(m_PressedButtons[i]);
  437. else if (m_Type == DebugActionKeyType.Axis)
  438. m_TriggerPressedUp[i] |= Mathf.Approximately(Input.GetAxis(m_PressedAxis), 0f);
  439. else
  440. m_TriggerPressedUp[i] |= Input.GetKeyUp(m_PressedKeys[i]);
  441. #endif
  442. }
  443. bool allTriggerUp = true;
  444. foreach (bool value in m_TriggerPressedUp)
  445. allTriggerUp &= value;
  446. if (allTriggerUp || (m_Timer > desc.repeatDelay && desc.repeatMode == DebugActionRepeatMode.Delay))
  447. Reset();
  448. }
  449. }
  450. }
  451. }