7 #if UNITY_2017_2_OR_NEWER 21 [Tooltip(
"The Cursor object the HandGuidanceIndicator will be positioned around.")]
24 [Tooltip(
"GameObject to display when your hand is about to lose tracking.")]
30 [Tooltip(
"When to start showing the Hand Guidance Indicator. 1 is out of view, 0 is centered in view.")]
31 public float HandGuidanceThreshold = 0.5f;
34 private GameObject handGuidanceIndicatorGameObject;
36 private Quaternion defaultHandGuidanceRotation;
38 private uint? currentlyTrackedHand;
40 protected override void Awake()
43 if (HandGuidanceIndicator == null)
45 Debug.LogError(
"Please include a GameObject for the Hand Guidance Indicator.");
50 Debug.LogError(
"Please include a GameObject for the Cursor to display the indicator around.");
53 if (HandGuidanceIndicator != null)
57 defaultHandGuidanceRotation = HandGuidanceIndicator.transform.rotation;
61 handGuidanceIndicatorGameObject = Instantiate(HandGuidanceIndicator);
62 handGuidanceIndicatorGameObject.SetActive(
false);
66 #if UNITY_2017_2_OR_NEWER 67 InteractionManager.InteractionSourceLost += InteractionManager_InteractionSourceLost;
68 InteractionManager.InteractionSourceUpdated += InteractionManager_InteractionSourceUpdated;
69 InteractionManager.InteractionSourceReleased += InteractionManager_InteractionSourceReleased;
71 InteractionManager.SourceLost += InteractionManager_SourceLost;
72 InteractionManager.SourceUpdated += InteractionManager_SourceUpdated;
73 InteractionManager.SourceReleased += InteractionManager_SourceReleased;
77 private void ShowHandGuidanceIndicator(InteractionSourceState hand)
79 if (!currentlyTrackedHand.HasValue)
85 if (handGuidanceIndicatorGameObject != null)
89 GetIndicatorPositionAndRotation(hand, out position, out rotation);
91 handGuidanceIndicatorGameObject.transform.position = position;
92 handGuidanceIndicatorGameObject.transform.rotation = rotation * defaultHandGuidanceRotation;
93 handGuidanceIndicatorGameObject.SetActive(
true);
97 private void HideHandGuidanceIndicator(InteractionSourceState hand)
99 if (!currentlyTrackedHand.HasValue)
104 if (handGuidanceIndicatorGameObject != null)
106 handGuidanceIndicatorGameObject.SetActive(
false);
110 private void GetIndicatorPositionAndRotation(InteractionSourceState hand, out Vector3 position, out Quaternion rotation)
114 const float maxDistanceFromCenter = 0.3f;
115 float distanceFromCenter = (float)(hand.properties.sourceLossRisk * maxDistanceFromCenter);
118 position = Cursor.transform.position - hand.properties.sourceLossMitigationDirection * distanceFromCenter;
119 rotation = Quaternion.LookRotation(
CameraCache.
Main.transform.forward, hand.properties.sourceLossMitigationDirection);
122 #if UNITY_2017_2_OR_NEWER 123 private void InteractionManager_InteractionSourceUpdated(InteractionSourceUpdatedEventArgs obj)
125 if (obj.state.source.kind == InteractionSourceKind.Hand)
127 InteractionSourceState hand = obj.state;
130 if (!hand.anyPressed)
136 if (!currentlyTrackedHand.HasValue)
138 currentlyTrackedHand = hand.source.id;
140 else if (currentlyTrackedHand.Value != hand.source.id)
147 if (hand.properties.sourceLossRisk > HandGuidanceThreshold)
149 ShowHandGuidanceIndicator(hand);
153 HideHandGuidanceIndicator(hand);
158 private void InteractionManager_InteractionSourceReleased(InteractionSourceReleasedEventArgs obj)
160 if (obj.state.source.kind == InteractionSourceKind.Hand)
163 RemoveTrackedHand(obj.state);
167 private void InteractionManager_InteractionSourceLost(InteractionSourceLostEventArgs obj)
169 if (obj.state.source.kind == InteractionSourceKind.Hand)
172 RemoveTrackedHand(obj.state);
176 private void RemoveTrackedHand(InteractionSourceState hand)
179 if (currentlyTrackedHand.HasValue && currentlyTrackedHand.Value == hand.source.id)
182 handGuidanceIndicatorGameObject.SetActive(
false);
183 currentlyTrackedHand = null;
187 protected override void OnDestroy()
189 InteractionManager.InteractionSourceLost -= InteractionManager_InteractionSourceLost;
190 InteractionManager.InteractionSourceUpdated -= InteractionManager_InteractionSourceUpdated;
191 InteractionManager.InteractionSourceReleased -= InteractionManager_InteractionSourceReleased;
196 private void InteractionManager_SourceUpdated(InteractionSourceState hand)
205 if (!currentlyTrackedHand.HasValue)
207 currentlyTrackedHand = hand.source.id;
209 else if (currentlyTrackedHand.Value != hand.source.id)
216 if (hand.properties.sourceLossRisk > HandGuidanceThreshold)
218 ShowHandGuidanceIndicator(hand);
222 HideHandGuidanceIndicator(hand);
226 private void InteractionManager_SourceReleased(InteractionSourceState hand)
229 RemoveTrackedHand(hand);
232 private void InteractionManager_SourceLost(InteractionSourceState hand)
235 RemoveTrackedHand(hand);
238 private void RemoveTrackedHand(InteractionSourceState hand)
241 if (currentlyTrackedHand.HasValue && currentlyTrackedHand.Value == hand.source.id)
244 handGuidanceIndicatorGameObject.SetActive(
false);
245 currentlyTrackedHand = null;
249 protected override void OnDestroy()
251 InteractionManager.SourceLost -= InteractionManager_SourceLost;
252 InteractionManager.SourceUpdated -= InteractionManager_SourceUpdated;
253 InteractionManager.SourceReleased -= InteractionManager_SourceReleased;