Без опису
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using Unity.Collections.LowLevel.Unsafe;
  3. using UnityEngine.InputSystem.Layouts;
  4. #if UNITY_EDITOR
  5. using UnityEditor;
  6. #endif
  7. ////TODO: add API to send events in bulk rather than one by one
  8. namespace UnityEngine.InputSystem.LowLevel
  9. {
  10. internal delegate void InputUpdateDelegate(InputUpdateType updateType, ref InputEventBuffer eventBuffer);
  11. /// <summary>
  12. /// Input functions that have to be performed by the underlying input runtime.
  13. /// </summary>
  14. /// <remarks>
  15. /// The runtime owns the input event queue, reports device discoveries, and runs
  16. /// periodic updates that flushes out events from the queue. Updates can also be manually
  17. /// triggered by calling <see cref="Update"/>.
  18. /// </remarks>
  19. internal unsafe interface IInputRuntime
  20. {
  21. /// <summary>
  22. /// Allocate a new unique device ID.
  23. /// </summary>
  24. /// <returns>A numeric device ID that is not <see cref="InputDevice.InvalidDeviceId"/>.</returns>
  25. /// <remarks>
  26. /// Device IDs are managed by the runtime. This method allows creating devices that
  27. /// can use the same ID system but are not known to the underlying runtime.
  28. /// </remarks>
  29. int AllocateDeviceId();
  30. /// <summary>
  31. /// Manually trigger an update.
  32. /// </summary>
  33. /// <param name="type">Type of update to run. If this is a combination of updates, each flag
  34. /// that is set in the mask will run a separate update.</param>
  35. /// <remarks>
  36. /// Updates will flush out events and trigger <see cref="onBeforeUpdate"/> and <see cref="onUpdate"/>.
  37. /// Also, newly discovered devices will be reported by an update is run.
  38. /// </remarks>
  39. void Update(InputUpdateType type);
  40. /// <summary>
  41. /// Queue an input event.
  42. /// </summary>
  43. /// <remarks>
  44. /// This method has to be thread-safe.
  45. /// </remarks>
  46. /// <param name="ptr">Pointer to the event data. Uses the <see cref="InputEvent"/> format.</param>
  47. /// <remarks>
  48. /// Events are copied into an internal buffer. Thus the memory referenced by this method does
  49. /// not have to persist until the event is processed.
  50. /// </remarks>
  51. void QueueEvent(InputEvent* ptr);
  52. //NOTE: This method takes an IntPtr instead of a generic ref type parameter (like InputDevice.ExecuteCommand)
  53. // to avoid issues with AOT where generic interface methods can lead to problems. Il2cpp can handle it here
  54. // just fine but Mono will run into issues.
  55. /// <summary>
  56. /// Perform an I/O transaction directly against a specific device.
  57. /// </summary>
  58. /// <remarks>
  59. /// This function is used to set up device-specific communication controls between
  60. /// a device and the user of a device. The interface does not dictate a set of supported
  61. /// IOCTL control codes.
  62. /// </remarks>
  63. /// <param name="deviceId">Device to send the command to.</param>
  64. /// <param name="commandPtr">Pointer to the command buffer.</param>
  65. /// <returns>Negative value on failure, >=0 on success. Meaning of return values depends on the
  66. /// command sent to the device.</returns>
  67. long DeviceCommand(int deviceId, InputDeviceCommand* commandPtr);
  68. /// <summary>
  69. /// Set delegate to be called on input updates.
  70. /// </summary>
  71. InputUpdateDelegate onUpdate { get; set; }
  72. /// <summary>
  73. /// Set delegate to be called right before <see cref="onUpdate"/>.
  74. /// </summary>
  75. /// <remarks>
  76. /// This delegate is meant to allow events to be queued that should be processed right
  77. /// in the upcoming update.
  78. /// </remarks>
  79. Action<InputUpdateType> onBeforeUpdate { get; set; }
  80. Func<InputUpdateType, bool> onShouldRunUpdate { get; set; }
  81. #if UNITY_EDITOR
  82. /// <summary>
  83. /// Set delegate to be called during player loop initialization callbacks.
  84. /// </summary>
  85. Action onPlayerLoopInitialization { get; set; }
  86. #endif
  87. /// <summary>
  88. /// Set delegate to be called when a new device is discovered.
  89. /// </summary>
  90. /// <remarks>
  91. /// The runtime should delay reporting of already present devices until the delegate
  92. /// has been put in place and then call the delegate for every device already in the system.
  93. ///
  94. /// First parameter is the ID assigned to the device, second parameter is a description
  95. /// in JSON format of the device (see <see cref="InputDeviceDescription.FromJson"/>).
  96. /// </remarks>
  97. Action<int, string> onDeviceDiscovered { get; set; }
  98. /// <summary>
  99. /// Set delegate to call when the application changes focus.
  100. /// </summary>
  101. /// <seealso cref="Application.onFocusChanged"/>
  102. Action<bool> onPlayerFocusChanged { get; set; }
  103. /// <summary>
  104. // Is true when the player or game view has focus.
  105. /// </summary>
  106. /// <seealso cref="Application.isFocused"/>
  107. bool isPlayerFocused { get; }
  108. /// <summary>
  109. /// Set delegate to invoke when system is shutting down.
  110. /// </summary>
  111. Action onShutdown { get; set; }
  112. /// <summary>
  113. /// Set the background polling frequency for devices that have to be polled.
  114. /// </summary>
  115. /// <remarks>
  116. /// The frequency is in Hz. A value of 60 means that polled devices get sampled
  117. /// 60 times a second.
  118. /// </remarks>
  119. float pollingFrequency { get; set; }
  120. /// <summary>
  121. /// The current time on the same timeline that input events are delivered on.
  122. /// </summary>
  123. /// <remarks>
  124. /// This is used to timestamp events that are not explicitly supplied with timestamps.
  125. ///
  126. /// Time in the input system progresses linearly and in real-time and relates to when Unity was started.
  127. /// In the editor, this always corresponds to <see cref="EditorApplication.timeSinceStartup"/>.
  128. ///
  129. /// Input time, however, is offset in relation to <see cref="Time.realtimeSinceStartup"/>. This is because
  130. /// in the player, <see cref="Time.realtimeSinceStartup"/> is reset to 0 upon loading the first scene and
  131. /// in the editor, <see cref="Time.realtimeSinceStartup"/> is reset to 0 whenever the editor enters play
  132. /// mode. As the resetting runs counter to the need of linearly progressing time for input, the input
  133. /// system will not reset time along with <see cref="Time.realtimeSinceStartup"/>.
  134. /// </remarks>
  135. double currentTime { get; }
  136. /// <summary>
  137. /// The current time on the same timeline that input events are delivered on, for the current FixedUpdate.
  138. /// </summary>
  139. /// <remarks>
  140. /// This should be used inside FixedUpdate calls instead of currentTime, as FixedUpdates are simulated at times
  141. /// not matching the real time the simulation corresponds to.
  142. /// </remarks>
  143. double currentTimeForFixedUpdate { get; }
  144. /// <summary>
  145. /// The value of <c>Time.unscaledTime</c>.
  146. /// </summary>
  147. float unscaledGameTime { get; }
  148. /// <summary>
  149. /// The time offset that <see cref="currentTime"/> currently has to <see cref="Time.realtimeSinceStartup"/>.
  150. /// </summary>
  151. double currentTimeOffsetToRealtimeSinceStartup { get; }
  152. bool runInBackground { get; set; }
  153. Vector2 screenSize { get; }
  154. ScreenOrientation screenOrientation { get; }
  155. // If analytics are enabled, the runtime receives analytics events from the input manager.
  156. // See InputAnalytics.
  157. #if UNITY_ANALYTICS || UNITY_EDITOR
  158. void RegisterAnalyticsEvent(string name, int maxPerHour, int maxPropertiesPerEvent);
  159. void SendAnalyticsEvent(string name, object data);
  160. #endif
  161. bool isInBatchMode { get; }
  162. #if UNITY_EDITOR
  163. Action<PlayModeStateChange> onPlayModeChanged { get; set; }
  164. Action onProjectChange { get; set; }
  165. bool isInPlayMode { get; }
  166. bool isPaused { get; }
  167. bool isEditorActive { get; }
  168. // Functionality related to the Unity Remote.
  169. Func<IntPtr, bool> onUnityRemoteMessage { set; }
  170. void SetUnityRemoteGyroEnabled(bool value);
  171. void SetUnityRemoteGyroUpdateInterval(float interval);
  172. #endif
  173. }
  174. internal static class InputRuntime
  175. {
  176. public static IInputRuntime s_Instance;
  177. public static double s_CurrentTimeOffsetToRealtimeSinceStartup;
  178. }
  179. internal static class InputRuntimeExtensions
  180. {
  181. public static unsafe long DeviceCommand<TCommand>(this IInputRuntime runtime, int deviceId, ref TCommand command)
  182. where TCommand : struct, IInputDeviceCommandInfo
  183. {
  184. if (runtime == null)
  185. throw new ArgumentNullException(nameof(runtime));
  186. return runtime.DeviceCommand(deviceId, (InputDeviceCommand*)UnsafeUtility.AddressOf(ref command));
  187. }
  188. }
  189. }