17 [Tooltip(
"Set this in the editor to an object with a component that implements IPointerSource to tell this cursor which pointer to follow. To set the pointer programmatically, set Pointer directly.")]
25 get {
return pointer; }
40 private bool pointerIsInputSourcePointer =
false;
45 [Header(
"Cursor Distance")]
46 [Tooltip(
"The minimum distance the cursor can be with nothing hit")]
47 public float MinCursorDistance = 1.0f;
52 [Tooltip(
"The maximum distance the cursor can be with nothing hit")]
53 public float DefaultCursorDistance = 2.0f;
58 [Tooltip(
"The distance from the hit surface to place the cursor")]
59 public float SurfaceCursorDistance = 0.02f;
62 [Tooltip(
"When lerping, use unscaled time. This is useful for games that have a pause mechanism or otherwise adjust the game timescale.")]
63 public bool UseUnscaledTime =
true;
68 public float PositionLerpTime = 0.01f;
73 public float ScaleLerpTime = 0.01f;
78 public float RotationLerpTime = 0.01f;
84 public float LookRotationBlend = 0.5f;
89 [Header(
"Transform References")]
94 get {
return transform.position; }
99 get {
return transform.rotation; }
102 public Vector3 LocalScale
104 get {
return transform.localScale; }
120 private uint visibleHandsCount = 0;
123 [Tooltip(
"Set this to specify if the Cursor should start visible or invisible in the scene.")]
124 private bool isVisible =
true;
129 private Vector3 targetPosition;
130 private Vector3 targetScale;
131 private Quaternion targetRotation;
137 private float originalDefaultCursorDistance;
142 public bool IsVisible
147 SetVisibility(isVisible);
151 #region MonoBehaviour Functions 155 originalDefaultCursorDistance = DefaultCursorDistance;
158 IsVisible = isVisible;
164 TryLoadPointerIfNeeded();
170 UpdateCursorTransform();
190 TargetedObject = null;
191 TargetedCursorModifier = null;
192 visibleHandsCount = 0;
193 IsHandVisible =
false;
199 UnregisterManagers();
211 IsHandVisible = visibleHandsCount > 0;
250 private void TryLoadPointerIfNeeded()
256 else if (LoadPointer != null)
262 Debug.LogErrorFormat(
"Load pointer object \"{0}\" is missing its {1} component.",
275 Pointer = pointingSource;
293 if (pointer == Pointer)
295 TargetedObject = newFocusedObject;
301 OnActiveModifier(newModifier);
311 TargetedCursorModifier = modifier;
320 GameObject newTargetedObject = focusDetails.
Object;
321 Vector3 lookForward = Vector3.forward;
324 targetScale = Vector3.one;
327 if (newTargetedObject == null)
329 TargetedObject = null;
330 TargetedCursorModifier = null;
332 if (pointerIsInputSourcePointer)
337 if (DefaultCursorDistance != distance)
339 DefaultCursorDistance = distance;
342 else if (DefaultCursorDistance != originalDefaultCursorDistance)
344 DefaultCursorDistance = originalDefaultCursorDistance;
349 targetRotation = lookForward.magnitude > 0 ? Quaternion.LookRotation(lookForward, Vector3.up) : transform.rotation;
354 TargetedObject = newTargetedObject;
356 if (TargetedCursorModifier != null)
358 TargetedCursorModifier.
GetModifiedTransform(
this, out targetPosition, out targetRotation, out targetScale);
365 float distanceToTarget = Vector3.Distance(Pointer.Rays[0].Origin, focusDetails.
Point);
367 targetPosition = focusDetails.
Point + (lookForward * SurfaceCursorDistance);
368 Vector3 lookRotation = Vector3.Slerp(focusDetails.
Normal, lookForward, LookRotationBlend);
369 targetRotation = Quaternion.LookRotation(lookRotation == Vector3.zero ? lookForward : lookRotation, Vector3.up);
373 float deltaTime = UseUnscaledTime
374 ? Time.unscaledDeltaTime
378 transform.position = Vector3.Lerp(transform.position, targetPosition, deltaTime / PositionLerpTime);
379 transform.localScale = Vector3.Lerp(transform.localScale, targetScale, deltaTime / ScaleLerpTime);
380 transform.rotation = Quaternion.Lerp(transform.rotation, targetRotation, deltaTime / RotationLerpTime);
388 if (PrimaryCursorVisual != null)
390 PrimaryCursorVisual.gameObject.SetActive(visible);
400 visibleHandsCount = 0;
401 IsHandVisible =
false;
420 if (Pointer != null && Pointer.OwnsInput(eventData))
422 IsInputSourceDown =
false;
432 if (Pointer != null && Pointer.OwnsInput(eventData))
434 IsInputSourceDown =
true;
454 if (Pointer != null && Pointer.OwnsInput(eventData))
457 IsHandVisible =
true;
468 if (Pointer != null && Pointer.OwnsInput(eventData))
471 if (visibleHandsCount == 0)
473 IsHandVisible =
false;
474 IsInputSourceDown =
false;
482 private void UpdateCursorState()
485 if (cursorState != newState)
487 OnCursorStateChange(newState);
498 if (IsInputSourceDown)