MoveChange.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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_GSXR_Module_InputSystem_KS.GSXR_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. public void zhijiekaishi()
  86. {
  87. ARSpace.isFind = true;
  88. islixian = true;
  89. }
  90. public static bool isCx;
  91. public static bool islixian;
  92. public GameObject saomiao;
  93. void Update()
  94. {
  95. if (ARSpace.isFind)
  96. {
  97. if (!isCx)
  98. {
  99. saomiao.SetActive(false);
  100. txtTip.SetActive(!islixian);
  101. txtTip.GetComponent<ShowTipTime>().TimeFloat = 3;
  102. txtTip.GetComponent<ShowTipTime>().t = 0;
  103. txtTip.GetComponentInChildren<Text>().text = "空间定位成功";
  104. XRLocalizer.Instance.gameObject.SetActive(false);
  105. }
  106. isCx = true;
  107. }
  108. else
  109. {
  110. txtTip.SetActive(false);
  111. saomiao.SetActive(true);
  112. // txtTip.GetComponentInChildren<Text>().text = "请扫描周围环境以进行空间定位";
  113. }
  114. if (isDown && SetSceneActive.Instance.active&&!islixian)
  115. {
  116. if(!IsRot)
  117. {
  118. goRefrence.transform.position = OpenXRCamera.Instance.head.position - TempVector3;
  119. goRefrence.transform.position = new Vector3(goRefrence.transform.position.x, y, goRefrence.transform.position.z);
  120. }
  121. else
  122. {
  123. if(DeviceType.type != "A01" && DeviceType.type != "Phone")
  124. {
  125. Debug.Log("正在旋轉======》");
  126. v3.y = ControllerTracker_Y - ControllerTracker.eulerAngles.y;
  127. goRefrence.transform.localEulerAngles += v3;
  128. ControllerTracker_Y = ControllerTracker.eulerAngles.y;
  129. }else
  130. {
  131. goRefrence.transform.localEulerAngles = TempVector3Rot - ControllerTracker.eulerAngles;
  132. goRefrence.transform.localEulerAngles = new Vector3(0, goRefrence.transform.eulerAngles.y, 0);
  133. }
  134. /*
  135. goRefrence.transform.localEulerAngles = ControllerTracker.localEulerAngles - TempVector3;
  136. goRefrence.transform.localEulerAngles = new Vector3(0, goRefrence.transform.localEulerAngles.y, 0);
  137. */
  138. }
  139. //moveGame.transform.eulerAngles = new Vector3(0, moveGame.transform.eulerAngles.y, 0);
  140. }
  141. if (Input.GetKeyDown(KeyCode.I))
  142. {
  143. ARSpace.isFind = true;
  144. }
  145. ControllerTracker_Y = ControllerTracker.eulerAngles.y;
  146. }
  147. private Vector3 v3 = Vector3.zero; // 用于存放手柄上次与下次的差值
  148. private float ControllerTracker_Y = 0; // 上次手柄的旋转Y值
  149. private bool IsRot;
  150. private Vector3 TempVector3;
  151. private Vector3 TempVector3Rot;
  152. public GameObject goRefrence // 参照物
  153. {
  154. get =>SceneIOCContainer.Instance.Pull("goRefrence")as GameObject;
  155. }
  156. public GameObject txtTip;
  157. private void RotToggleOnValueChanged(bool isOn)
  158. {
  159. Debug.Log("选择了RotToggleOnValueChanged");
  160. txtTip.SetActive(true);
  161. if (isOn)
  162. {
  163. txtTip.GetComponentInChildren<Text>().text = "如需调整场景旋转角度,请按住手柄拖动场景\n场景将以锚点为圆心进行旋转";
  164. IsRot = true;
  165. }
  166. else
  167. {
  168. txtTip.GetComponentInChildren<Text>().text = "如需移动场景,请按住手柄拖动场景并走动\n场景将跟随您的脚步进行整体移动";
  169. IsRot = false;
  170. }
  171. }
  172. }