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.

EnhancedTouchSupport.cs 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine.InputSystem.LowLevel;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. ////REVIEW: this *really* should be renamed to TouchPolling or something like that
  8. ////REVIEW: Should this auto-enable itself when the API is used? Problem with this is that it means the first touch inputs will get missed
  9. //// as by the time the API is polled, we're already into the first frame.
  10. ////TODO: gesture support
  11. ////TODO: high-frequency touch support
  12. ////REVIEW: have TouchTap, TouchSwipe, etc. wrapper MonoBehaviours like LeanTouch?
  13. ////TODO: as soon as we can break the API, remove the EnhancedTouchSupport class altogether and rename UnityEngine.InputSystem.EnhancedTouch to TouchPolling
  14. ////FIXME: does not survive domain reloads
  15. namespace UnityEngine.InputSystem.EnhancedTouch
  16. {
  17. /// <summary>
  18. /// API to control enhanced touch facilities like <see cref="Touch"/> that are not
  19. /// enabled by default.
  20. /// </summary>
  21. /// <remarks>
  22. /// Enhanced touch support provides automatic finger tracking and touch history recording.
  23. /// It is an API designed for polling, i.e. for querying touch state directly in methods
  24. /// such as <c>MonoBehaviour.Update</c>. Enhanced touch support cannot be used in combination
  25. /// with <see cref="InputAction"/>s though both can be used side-by-side.
  26. ///
  27. /// <example>
  28. /// <code>
  29. /// public class MyBehavior : MonoBehaviour
  30. /// {
  31. /// protected void OnEnable()
  32. /// {
  33. /// EnhancedTouchSupport.Enable();
  34. /// }
  35. ///
  36. /// protected void OnDisable()
  37. /// {
  38. /// EnhancedTouchSupport.Disable();
  39. /// }
  40. ///
  41. /// protected void Update()
  42. /// {
  43. /// var activeTouches = Touch.activeTouches;
  44. /// for (var i = 0; i &lt; activeTouches.Count; ++i)
  45. /// Debug.Log("Active touch: " + activeTouches[i]);
  46. /// }
  47. /// }
  48. /// </code>
  49. /// </example>
  50. /// </remarks>
  51. /// <seealso cref="Touch"/>
  52. /// <seealso cref="Finger"/>
  53. public static class EnhancedTouchSupport
  54. {
  55. /// <summary>
  56. /// Whether enhanced touch support is currently enabled.
  57. /// </summary>
  58. /// <value>True if EnhancedTouch support has been enabled.</value>
  59. public static bool enabled => s_Enabled > 0;
  60. private static int s_Enabled;
  61. private static InputSettings.UpdateMode s_UpdateMode;
  62. /// <summary>
  63. /// Enable enhanced touch support.
  64. /// </summary>
  65. /// <remarks>
  66. /// Calling this method is necessary to enable the functionality provided
  67. /// by <see cref="Touch"/> and <see cref="Finger"/>. These APIs add extra
  68. /// processing to touches and are thus disabled by default.
  69. ///
  70. /// Calls to <c>Enable</c> and <see cref="Disable"/> balance each other out.
  71. /// If <c>Enable</c> is called repeatedly, it will take as many calls to
  72. /// <see cref="Disable"/> to disable the system again.
  73. /// </remarks>
  74. public static void Enable()
  75. {
  76. ++s_Enabled;
  77. if (s_Enabled > 1)
  78. return;
  79. InputSystem.onDeviceChange += OnDeviceChange;
  80. InputSystem.onBeforeUpdate += Touch.BeginUpdate;
  81. InputSystem.onSettingsChange += OnSettingsChange;
  82. #if UNITY_EDITOR
  83. AssemblyReloadEvents.beforeAssemblyReload += OnBeforeDomainReload;
  84. #endif
  85. SetUpState();
  86. }
  87. /// <summary>
  88. /// Disable enhanced touch support.
  89. /// </summary>
  90. /// <remarks>
  91. /// This method only undoes a single call to <see cref="Enable"/>.
  92. /// </remarks>
  93. public static void Disable()
  94. {
  95. if (!enabled)
  96. return;
  97. --s_Enabled;
  98. if (s_Enabled > 0)
  99. return;
  100. InputSystem.onDeviceChange -= OnDeviceChange;
  101. InputSystem.onBeforeUpdate -= Touch.BeginUpdate;
  102. InputSystem.onSettingsChange -= OnSettingsChange;
  103. #if UNITY_EDITOR
  104. AssemblyReloadEvents.beforeAssemblyReload -= OnBeforeDomainReload;
  105. #endif
  106. TearDownState();
  107. }
  108. internal static void Reset()
  109. {
  110. Touch.s_GlobalState.touchscreens = default;
  111. Touch.s_GlobalState.playerState.Destroy();
  112. Touch.s_GlobalState.playerState = default;
  113. #if UNITY_EDITOR
  114. Touch.s_GlobalState.editorState.Destroy();
  115. Touch.s_GlobalState.editorState = default;
  116. #endif
  117. s_Enabled = 0;
  118. }
  119. private static void SetUpState()
  120. {
  121. Touch.s_GlobalState.playerState.updateMask = InputUpdateType.Dynamic | InputUpdateType.Manual | InputUpdateType.Fixed;
  122. #if UNITY_EDITOR
  123. Touch.s_GlobalState.editorState.updateMask = InputUpdateType.Editor;
  124. #endif
  125. s_UpdateMode = InputSystem.settings.updateMode;
  126. foreach (var device in InputSystem.devices)
  127. OnDeviceChange(device, InputDeviceChange.Added);
  128. }
  129. internal static void TearDownState()
  130. {
  131. foreach (var device in InputSystem.devices)
  132. OnDeviceChange(device, InputDeviceChange.Removed);
  133. Touch.s_GlobalState.playerState.Destroy();
  134. #if UNITY_EDITOR
  135. Touch.s_GlobalState.editorState.Destroy();
  136. #endif
  137. Touch.s_GlobalState.playerState = default;
  138. #if UNITY_EDITOR
  139. Touch.s_GlobalState.editorState = default;
  140. #endif
  141. }
  142. private static void OnDeviceChange(InputDevice device, InputDeviceChange change)
  143. {
  144. switch (change)
  145. {
  146. case InputDeviceChange.Added:
  147. {
  148. if (device is Touchscreen touchscreen)
  149. Touch.AddTouchscreen(touchscreen);
  150. break;
  151. }
  152. case InputDeviceChange.Removed:
  153. {
  154. if (device is Touchscreen touchscreen)
  155. Touch.RemoveTouchscreen(touchscreen);
  156. break;
  157. }
  158. }
  159. }
  160. private static void OnSettingsChange()
  161. {
  162. var currentUpdateMode = InputSystem.settings.updateMode;
  163. if (s_UpdateMode == currentUpdateMode)
  164. return;
  165. TearDownState();
  166. SetUpState();
  167. }
  168. #if UNITY_EDITOR
  169. private static void OnBeforeDomainReload()
  170. {
  171. // We need to release NativeArrays we're holding before losing track of them during domain reloads.
  172. Touch.s_GlobalState.playerState.Destroy();
  173. Touch.s_GlobalState.editorState.Destroy();
  174. }
  175. #endif
  176. [Conditional("DEVELOPMENT_BUILD")]
  177. [Conditional("UNITY_EDITOR")]
  178. internal static void CheckEnabled()
  179. {
  180. if (!enabled)
  181. throw new InvalidOperationException("EnhancedTouch API is not enabled; call EnhancedTouchSupport.Enable()");
  182. }
  183. }
  184. }