ControllerHintsExample.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Demonstrates the use of the controller hint system
  4. //
  5. //=============================================================================
  6. using UnityEngine;
  7. using System.Collections;
  8. using Valve.VR;
  9. namespace Valve.VR.InteractionSystem.Sample
  10. {
  11. //-------------------------------------------------------------------------
  12. public class ControllerHintsExample : MonoBehaviour
  13. {
  14. private Coroutine buttonHintCoroutine;
  15. private Coroutine textHintCoroutine;
  16. //-------------------------------------------------
  17. public void ShowButtonHints( Hand hand )
  18. {
  19. if ( buttonHintCoroutine != null )
  20. {
  21. StopCoroutine( buttonHintCoroutine );
  22. }
  23. buttonHintCoroutine = StartCoroutine( TestButtonHints( hand ) );
  24. }
  25. //-------------------------------------------------
  26. public void ShowTextHints( Hand hand )
  27. {
  28. if ( textHintCoroutine != null )
  29. {
  30. StopCoroutine( textHintCoroutine );
  31. }
  32. textHintCoroutine = StartCoroutine( TestTextHints( hand ) );
  33. }
  34. //-------------------------------------------------
  35. public void DisableHints()
  36. {
  37. if ( buttonHintCoroutine != null )
  38. {
  39. StopCoroutine( buttonHintCoroutine );
  40. buttonHintCoroutine = null;
  41. }
  42. if ( textHintCoroutine != null )
  43. {
  44. StopCoroutine( textHintCoroutine );
  45. textHintCoroutine = null;
  46. }
  47. foreach ( Hand hand in Player.instance.hands )
  48. {
  49. ControllerButtonHints.HideAllButtonHints( hand );
  50. ControllerButtonHints.HideAllTextHints( hand );
  51. }
  52. }
  53. //-------------------------------------------------
  54. // Cycles through all the button hints on the controller
  55. //-------------------------------------------------
  56. private IEnumerator TestButtonHints( Hand hand )
  57. {
  58. ControllerButtonHints.HideAllButtonHints( hand );
  59. while ( true )
  60. {
  61. for (int actionIndex = 0; actionIndex < SteamVR_Input.actionsIn.Length; actionIndex++)
  62. {
  63. ISteamVR_Action_In action = SteamVR_Input.actionsIn[actionIndex];
  64. if (action.GetActive(hand.handType))
  65. {
  66. ControllerButtonHints.ShowButtonHint(hand, action);
  67. yield return new WaitForSeconds(1.0f);
  68. ControllerButtonHints.HideButtonHint(hand, action);
  69. yield return new WaitForSeconds(0.5f);
  70. }
  71. yield return null;
  72. }
  73. ControllerButtonHints.HideAllButtonHints( hand );
  74. yield return new WaitForSeconds( 1.0f );
  75. }
  76. }
  77. //-------------------------------------------------
  78. // Cycles through all the text hints on the controller
  79. //-------------------------------------------------
  80. private IEnumerator TestTextHints( Hand hand )
  81. {
  82. ControllerButtonHints.HideAllTextHints( hand );
  83. while ( true )
  84. {
  85. for (int actionIndex = 0; actionIndex < SteamVR_Input.actionsIn.Length; actionIndex++)
  86. {
  87. ISteamVR_Action_In action = SteamVR_Input.actionsIn[actionIndex];
  88. if (action.GetActive(hand.handType))
  89. {
  90. ControllerButtonHints.ShowTextHint(hand, action, action.GetShortName());
  91. yield return new WaitForSeconds(3.0f);
  92. ControllerButtonHints.HideTextHint(hand, action);
  93. yield return new WaitForSeconds(0.5f);
  94. }
  95. yield return null;
  96. }
  97. ControllerButtonHints.HideAllTextHints(hand);
  98. yield return new WaitForSeconds(3.0f);
  99. }
  100. }
  101. }
  102. }