MoveChange.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Immersal.AR;
  2. using SC.XR.Unity.Module_InputSystem;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class MoveChange : MonoBehaviour
  6. {
  7. public Transform ControllerTracker; // 手柄
  8. private GameObject mesh_test => SceneIOCContainer.Instance.Pull("mesh_test") as GameObject;
  9. private Transform ARSpaceForAll=>(SceneIOCContainer.Instance.Pull("ARSpaceForAll")as GameObject).transform;
  10. [SerializeField] private Toggle RotToggle;
  11. private bool isDown;
  12. private float y=> SetSceneActive.Instance.goReference_y;
  13. void Start()
  14. {
  15. ControllerTracker = GameObject.Find("SDKSystem").transform.Find("NRInput/Right/ControllerTracker");
  16. DispatcherBase.KeyDownDelegateRegister(keyDownEventDelegate);
  17. DispatcherBase.KeyUpDelegateRegister(keyUpEventDelegate);
  18. RotToggle.onValueChanged.AddListener(RotToggleOnValueChanged);
  19. IsRot = RotToggle.isOn;
  20. }
  21. private void keyUpEventDelegate(InputKeyCode keyCode, InputDevicePartBase part)
  22. {
  23. if (keyCode != InputKeyCode.Enter || goRefrence==null)
  24. {
  25. return;
  26. }
  27. Debug.Log("keyDownEventDelegate===>" + keyCode);
  28. mesh_test.transform.parent = ARSpaceForAll;
  29. goRefrence.transform.parent = ARSpaceForAll;
  30. isDown = false;
  31. }
  32. private void keyDownEventDelegate(InputKeyCode keyCode, InputDevicePartBase part)
  33. {
  34. if(keyCode != InputKeyCode.Enter)
  35. {
  36. return;
  37. }
  38. if (!SetSceneActive.Instance.active|| goRefrence==null)
  39. return;
  40. if(!IsRot)
  41. {
  42. TempVector3 = OpenXRCamera.Instance.head.position-goRefrence.transform.position;
  43. }
  44. mesh_test.transform.parent = goRefrence.transform;
  45. /*
  46. Debug.Log("keyUpEventDelegate===>" + keyCode);
  47. #if UNITY_EDITOR
  48. //moveGame.transform.parent = OpenXRCamera.Instance.head;
  49. #else
  50. switch (DeviceType.type)
  51. {
  52. case "Nreal":
  53. moveGame.transform.parent = API_GSXR_Module_InputSystem_KS.GSXR_KSRight.transform;// OpenXRCamera.Instance.head;
  54. break;
  55. case "A01":
  56. moveGame.transform.parent = OpenXRCamera.Instance.head;
  57. break;
  58. case "Phone":
  59. moveGame.transform.parent = OpenXRCamera.Instance.head;
  60. break;
  61. case "Rhinox":
  62. moveGame.transform.parent = API_GSXR_Module_InputSystem_KS.GSXR_KSRight.inputDeviceKSPartUI.transform;
  63. break;
  64. default:
  65. moveGame.transform.parent = API_GSXR_Module_InputSystem_KS.GSXR_KSRight.transform;// OpenXRCamera.Instance.head;
  66. break;
  67. }
  68. #endif
  69. */
  70. isDown = true;
  71. }
  72. bool isCx;
  73. void Update()
  74. {
  75. if (ARSpace.isFind)
  76. {
  77. if (!isCx)
  78. {
  79. txtTip.SetActive(true);
  80. txtTip.GetComponent<ShowTipTime>().TimeFloat = 3;
  81. txtTip.GetComponent<ShowTipTime>().t = 0;
  82. txtTip.GetComponentInChildren<Text>().text = "空间定位成功";
  83. }
  84. isCx = true;
  85. }
  86. else
  87. {
  88. txtTip.SetActive(true);
  89. txtTip.GetComponentInChildren<Text>().text = "请扫描周围环境以进行空间定位";
  90. }
  91. if (isDown && SetSceneActive.Instance.active)
  92. {
  93. if(!IsRot)
  94. {
  95. goRefrence.transform.position = OpenXRCamera.Instance.head.position - TempVector3;
  96. goRefrence.transform.position = new Vector3(goRefrence.transform.position.x, y, goRefrence.transform.position.z);
  97. }
  98. else
  99. {
  100. v3.y = ControllerTracker_Y - ControllerTracker.localEulerAngles.y;
  101. goRefrence.transform.localEulerAngles +=v3;
  102. ControllerTracker_Y = ControllerTracker.localEulerAngles.y;
  103. /*
  104. goRefrence.transform.localEulerAngles = ControllerTracker.localEulerAngles - TempVector3;
  105. goRefrence.transform.localEulerAngles = new Vector3(0, goRefrence.transform.localEulerAngles.y, 0);
  106. */
  107. }
  108. //moveGame.transform.eulerAngles = new Vector3(0, moveGame.transform.eulerAngles.y, 0);
  109. }
  110. if (Input.GetKeyDown(KeyCode.I))
  111. {
  112. ARSpace.isFind = true;
  113. }
  114. ControllerTracker_Y = ControllerTracker.localEulerAngles.y;
  115. }
  116. private Vector3 v3 = Vector3.zero; // 用于存放手柄上次与下次的差值
  117. private float ControllerTracker_Y = 0; // 上次手柄的旋转Y值
  118. private bool IsRot;
  119. private Vector3 TempVector3;
  120. public GameObject goRefrence // 参照物
  121. {
  122. get =>SceneIOCContainer.Instance.Pull("goRefrence")as GameObject;
  123. }
  124. public GameObject txtTip;
  125. private void RotToggleOnValueChanged(bool isOn)
  126. {
  127. Debug.Log("选择了RotToggleOnValueChanged");
  128. txtTip.SetActive(true);
  129. if (isOn)
  130. {
  131. txtTip.GetComponentInChildren<Text>().text = "如需调整场景旋转角度,请按住手柄拖动场景\n场景将以锚点为圆心进行旋转";
  132. IsRot = true;
  133. }
  134. else
  135. {
  136. txtTip.GetComponentInChildren<Text>().text = "如需移动场景,请按住手柄拖动场景并走动\n场景将跟随您的脚步进行整体移动";
  137. IsRot = false;
  138. }
  139. }
  140. }