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