MoveChange.cs 5.9 KB

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