DragTest.cs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. using Nxr.Internal;
  3. using NibiruAxis;
  4. using NibiruTask;
  5. namespace NXR.Samples
  6. {
  7. public class DragTest : MonoBehaviour
  8. {
  9. public TextMesh statusTextMesh;
  10. NxrDragableItem dragableItem;
  11. bool IsSelected = false;
  12. private Transform parentTrans;
  13. public void OnGazeEnter()
  14. {
  15. IsSelected = true;
  16. statusTextMesh.text = "Selected";
  17. }
  18. public void OnGazeExit()
  19. {
  20. IsSelected = false;
  21. statusTextMesh.text = "UnSelected";
  22. if (dragableItem != null && dragableItem.IsDraging)
  23. {
  24. statusTextMesh.text = "Draging";
  25. }
  26. }
  27. public void OnGazeTrigger()
  28. {
  29. bool isDraging = dragableItem != null && dragableItem.IsDraging;
  30. if (!IsSelected && !isDraging) return;
  31. if(!isDraging)
  32. {
  33. NxrLaserPointer nxrLaserPointer = NxrPlayerCtrl.Instance.GetControllerLaser();
  34. if(nxrLaserPointer != null)
  35. {
  36. // 选中跟随
  37. if (dragableItem != null)
  38. {
  39. dragableItem.OnBeginDrag(nxrLaserPointer.transform);
  40. }
  41. } else
  42. {
  43. // 头部
  44. NxrReticle mReticle = NxrViewer.Instance.GetNxrReticle();
  45. if(dragableItem != null)
  46. {
  47. dragableItem.OnBeginDrag(mReticle.gameObject.transform.parent);
  48. }
  49. }
  50. } else
  51. {
  52. // 恢复
  53. if (dragableItem != null)
  54. {
  55. dragableItem.OnEndDrag(parentTrans);
  56. }
  57. }
  58. }
  59. // Start is called before the first frame update
  60. void Start()
  61. {
  62. dragableItem = GetComponent<NxrDragableItem>();
  63. parentTrans = gameObject.transform.parent;
  64. statusTextMesh.text = "UnSelected";
  65. }
  66. private void Update()
  67. {
  68. if (NxrInput.GetKeyDown(CKeyEvent.KEYCODE_DPAD_CENTER) ||
  69. NxrInput.GetControllerKeyDown(CKeyEvent.KEYCODE_CONTROLLER_TOUCHPAD))
  70. {
  71. OnGazeTrigger();
  72. }
  73. }
  74. }
  75. }