ControllerHoverHighlight.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Highlights the controller when hovering over interactables
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. namespace Valve.VR.InteractionSystem
  9. {
  10. //-------------------------------------------------------------------------
  11. public class ControllerHoverHighlight : MonoBehaviour
  12. {
  13. public Material highLightMaterial;
  14. public bool fireHapticsOnHightlight = true;
  15. protected Hand hand;
  16. protected RenderModel renderModel;
  17. protected SteamVR_Events.Action renderModelLoadedAction;
  18. protected void Awake()
  19. {
  20. hand = GetComponentInParent<Hand>();
  21. }
  22. protected void OnHandInitialized(int deviceIndex)
  23. {
  24. GameObject renderModelGameObject = GameObject.Instantiate(hand.renderModelPrefab);
  25. renderModelGameObject.transform.parent = this.transform;
  26. renderModelGameObject.transform.localPosition = Vector3.zero;
  27. renderModelGameObject.transform.localRotation = Quaternion.identity;
  28. renderModelGameObject.transform.localScale = hand.renderModelPrefab.transform.localScale;
  29. renderModel = renderModelGameObject.GetComponent<RenderModel>();
  30. renderModel.SetInputSource(hand.handType);
  31. renderModel.OnHandInitialized(deviceIndex);
  32. renderModel.SetMaterial(highLightMaterial);
  33. hand.SetHoverRenderModel(renderModel);
  34. renderModel.onControllerLoaded += RenderModel_onControllerLoaded;
  35. renderModel.Hide();
  36. }
  37. private void RenderModel_onControllerLoaded()
  38. {
  39. renderModel.Hide();
  40. }
  41. //-------------------------------------------------
  42. protected void OnParentHandHoverBegin(Interactable other)
  43. {
  44. if (!this.isActiveAndEnabled)
  45. {
  46. return;
  47. }
  48. if (other.transform.parent != transform.parent)
  49. {
  50. ShowHighlight();
  51. }
  52. }
  53. //-------------------------------------------------
  54. private void OnParentHandHoverEnd(Interactable other)
  55. {
  56. HideHighlight();
  57. }
  58. //-------------------------------------------------
  59. private void OnParentHandInputFocusAcquired()
  60. {
  61. if (!this.isActiveAndEnabled)
  62. {
  63. return;
  64. }
  65. if (hand.hoveringInteractable && hand.hoveringInteractable.transform.parent != transform.parent)
  66. {
  67. ShowHighlight();
  68. }
  69. }
  70. //-------------------------------------------------
  71. private void OnParentHandInputFocusLost()
  72. {
  73. HideHighlight();
  74. }
  75. //-------------------------------------------------
  76. public void ShowHighlight()
  77. {
  78. if (renderModel == null)
  79. {
  80. return;
  81. }
  82. if (fireHapticsOnHightlight)
  83. {
  84. hand.TriggerHapticPulse(500);
  85. }
  86. renderModel.Show();
  87. }
  88. //-------------------------------------------------
  89. public void HideHighlight()
  90. {
  91. if (renderModel == null)
  92. {
  93. return;
  94. }
  95. if (fireHapticsOnHightlight)
  96. {
  97. hand.TriggerHapticPulse(300);
  98. }
  99. renderModel.Hide();
  100. }
  101. }
  102. }