No Description
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.

InputSystemPlugin.cs 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #if UNITY_EDITOR && UNITY_2021_1_OR_NEWER
  2. using System;
  3. using System.Collections.Generic;
  4. using UnityEditor.DeviceSimulation;
  5. using UnityEngine.InputSystem.LowLevel;
  6. namespace UnityEngine.InputSystem.Editor
  7. {
  8. internal class InputSystemPlugin : DeviceSimulatorPlugin
  9. {
  10. internal Touchscreen SimulatorTouchscreen;
  11. private bool m_InputSystemEnabled;
  12. private bool m_Quitting;
  13. private List<InputDevice> m_DisabledDevices;
  14. public override string title => "Input System";
  15. public override void OnCreate()
  16. {
  17. m_InputSystemEnabled = EditorPlayerSettingHelpers.newSystemBackendsEnabled;
  18. if (m_InputSystemEnabled)
  19. {
  20. // Monitor whether the editor is quitting to avoid risking unsafe EnableDevice while quitting
  21. UnityEditor.EditorApplication.quitting += OnQuitting;
  22. m_DisabledDevices = new List<InputDevice>();
  23. // deviceSimulator is never null when the plugin is instantiated by a simulator window, but it can be null during unit tests
  24. if (deviceSimulator != null)
  25. deviceSimulator.touchScreenInput += OnTouchEvent;
  26. InputSystem.onDeviceChange += OnDeviceChange;
  27. // UGUI elements like a button don't get pressed when multiple pointers for example mouse and touchscreen are sending data at the same time
  28. foreach (var device in InputSystem.devices)
  29. {
  30. DisableConflictingDevice(device);
  31. }
  32. SimulatorTouchscreen = InputSystem.AddDevice<Touchscreen>("Device Simulator Touchscreen");
  33. }
  34. }
  35. internal void OnTouchEvent(TouchEvent touchEvent)
  36. {
  37. // Input System does not accept 0 as id
  38. var id = touchEvent.touchId + 1;
  39. InputSystem.QueueStateEvent(SimulatorTouchscreen,
  40. new TouchState
  41. {
  42. touchId = id,
  43. phase = ToInputSystem(touchEvent.phase),
  44. position = touchEvent.position
  45. });
  46. }
  47. private void DisableConflictingDevice(InputDevice device)
  48. {
  49. if (device.native && (device is Mouse || device is Pen) && device.enabled)
  50. {
  51. InputSystem.DisableDevice(device);
  52. m_DisabledDevices.Add(device);
  53. }
  54. }
  55. private void OnDeviceChange(InputDevice device, InputDeviceChange change)
  56. {
  57. if (change == InputDeviceChange.Added || change == InputDeviceChange.Reconnected)
  58. DisableConflictingDevice(device);
  59. }
  60. private static UnityEngine.InputSystem.TouchPhase ToInputSystem(UnityEditor.DeviceSimulation.TouchPhase original)
  61. {
  62. switch (original)
  63. {
  64. case UnityEditor.DeviceSimulation.TouchPhase.Began:
  65. return UnityEngine.InputSystem.TouchPhase.Began;
  66. case UnityEditor.DeviceSimulation.TouchPhase.Moved:
  67. return UnityEngine.InputSystem.TouchPhase.Moved;
  68. case UnityEditor.DeviceSimulation.TouchPhase.Ended:
  69. return UnityEngine.InputSystem.TouchPhase.Ended;
  70. case UnityEditor.DeviceSimulation.TouchPhase.Canceled:
  71. return UnityEngine.InputSystem.TouchPhase.Canceled;
  72. case UnityEditor.DeviceSimulation.TouchPhase.Stationary:
  73. return UnityEngine.InputSystem.TouchPhase.Stationary;
  74. default:
  75. throw new ArgumentOutOfRangeException(nameof(original), original, "Unexpected value");
  76. }
  77. }
  78. public override void OnDestroy()
  79. {
  80. if (m_InputSystemEnabled)
  81. {
  82. // deviceSimulator is never null when the plugin is instantiated by a simulator window, but it can be null during unit tests
  83. if (deviceSimulator != null)
  84. deviceSimulator.touchScreenInput -= OnTouchEvent;
  85. InputSystem.onDeviceChange -= OnDeviceChange;
  86. UnityEditor.EditorApplication.quitting -= OnQuitting;
  87. if (SimulatorTouchscreen != null)
  88. InputSystem.RemoveDevice(SimulatorTouchscreen);
  89. foreach (var device in m_DisabledDevices)
  90. {
  91. // Note that m_Quitting is used here to mitigate the problem reported in issue tracker:
  92. // https://issuetracker.unity3d.com/product/unity/issues/guid/UUM-10774.
  93. // Enabling a device will call into IOCTL of backend which will (may) be destroyed prior
  94. // to this callback on Unity version <= 2022.2. This is not a fix for the actual problem
  95. // of shutdown order but a package fix to mitigate this problem.
  96. if (device.added && !m_Quitting)
  97. InputSystem.EnableDevice(device);
  98. }
  99. }
  100. }
  101. private void OnQuitting()
  102. {
  103. m_Quitting = true;
  104. }
  105. }
  106. }
  107. #endif