Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UnityRemoteTestScript.cs 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3. using UnityEngine.InputSystem.EnhancedTouch;
  4. using UnityEngine.UI;
  5. using Gyroscope = UnityEngine.InputSystem.Gyroscope;
  6. using Touch = UnityEngine.InputSystem.EnhancedTouch.Touch;
  7. public class UnityRemoteTestScript : MonoBehaviour
  8. {
  9. // Suppress harmless warning about overriding camera from UnityEngine.Component.camera where this property have been
  10. // marked obsolete/deprecated and depending on Unity version this warning may trigger when building a player.
  11. // warning CS0109: The member 'UnityRemoteTestScript.camera' does not hide an accessible member. The new keyword is not required.
  12. #pragma warning disable 0109
  13. public new Camera camera;
  14. #pragma warning restore 0109
  15. public Text accelerometerInputText;
  16. public Text touchInputText;
  17. public Text gyroInputText;
  18. // We rotate this cube based on gyro input. Also, we sync its position on screen
  19. // the position of the primary touch.
  20. public Transform rotatingCube;
  21. private Vector3 m_Rotation;
  22. private float m_CubeOffsetFromCanvas;
  23. private Vector3 m_CubeStartingPosition;
  24. public void ResetCube()
  25. {
  26. rotatingCube.SetPositionAndRotation(m_CubeStartingPosition, default);
  27. }
  28. private void OnEnable()
  29. {
  30. m_CubeOffsetFromCanvas = rotatingCube.position.z - transform.position.z;
  31. m_CubeStartingPosition = rotatingCube.position;
  32. EnhancedTouchSupport.Enable();
  33. }
  34. private void OnDisable()
  35. {
  36. EnhancedTouchSupport.Disable();
  37. }
  38. private void Update()
  39. {
  40. UpdateTouch();
  41. UpdateAccelerometer();
  42. UpdateGyro();
  43. }
  44. private void UpdateTouch()
  45. {
  46. var touchscreen = GetRemoteDevice<Touchscreen>();
  47. if (touchscreen == null)
  48. {
  49. touchInputText.text = "No remote touchscreen found.";
  50. return;
  51. }
  52. // Dump active touches.
  53. string activeTouches = null;
  54. foreach (var touch in Touch.activeTouches)
  55. {
  56. // Skip any touch not from our remote touchscreen.
  57. if (touch.screen != touchscreen)
  58. continue;
  59. if (activeTouches == null)
  60. activeTouches = "Active Touches:\n";
  61. activeTouches += $"\nid={touch.touchId} phase={touch.phase} position={touch.screenPosition} pressure={touch.pressure}\n";
  62. }
  63. if (activeTouches == null)
  64. activeTouches = "No active touches.";
  65. touchInputText.text = activeTouches;
  66. // Find world-space position of current primary touch (if any).
  67. if (touchscreen.primaryTouch.isInProgress)
  68. {
  69. var touchPosition = touchscreen.primaryTouch.position.ReadValue();
  70. var worldSpacePosition =
  71. camera.ScreenToWorldPoint(new Vector3(touchPosition.x, touchPosition.y, transform.position.z + m_CubeOffsetFromCanvas));
  72. rotatingCube.position = worldSpacePosition;
  73. }
  74. }
  75. private void UpdateAccelerometer()
  76. {
  77. var accelerometer = GetRemoteDevice<Accelerometer>();
  78. if (accelerometer == null)
  79. {
  80. accelerometerInputText.text = "No remote accelerometer found.";
  81. return;
  82. }
  83. var value = accelerometer.acceleration.ReadValue();
  84. accelerometerInputText.text = $"Accelerometer: x={value.x} y={value.y} z={value.z}";
  85. }
  86. private void UpdateGyro()
  87. {
  88. var gyro = GetRemoteDevice<Gyroscope>();
  89. var attitude = GetRemoteDevice<AttitudeSensor>();
  90. var gravity = GetRemoteDevice<GravitySensor>();
  91. var acceleration = GetRemoteDevice<LinearAccelerationSensor>();
  92. // Enable gyro from remote, if needed.
  93. EnableDeviceIfNeeded(gyro);
  94. EnableDeviceIfNeeded(attitude);
  95. EnableDeviceIfNeeded(gravity);
  96. EnableDeviceIfNeeded(acceleration);
  97. string text;
  98. if (gyro == null && attitude == null && gravity == null && acceleration == null)
  99. {
  100. text = "No remote gyro found.";
  101. }
  102. else
  103. {
  104. string gyroText = null;
  105. string attitudeText = null;
  106. string gravityText = null;
  107. string accelerationText = null;
  108. if (gyro != null)
  109. {
  110. var rotation = gyro.angularVelocity.ReadValue();
  111. gyroText = $"Rotation: x={rotation.x} y={rotation.y} z={rotation.z}";
  112. // Update rotation of cube.
  113. m_Rotation += rotation;
  114. rotatingCube.localEulerAngles = m_Rotation;
  115. }
  116. if (attitude != null)
  117. {
  118. var attitudeValue = attitude.attitude.ReadValue();
  119. attitudeText = $"Attitude: x={attitudeValue.x} y={attitudeValue.y} z={attitudeValue.z} w={attitudeValue.w}";
  120. }
  121. if (gravity != null)
  122. {
  123. var gravityValue = gravity.gravity.ReadValue();
  124. gravityText = $"Gravity: x={gravityValue.x} y={gravityValue.y} z={gravityValue.z}";
  125. }
  126. if (acceleration != null)
  127. {
  128. var accelerationValue = acceleration.acceleration.ReadValue();
  129. accelerationText = $"Acceleration: x={accelerationValue.x} y={accelerationValue.y} z={accelerationValue.z}";
  130. }
  131. text = string.Join("\n", gyroText, attitudeText, gravityText, accelerationText);
  132. }
  133. gyroInputText.text = text;
  134. }
  135. private static void EnableDeviceIfNeeded(InputDevice device)
  136. {
  137. if (device != null && !device.enabled)
  138. InputSystem.EnableDevice(device);
  139. }
  140. // Make sure we're not thrown off track by locally having sensors on the device. Instead
  141. // explicitly grab the remote ones.
  142. private static TDevice GetRemoteDevice<TDevice>()
  143. where TDevice : InputDevice
  144. {
  145. foreach (var device in InputSystem.devices)
  146. if (device.remote && device is TDevice deviceOfType)
  147. return deviceOfType;
  148. return default;
  149. }
  150. }