MoveChange.cs 6.3 KB

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