6 #if UNITY_2017_2_OR_NEWER 23 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 24 public readonly InteractionSourceHandedness
Handedness;
27 private GameObject home;
28 private Transform homePressed;
29 private Transform homeUnpressed;
30 private GameObject menu;
31 private Transform menuPressed;
32 private Transform menuUnpressed;
33 private GameObject grasp;
34 private Transform graspPressed;
35 private Transform graspUnpressed;
36 private GameObject thumbstickPress;
37 private Transform thumbstickPressed;
38 private Transform thumbstickUnpressed;
39 private GameObject thumbstickX;
40 private Transform thumbstickXMin;
41 private Transform thumbstickXMax;
42 private GameObject thumbstickY;
43 private Transform thumbstickYMin;
44 private Transform thumbstickYMax;
45 private GameObject select;
46 private Transform selectPressed;
47 private Transform selectUnpressed;
48 private GameObject touchpadPress;
49 private Transform touchpadPressed;
50 private Transform touchpadUnpressed;
51 private GameObject touchpadTouchX;
52 private Transform touchpadTouchXMin;
53 private Transform touchpadTouchXMax;
54 private GameObject touchpadTouchY;
55 private Transform touchpadTouchYMin;
56 private Transform touchpadTouchYMax;
57 private GameObject touchpadTouchVisualizer;
58 private GameObject pointingPose;
61 private bool wasGrasped;
62 private bool wasMenuPressed;
63 private bool wasHomePressed;
64 private bool wasThumbstickPressed;
65 private bool wasTouchpadPressed;
66 private bool wasTouchpadTouched;
67 private Vector2 lastThumbstickPosition;
68 private Vector2 lastTouchpadPosition;
69 private double lastSelectPressedAmount;
72 #
if UNITY_WSA && UNITY_2017_2_OR_NEWER
73 , InteractionSourceHandedness handedness
77 ControllerParent = controllerParent;
78 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 79 Handedness = handedness;
104 elementTransform = home.transform;
111 elementTransform = menu.transform;
118 elementTransform = select.transform;
125 elementTransform = grasp.transform;
130 if (thumbstickPress != null)
132 elementTransform = thumbstickPress.transform;
137 if (touchpadPress != null)
139 elementTransform = touchpadPress.transform;
145 if (pointingPose != null)
147 elementTransform = pointingPose.transform;
153 elementTransform = null;
166 foreach (Transform child
in childTransforms)
174 switch (child.name.ToLower())
179 case "pointing_pose":
180 pointingPose = child.gameObject;
183 switch (child.parent.name.ToLower())
192 graspPressed = child;
195 selectPressed = child;
197 case "thumbstick_press":
198 thumbstickPressed = child;
200 case "touchpad_press":
201 touchpadPressed = child;
206 switch (child.parent.name.ToLower())
209 homeUnpressed = child;
212 menuUnpressed = child;
215 graspUnpressed = child;
218 selectUnpressed = child;
220 case "thumbstick_press":
221 thumbstickUnpressed = child;
223 case "touchpad_press":
224 touchpadUnpressed = child;
229 switch (child.parent.name.ToLower())
232 thumbstickXMin = child;
235 thumbstickYMin = child;
237 case "touchpad_touch_x":
238 touchpadTouchXMin = child;
240 case "touchpad_touch_y":
241 touchpadTouchYMin = child;
246 switch (child.parent.name.ToLower())
249 thumbstickXMax = child;
252 thumbstickYMax = child;
254 case "touchpad_touch_x":
255 touchpadTouchXMax = child;
257 case "touchpad_touch_y":
258 touchpadTouchYMax = child;
263 switch (child.parent.name.ToLower())
266 home = child.gameObject;
269 menu = child.gameObject;
272 grasp = child.gameObject;
275 select = child.gameObject;
277 case "thumbstick_press":
278 thumbstickPress = child.gameObject;
281 thumbstickX = child.gameObject;
284 thumbstickY = child.gameObject;
286 case "touchpad_press":
287 touchpadPress = child.gameObject;
289 case "touchpad_touch_x":
290 touchpadTouchX = child.gameObject;
292 case "touchpad_touch_y":
293 touchpadTouchY = child.gameObject;
303 if (grasp != null && graspPressed != null && graspUnpressed != null && isGrasped != wasGrasped)
305 SetLocalPositionAndRotation(grasp, isGrasped ? graspPressed : graspUnpressed);
306 wasGrasped = isGrasped;
312 if (menu != null && menuPressed != null && menuUnpressed != null && isMenuPressed != wasMenuPressed)
314 SetLocalPositionAndRotation(menu, isMenuPressed ? menuPressed : menuUnpressed);
315 wasMenuPressed = isMenuPressed;
321 if (home != null && homePressed != null && homeUnpressed != null && isHomePressed != wasHomePressed)
323 SetLocalPositionAndRotation(home, isHomePressed ? homePressed : homeUnpressed);
324 wasHomePressed = isHomePressed;
330 if (select != null && selectPressed != null && selectUnpressed != null && newSelectPressedAmount != lastSelectPressedAmount)
332 select.transform.localPosition = Vector3.Lerp(selectUnpressed.localPosition, selectPressed.localPosition, newSelectPressedAmount);
333 select.transform.localRotation = Quaternion.Lerp(selectUnpressed.localRotation, selectPressed.localRotation, newSelectPressedAmount);
334 lastSelectPressedAmount = newSelectPressedAmount;
340 if (thumbstickPress != null && thumbstickPressed != null && thumbstickUnpressed != null && isThumbstickPressed != wasThumbstickPressed)
342 SetLocalPositionAndRotation(thumbstickPress, isThumbstickPressed ? thumbstickPressed : thumbstickUnpressed);
343 wasThumbstickPressed = isThumbstickPressed;
346 if (thumbstickX != null && thumbstickY != null && thumbstickXMin != null && thumbstickXMax != null && thumbstickYMin != null && thumbstickYMax != null && newThumbstickPosition != lastThumbstickPosition)
348 Vector2 thumbstickNormalized = (newThumbstickPosition + Vector2.one) * 0.5f;
350 thumbstickX.transform.localPosition = Vector3.Lerp(thumbstickXMin.localPosition, thumbstickXMax.localPosition, thumbstickNormalized.x);
351 thumbstickX.transform.localRotation = Quaternion.Lerp(thumbstickXMin.localRotation, thumbstickXMax.localRotation, thumbstickNormalized.x);
353 thumbstickY.transform.localPosition = Vector3.Lerp(thumbstickYMax.localPosition, thumbstickYMin.localPosition, thumbstickNormalized.y);
354 thumbstickY.transform.localRotation = Quaternion.Lerp(thumbstickYMax.localRotation, thumbstickYMin.localRotation, thumbstickNormalized.y);
356 lastThumbstickPosition = newThumbstickPosition;
360 public void AnimateTouchpad(
bool isTouchpadPressed,
bool isTouchpadTouched, Vector2 newTouchpadPosition)
362 if (touchpadPress != null && touchpadPressed != null && touchpadUnpressed != null && isTouchpadPressed != wasTouchpadPressed)
364 SetLocalPositionAndRotation(touchpadPress, isTouchpadPressed ? touchpadPressed : touchpadUnpressed);
365 wasTouchpadPressed = isTouchpadPressed;
368 if (touchpadTouchVisualizer != null && isTouchpadTouched != wasTouchpadTouched)
370 touchpadTouchVisualizer.SetActive(isTouchpadTouched);
371 wasTouchpadTouched = isTouchpadTouched;
374 if (touchpadTouchX != null && touchpadTouchY != null && touchpadTouchXMin != null && touchpadTouchXMax != null && touchpadTouchYMin != null && touchpadTouchYMax != null && newTouchpadPosition != lastTouchpadPosition)
376 Vector2 touchpadNormalized = (newTouchpadPosition + Vector2.one) * 0.5f;
378 touchpadTouchX.transform.localPosition = Vector3.Lerp(touchpadTouchXMin.localPosition, touchpadTouchXMax.localPosition, touchpadNormalized.x);
379 touchpadTouchX.transform.localRotation = Quaternion.Lerp(touchpadTouchXMin.localRotation, touchpadTouchXMax.localRotation, touchpadNormalized.x);
381 touchpadTouchY.transform.localPosition = Vector3.Lerp(touchpadTouchYMax.localPosition, touchpadTouchYMin.localPosition, touchpadNormalized.y);
382 touchpadTouchY.transform.localRotation = Quaternion.Lerp(touchpadTouchYMax.localRotation, touchpadTouchYMin.localRotation, touchpadNormalized.y);
384 lastTouchpadPosition = newTouchpadPosition;
388 private void SetLocalPositionAndRotation(GameObject buttonGameObject, Transform newTransform)
390 buttonGameObject.transform.localPosition = newTransform.localPosition;
391 buttonGameObject.transform.localRotation = newTransform.localRotation;
396 MeshRenderer[] renderers = ControllerParent.GetComponentsInChildren<MeshRenderer>();
397 for (
int i = 0; i < renderers.Length; i++)
399 renderers[i].enabled = visible;