HandleControllerDemo.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using EZXR.Glass.Inputs;
  4. using UnityEngine;
  5. using UnityEngine.SceneManagement;
  6. using UnityEngine.UI;
  7. public class HandleControllerDemo : BaseGlassSDKDemo
  8. {
  9. public Text keyCodeText;
  10. public Text sliderValueText;
  11. public Slider sliderView;
  12. // Start is called before the first frame update
  13. void Start()
  14. {
  15. }
  16. // Update is called once per frame
  17. new void Update()
  18. {
  19. keyCodeText.text = string.Empty;
  20. if (InputSystem.CurrentActiveControllerType == ControllerType.Controllers)
  21. {
  22. keyCodeText.text += GetButtonInfo(HandType.Left);
  23. keyCodeText.text += GetButtonInfo(HandType.Right);
  24. }
  25. base.Update();
  26. }
  27. private string GetButtonInfo(HandType handType)
  28. {
  29. string text_info = string.Empty;
  30. string text_hand = handType == HandType.Left ? "左手柄" : "右手柄";
  31. if (HandleControllerManager.Instance.GetButton(handType, HandleKeyCode.Grid))
  32. text_info = $"检测到{text_hand}【侧握键】";
  33. if (HandleControllerManager.Instance.GetButton(handType, HandleKeyCode.Home))
  34. text_info = $"检测到{text_hand}【Home键】";
  35. if (HandleControllerManager.Instance.GetButton(handType, HandleKeyCode.Primary))
  36. text_info = $"检测到{text_hand}【{(handType == HandType.Left ? "X" : "A")}键】";
  37. if (HandleControllerManager.Instance.GetButton(handType, HandleKeyCode.Return))
  38. text_info = $"检测到{text_hand}【返回键】";
  39. if (HandleControllerManager.Instance.GetButton(handType, HandleKeyCode.Rocker))
  40. text_info = $"检测到{text_hand}【摇杆键】";
  41. if (HandleControllerManager.Instance.GetButton(handType, HandleKeyCode.Secondary))
  42. text_info = $"检测到{text_hand}【{(handType == HandType.Left ? "Y" : "B")}键】";
  43. if (HandleControllerManager.Instance.GetButton(handType, HandleKeyCode.Trigger))
  44. text_info = $"检测到{text_hand}【扳机键】";
  45. var axis = HandleControllerManager.Instance.GetAxis2D(handType);
  46. if (axis != Vector2.zero)
  47. {
  48. if (!string.IsNullOrEmpty(text_info)) text_info += "\n";
  49. text_info += $"{text_hand}({axis.x:F1}, {axis.y:F1})";
  50. }
  51. if (!string.IsNullOrEmpty(text_info)) text_info += "\n";
  52. return text_info;
  53. }
  54. public void OnSiliderChanged()
  55. {
  56. sliderValueText.text = sliderView.value.ToString();
  57. }
  58. public void OnVibrateHandle(int handType)
  59. {
  60. HandleControllerManager.Instance.VibrateHandle((HandType)handType, 6, 500);
  61. }
  62. }