CUI_ViveButtonState.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEngine;
  2. using System.Collections;
  3. using UnityEngine.UI;
  4. namespace CurvedUI {
  5. /// <summary>
  6. /// This script show you how to access the state of any button on Vive Controller via CurvedUI scripts. We use right controller as an example
  7. /// </summary>
  8. public class CUI_ViveButtonState : MonoBehaviour
  9. {
  10. enum ViveButton
  11. {
  12. Trigger,
  13. TouchpadTouch,
  14. TouchpadPress,
  15. Grip,
  16. Menu,
  17. }
  18. #pragma warning disable 414 // this is just so we wont get "unused variable" code warnings when compiling without Vive.
  19. [SerializeField]
  20. Color ActiveColor = Color.green;
  21. [SerializeField]
  22. Color InActiveColor = Color.gray;
  23. [SerializeField] ViveButton ShowStateFor = ViveButton.Trigger;
  24. #pragma warning restore 414
  25. #if CURVEDUI_STEAMVR_LEGACY
  26. // Update is called once per frame
  27. void Update()
  28. {
  29. if(CurvedUIInputModule.Right == null)
  30. {
  31. Debug.LogError("Right controller not found - it may be off");
  32. return;
  33. }
  34. bool pressed = false;
  35. switch (ShowStateFor)
  36. {
  37. case ViveButton.Trigger:
  38. {
  39. pressed = GetUsedController().IsTriggerPressed;
  40. break;
  41. }
  42. case ViveButton.TouchpadPress:
  43. {
  44. pressed = GetUsedController().IsTouchpadPressed;
  45. break;
  46. }
  47. case ViveButton.TouchpadTouch:
  48. {
  49. pressed = GetUsedController().IsTouchpadTouched;
  50. break;
  51. }
  52. case ViveButton.Grip:
  53. {
  54. pressed = GetUsedController().IsGripPressed;
  55. break;
  56. }
  57. case ViveButton.Menu:
  58. {
  59. pressed = GetUsedController().IsApplicationMenuPressed;
  60. break;
  61. }
  62. }
  63. this.GetComponentInChildren<Image>().color = pressed ? ActiveColor : InActiveColor;
  64. }
  65. CurvedUIViveController GetUsedController()
  66. {
  67. return CurvedUIInputModule.Instance.UsedHand == CurvedUIInputModule.Hand.Right ? CurvedUIInputModule.Right : CurvedUIInputModule.Left;
  68. }
  69. #endif
  70. }
  71. }