OSEventSystem.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. using EZXR.Glass.Inputs;
  2. using EZXR.Glass.MiraCast;
  3. using EZXR.Glass.Recording;
  4. using EZXR.Glass.SixDof;
  5. using System;
  6. using System.Collections;
  7. using UnityEngine;
  8. namespace EZXR.Glass.Core
  9. {
  10. [ScriptExecutionOrder(-29)]
  11. public class OSEventSystem : MonoBehaviour
  12. {
  13. public static event Action OnInstructionsReceived_EZXRServerResetEnd;
  14. /// <summary>
  15. /// 系统进入待机模式的时候会触发
  16. /// </summary>
  17. public static event Action OnSystemEnterStandbyMode;
  18. public bool enableSystemUI = true;
  19. // Start is called before the first frame update
  20. void Awake()
  21. {
  22. BroadcastReceiver.Instance.Register("Instructions", "android.sunny.action.SCREEN");
  23. BroadcastReceiver.Instance.RegisterCallback("SetSDKInputState", OnReceive_SetSDKInputState, BroadcastReceiver.DataType.String);
  24. BroadcastReceiver.Instance.RegisterCallback("SetSDKRayState", OnReceive_RayState, BroadcastReceiver.DataType.String);
  25. BroadcastReceiver.Instance.RegisterCallback("Miracast", OnReceive_MiracastInstructions, BroadcastReceiver.DataType.String);
  26. BroadcastReceiver.Instance.RegisterCallback("Recording", OnReceive_RecordingInstructions, BroadcastReceiver.DataType.String);
  27. BroadcastReceiver.Instance.RegisterCallback("EZXRServerReset", OnReceiveReset, BroadcastReceiver.DataType.String);
  28. BroadcastReceiver.Instance.RegisterCallback("SetSDKDynamicControllerType", OnReceive_SetSDKDynamicControllerType, BroadcastReceiver.DataType.String);
  29. BroadcastReceiver.Instance.RegisterCallback("SCREEN_STATE", OnReceive_SCREEN_STATE, BroadcastReceiver.DataType.Int);
  30. SetSystemInteractionState(enableSystemUI);
  31. }
  32. private void OnApplicationPause(bool pause)
  33. {
  34. if (!pause)
  35. {
  36. OnReceive_SetSDKInputState("True");
  37. }
  38. }
  39. /// <summary>
  40. /// Receive from System, set hand visibility
  41. /// </summary>
  42. /// <param name="data">true for show</param>
  43. private void OnReceive_SetSDKInputState(string data)
  44. {
  45. Debug.Log("UnityBroadcastReceiver OnReceive_SetSDKInputState: " + data);
  46. bool enabled;
  47. if (bool.TryParse(data, out enabled))
  48. {
  49. if (InputSystem.Instance != null)
  50. {
  51. InputSystem.Instance.SetActive(enabled);
  52. }
  53. }
  54. }
  55. /// <summary>
  56. /// Receive from System, set RaycastInteraction status
  57. /// </summary>
  58. /// <param name="data"></param>
  59. private void OnReceive_RayState(string data)
  60. {
  61. bool enable = bool.Parse(data);
  62. Debug.Log("UnityBroadcastReceiver OnReceive_RayState: " + data);
  63. if (InputSystem.leftHand != null)
  64. {
  65. ARHandManager.leftHand.RaycastInteraction = enable;
  66. HandleControllerManager.leftHand.RaycastInteraction = enable;
  67. //InputSystem.leftHand.RaycastInteraction = enable;
  68. }
  69. if (InputSystem.rightHand != null)
  70. {
  71. ARHandManager.rightHand.RaycastInteraction = enable;
  72. HandleControllerManager.rightHand.RaycastInteraction = enable;
  73. //InputSystem.rightHand.RaycastInteraction = enable;
  74. }
  75. //if (UGUIHandler.Instance != null)
  76. //{
  77. // UGUIHandler.Instance.gameObject.SetActive(enable);
  78. //}
  79. }
  80. /// <summary>
  81. /// Receive from System, Miracast instructions
  82. /// </summary>
  83. /// <param name="data"></param>
  84. void OnReceive_MiracastInstructions(string data)
  85. {
  86. Debug.Log("UnityBroadcastReceiver OnReceive_MiracastInstructions: " + data);
  87. if (!string.IsNullOrEmpty(data))
  88. {
  89. string[] parameters = data.Split('^');
  90. switch (parameters[0])
  91. {
  92. case "Start":
  93. StartCoroutine(TryStartServer(parameters[1]));
  94. break;
  95. case "Stop":
  96. if (MiracastManager.Instance != null && MiracastManager.Instance.isMiracasting)
  97. {
  98. MiracastManager.Instance.Stop();
  99. }
  100. break;
  101. }
  102. }
  103. }
  104. IEnumerator TryStartServer(string serverIP)
  105. {
  106. if (MiracastManager.Instance != null)
  107. {
  108. MiracastManager.Instance.Stop();
  109. //Destroy(MiracastManager.Instance.gameObject);
  110. Debug.Log("TryStartServer: 0");
  111. }
  112. yield return new WaitForSeconds(2);
  113. GameObject miracastManagerObj = Instantiate(ResourcesManager.Load<GameObject>("MiracastManager"));
  114. Debug.Log("TryStartServer: 1");
  115. GameObject cameraRigObj = HMDPoseTracker.Instance.gameObject;
  116. if (cameraRigObj != null)
  117. {
  118. if (cameraRigObj.transform.parent != null && cameraRigObj.name != "XRMan")
  119. {
  120. miracastManagerObj.transform.parent = cameraRigObj.transform.parent;
  121. miracastManagerObj.transform.localPosition = Vector3.zero;
  122. miracastManagerObj.transform.localRotation = Quaternion.identity;
  123. }
  124. }
  125. if (MiracastManager.Instance != null)
  126. {
  127. Debug.Log("TryStartServer: 2");
  128. MiracastManager.Instance.Run(serverIP);
  129. Debug.Log("TryStartServer: 3");
  130. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "MiracastStatus", "Started");
  131. Debug.Log("TryStartServer: 4");
  132. }
  133. }
  134. /// <summary>
  135. /// Receive from System, Recording instructions
  136. /// </summary>
  137. /// <param name="data"></param>
  138. private void OnReceive_RecordingInstructions(string data)
  139. {
  140. Debug.Log("UnityBroadcastReceiver OnReceive_RecordingInstructions: " + data);
  141. switch (data)
  142. {
  143. case "recordingStart":
  144. if (RecordingManager.Instance != null)
  145. {
  146. Destroy(RecordingManager.Instance.gameObject);
  147. }
  148. StartCoroutine(tryStartRecording());
  149. break;
  150. case "recordingStop":
  151. if (RecordingManager.Instance != null && RecordingManager.Instance.isRecording)
  152. {
  153. RecordingManager.Instance.HandleRecordingClose();
  154. }
  155. break;
  156. case "screenshotStart":
  157. if (RecordingManager.Instance != null)
  158. {
  159. Destroy(RecordingManager.Instance.gameObject);
  160. }
  161. StartCoroutine(tryStartScreenShot());
  162. break;
  163. }
  164. }
  165. private IEnumerator tryStartRecording()
  166. {
  167. yield return new WaitForSeconds(2);
  168. GameObject recordingManagerObj = Instantiate(ResourcesManager.Load<GameObject>("RecordingManager"));
  169. GameObject cameraRigObj = HMDPoseTracker.Instance.gameObject;
  170. if (cameraRigObj != null)
  171. {
  172. if (cameraRigObj.transform.parent != null && cameraRigObj.name != "XRMan")
  173. {
  174. recordingManagerObj.transform.parent = cameraRigObj.transform.parent;
  175. recordingManagerObj.transform.localPosition = Vector3.zero;
  176. recordingManagerObj.transform.localRotation = Quaternion.identity;
  177. }
  178. }
  179. if (RecordingManager.Instance != null)
  180. RecordingManager.Instance.HandleRecordingOpen();
  181. }
  182. private IEnumerator tryStartScreenShot()
  183. {
  184. yield return new WaitForSeconds(2);
  185. GameObject recordingManagerObj = Instantiate(ResourcesManager.Load<GameObject>("RecordingManager"));
  186. GameObject cameraRigObj = HMDPoseTracker.Instance.gameObject;
  187. if (cameraRigObj != null)
  188. {
  189. if (cameraRigObj.transform.parent != null && cameraRigObj.name != "XRMan")
  190. {
  191. recordingManagerObj.transform.parent = cameraRigObj.transform.parent;
  192. recordingManagerObj.transform.localPosition = Vector3.zero;
  193. recordingManagerObj.transform.localRotation = Quaternion.identity;
  194. }
  195. }
  196. if (RecordingManager.Instance != null)
  197. RecordingManager.Instance.HandleScreenCapture();
  198. }
  199. /// <summary>
  200. ///
  201. /// </summary>
  202. /// <param name="data">Start/End</param>
  203. private void OnReceiveReset(string data)
  204. {
  205. Debug.Log("UnityBroadcastReceiver OnReceiveReset: OnInstructionsReceived_Reset + " + data);
  206. if (data == "End")
  207. {
  208. if (OnInstructionsReceived_EZXRServerResetEnd != null)
  209. {
  210. OnInstructionsReceived_EZXRServerResetEnd();
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 接收系统广播实现打开SystemUI
  216. /// </summary>
  217. /// <param name="obj">1为进入待机,2为退出待机(收不到,不要用)</param>
  218. private void OnReceive_SCREEN_STATE(string obj)
  219. {
  220. Debug.Log("OnReceive_SCREEN_STATE: " + obj);
  221. if (OnSystemEnterStandbyMode != null)
  222. {
  223. OnSystemEnterStandbyMode();
  224. }
  225. }
  226. /// <summary>
  227. /// 设置系统级交互的状态,包括:食指指尖Home、手腕菜单、系统级手的显示状态
  228. /// </summary>
  229. /// <param name="state">false表示禁用系统级交互</param>
  230. public static void SetSystemInteractionState(bool state)
  231. {
  232. Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "SetSystemHandState", state.ToString());
  233. }
  234. /// <summary>
  235. ///
  236. /// </summary>
  237. /// <param name="data">0是head,1是hand,2是controllers</param>
  238. private void OnReceive_SetSDKDynamicControllerType(string data)
  239. {
  240. Debug.Log("UnityBroadcastReceiver OnReceiveReset: OnReceive_SetSDKDynamicControllerType + " + data);
  241. ControllerType controllerType = (ControllerType)int.Parse(data);
  242. InputSystem.Instance.InitDynamicMode(controllerType);
  243. }
  244. }
  245. }