7 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 8 using System.Collections.Generic;
16 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 17 private class ControllerState
20 public Vector3 PointerPosition;
21 public Quaternion PointerRotation;
22 public Vector3 GripPosition;
23 public Quaternion GripRotation;
25 public bool MenuPressed;
26 public bool SelectPressed;
27 public float SelectPressedAmount;
28 public bool ThumbstickPressed;
29 public Vector2 ThumbstickPosition;
30 public bool TouchpadPressed;
31 public bool TouchpadTouched;
32 public Vector2 TouchpadPosition;
35 private Dictionary<uint, ControllerState> controllers;
68 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 69 controllers =
new Dictionary<uint, ControllerState>();
71 InteractionManager.InteractionSourceDetected += InteractionManager_InteractionSourceDetected;
73 InteractionManager.InteractionSourceLost += InteractionManager_InteractionSourceLost;
74 InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated;
86 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 87 private void InteractionManager_InteractionSourceDetected(InteractionSourceDetectedEventArgs obj)
89 Debug.LogFormat(
"{0} {1} Detected", obj.state.source.handedness, obj.state.source.kind);
91 if (obj.state.source.kind == InteractionSourceKind.Controller && !controllers.ContainsKey(obj.state.source.id))
93 controllers.Add(obj.state.source.id,
new ControllerState { Handedness = obj.state.source.handedness });
97 private void InteractionManager_InteractionSourceLost(InteractionSourceLostEventArgs obj)
99 Debug.LogFormat(
"{0} {1} Lost", obj.state.source.handedness, obj.state.source.kind);
101 controllers.Remove(obj.state.source.id);
104 private void InteractionManager_InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj)
106 ControllerState controllerState;
107 if (controllers.TryGetValue(obj.state.source.id, out controllerState))
109 obj.state.sourcePose.TryGetPosition(out controllerState.PointerPosition, InteractionSourceNode.Pointer);
110 obj.state.sourcePose.TryGetRotation(out controllerState.PointerRotation, InteractionSourceNode.Pointer);
111 obj.state.sourcePose.TryGetPosition(out controllerState.GripPosition, InteractionSourceNode.Grip);
112 obj.state.sourcePose.TryGetRotation(out controllerState.GripRotation, InteractionSourceNode.Grip);
114 controllerState.Grasped = obj.state.grasped;
115 controllerState.MenuPressed = obj.state.menuPressed;
116 controllerState.SelectPressed = obj.state.selectPressed;
117 controllerState.SelectPressedAmount = obj.state.selectPressedAmount;
118 controllerState.ThumbstickPressed = obj.state.thumbstickPressed;
119 controllerState.ThumbstickPosition = obj.state.thumbstickPosition;
120 controllerState.TouchpadPressed = obj.state.touchpadPressed;
121 controllerState.TouchpadTouched = obj.state.touchpadTouched;
122 controllerState.TouchpadPosition = obj.state.touchpadPosition;
127 private string GetControllerInfo()
129 string toReturn =
string.Empty;
130 #if UNITY_WSA && UNITY_2017_2_OR_NEWER 131 foreach (ControllerState controllerState
in controllers.Values)
134 toReturn +=
string.Format(
"Hand: {0}\nPointer: Position: {1} Rotation: {2}\n" +
135 "Grip: Position: {3} Rotation: {4}\nGrasped: {5} " +
136 "MenuPressed: {6}\nSelect: Pressed: {7} PressedAmount: {8}\n" +
137 "Thumbstick: Pressed: {9} Position: {10}\nTouchpad: Pressed: {11} " +
138 "Touched: {12} Position: {13}\n\n",
139 controllerState.Handedness, controllerState.PointerPosition, controllerState.PointerRotation.eulerAngles,
140 controllerState.GripPosition, controllerState.GripRotation.eulerAngles, controllerState.Grasped,
141 controllerState.MenuPressed, controllerState.SelectPressed, controllerState.SelectPressedAmount,
142 controllerState.ThumbstickPressed, controllerState.ThumbstickPosition, controllerState.TouchpadPressed,
143 controllerState.TouchpadTouched, controllerState.TouchpadPosition);
146 if (controllerState.Handedness.Equals(InteractionSourceHandedness.Left))
148 LeftInfoTextPointerPosition.text = controllerState.Handedness.ToString();
149 LeftInfoTextPointerRotation.text = controllerState.PointerRotation.ToString();
150 LeftInfoTextGripPosition.text = controllerState.GripPosition.ToString();
151 LeftInfoTextGripRotation.text = controllerState.GripRotation.ToString();
152 LeftInfoTextGripGrasped.text = controllerState.Grasped.ToString();
153 LeftInfoTextMenuPressed.text = controllerState.MenuPressed.ToString();
154 LeftInfoTextTriggerPressed.text = controllerState.SelectPressed.ToString();
155 LeftInfoTextTriggerPressedAmount.text = controllerState.SelectPressedAmount.ToString();
156 LeftInfoTextThumbstickPressed.text = controllerState.ThumbstickPressed.ToString();
157 LeftInfoTextThumbstickPosition.text = controllerState.ThumbstickPosition.ToString();
158 LeftInfoTextTouchpadPressed.text = controllerState.TouchpadPressed.ToString();
159 LeftInfoTextTouchpadTouched.text = controllerState.TouchpadTouched.ToString();
160 LeftInfoTextTouchpadPosition.text = controllerState.TouchpadPosition.ToString();
162 else if (controllerState.Handedness.Equals(InteractionSourceHandedness.Right))
164 RightInfoTextPointerPosition.text = controllerState.PointerPosition.ToString();
165 RightInfoTextPointerRotation.text = controllerState.PointerRotation.ToString();
166 RightInfoTextGripPosition.text = controllerState.GripPosition.ToString();
167 RightInfoTextGripRotation.text = controllerState.GripRotation.ToString();
168 RightInfoTextGripGrasped.text = controllerState.Grasped.ToString();
169 RightInfoTextMenuPressed.text = controllerState.MenuPressed.ToString();
170 RightInfoTextTriggerPressed.text = controllerState.SelectPressed.ToString();
171 RightInfoTextTriggerPressedAmount.text = controllerState.SelectPressedAmount.ToString();
172 RightInfoTextThumbstickPressed.text = controllerState.ThumbstickPressed.ToString();
173 RightInfoTextThumbstickPosition.text = controllerState.ThumbstickPosition.ToString();
174 RightInfoTextTouchpadPressed.text = controllerState.TouchpadPressed.ToString();
175 RightInfoTextTouchpadTouched.text = controllerState.TouchpadTouched.ToString();
176 RightInfoTextTouchpadPosition.text = controllerState.TouchpadPosition.ToString();
180 return toReturn.Substring(0, Math.Max(0, toReturn.Length - 2));