123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
- using EZXR.Glass.Inputs;
- using EZXR.Glass.MiraCast;
- using EZXR.Glass.Recording;
- using EZXR.Glass.SixDof;
- using System;
- using System.Collections;
- using UnityEngine;
- namespace EZXR.Glass.Core
- {
- [ScriptExecutionOrder(-29)]
- public class OSEventSystem : MonoBehaviour
- {
- public static event Action OnInstructionsReceived_EZXRServerResetEnd;
- /// <summary>
- /// 系统进入待机模式的时候会触发
- /// </summary>
- public static event Action OnSystemEnterStandbyMode;
- public bool enableSystemUI = true;
- // Start is called before the first frame update
- void Awake()
- {
- BroadcastReceiver.Instance.Register("Instructions", "android.sunny.action.SCREEN");
- BroadcastReceiver.Instance.RegisterCallback("SetSDKInputState", OnReceive_SetSDKInputState, BroadcastReceiver.DataType.String);
- BroadcastReceiver.Instance.RegisterCallback("SetSDKRayState", OnReceive_RayState, BroadcastReceiver.DataType.String);
- BroadcastReceiver.Instance.RegisterCallback("Miracast", OnReceive_MiracastInstructions, BroadcastReceiver.DataType.String);
- BroadcastReceiver.Instance.RegisterCallback("Recording", OnReceive_RecordingInstructions, BroadcastReceiver.DataType.String);
- BroadcastReceiver.Instance.RegisterCallback("EZXRServerReset", OnReceiveReset, BroadcastReceiver.DataType.String);
- BroadcastReceiver.Instance.RegisterCallback("SetSDKDynamicControllerType", OnReceive_SetSDKDynamicControllerType, BroadcastReceiver.DataType.String);
- BroadcastReceiver.Instance.RegisterCallback("SCREEN_STATE", OnReceive_SCREEN_STATE, BroadcastReceiver.DataType.Int);
- SetSystemInteractionState(enableSystemUI);
- }
- private void OnApplicationPause(bool pause)
- {
- if (!pause)
- {
- OnReceive_SetSDKInputState("True");
- }
- }
- /// <summary>
- /// Receive from System, set hand visibility
- /// </summary>
- /// <param name="data">true for show</param>
- private void OnReceive_SetSDKInputState(string data)
- {
- Debug.Log("UnityBroadcastReceiver OnReceive_SetSDKInputState: " + data);
- bool enabled;
- if (bool.TryParse(data, out enabled))
- {
- if (InputSystem.Instance != null)
- {
- InputSystem.Instance.SetActive(enabled);
- }
- }
- }
- /// <summary>
- /// Receive from System, set RaycastInteraction status
- /// </summary>
- /// <param name="data"></param>
- private void OnReceive_RayState(string data)
- {
- bool enable = bool.Parse(data);
- Debug.Log("UnityBroadcastReceiver OnReceive_RayState: " + data);
- if (InputSystem.leftHand != null)
- {
- ARHandManager.leftHand.RaycastInteraction = enable;
- HandleControllerManager.leftHand.RaycastInteraction = enable;
- //InputSystem.leftHand.RaycastInteraction = enable;
- }
- if (InputSystem.rightHand != null)
- {
- ARHandManager.rightHand.RaycastInteraction = enable;
- HandleControllerManager.rightHand.RaycastInteraction = enable;
- //InputSystem.rightHand.RaycastInteraction = enable;
- }
- //if (UGUIHandler.Instance != null)
- //{
- // UGUIHandler.Instance.gameObject.SetActive(enable);
- //}
- }
- /// <summary>
- /// Receive from System, Miracast instructions
- /// </summary>
- /// <param name="data"></param>
- void OnReceive_MiracastInstructions(string data)
- {
- Debug.Log("UnityBroadcastReceiver OnReceive_MiracastInstructions: " + data);
- if (!string.IsNullOrEmpty(data))
- {
- string[] parameters = data.Split('^');
- switch (parameters[0])
- {
- case "Start":
- StartCoroutine(TryStartServer(parameters[1]));
- break;
- case "Stop":
- if (MiracastManager.Instance != null && MiracastManager.Instance.isMiracasting)
- {
- MiracastManager.Instance.Stop();
- }
- break;
- }
- }
- }
- IEnumerator TryStartServer(string serverIP)
- {
- if (MiracastManager.Instance != null)
- {
- MiracastManager.Instance.Stop();
- //Destroy(MiracastManager.Instance.gameObject);
- Debug.Log("TryStartServer: 0");
- }
- yield return new WaitForSeconds(2);
- GameObject miracastManagerObj = Instantiate(ResourcesManager.Load<GameObject>("MiracastManager"));
- Debug.Log("TryStartServer: 1");
- GameObject cameraRigObj = HMDPoseTracker.Instance.gameObject;
- if (cameraRigObj != null)
- {
- if (cameraRigObj.transform.parent != null && cameraRigObj.name != "XRMan")
- {
- miracastManagerObj.transform.parent = cameraRigObj.transform.parent;
- miracastManagerObj.transform.localPosition = Vector3.zero;
- miracastManagerObj.transform.localRotation = Quaternion.identity;
- }
- }
- if (MiracastManager.Instance != null)
- {
- Debug.Log("TryStartServer: 2");
- MiracastManager.Instance.Run(serverIP);
- Debug.Log("TryStartServer: 3");
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "MiracastStatus", "Started");
- Debug.Log("TryStartServer: 4");
- }
- }
- /// <summary>
- /// Receive from System, Recording instructions
- /// </summary>
- /// <param name="data"></param>
- private void OnReceive_RecordingInstructions(string data)
- {
- Debug.Log("UnityBroadcastReceiver OnReceive_RecordingInstructions: " + data);
- switch (data)
- {
- case "recordingStart":
- if (RecordingManager.Instance != null)
- {
- Destroy(RecordingManager.Instance.gameObject);
- }
- StartCoroutine(tryStartRecording());
- break;
- case "recordingStop":
- if (RecordingManager.Instance != null && RecordingManager.Instance.isRecording)
- {
- RecordingManager.Instance.HandleRecordingClose();
- }
- break;
- case "screenshotStart":
- if (RecordingManager.Instance != null)
- {
- Destroy(RecordingManager.Instance.gameObject);
- }
- StartCoroutine(tryStartScreenShot());
- break;
- }
- }
- private IEnumerator tryStartRecording()
- {
- yield return new WaitForSeconds(2);
- GameObject recordingManagerObj = Instantiate(ResourcesManager.Load<GameObject>("RecordingManager"));
- GameObject cameraRigObj = HMDPoseTracker.Instance.gameObject;
- if (cameraRigObj != null)
- {
- if (cameraRigObj.transform.parent != null && cameraRigObj.name != "XRMan")
- {
- recordingManagerObj.transform.parent = cameraRigObj.transform.parent;
- recordingManagerObj.transform.localPosition = Vector3.zero;
- recordingManagerObj.transform.localRotation = Quaternion.identity;
- }
- }
- if (RecordingManager.Instance != null)
- RecordingManager.Instance.HandleRecordingOpen();
- }
- private IEnumerator tryStartScreenShot()
- {
- yield return new WaitForSeconds(2);
- GameObject recordingManagerObj = Instantiate(ResourcesManager.Load<GameObject>("RecordingManager"));
- GameObject cameraRigObj = HMDPoseTracker.Instance.gameObject;
- if (cameraRigObj != null)
- {
- if (cameraRigObj.transform.parent != null && cameraRigObj.name != "XRMan")
- {
- recordingManagerObj.transform.parent = cameraRigObj.transform.parent;
- recordingManagerObj.transform.localPosition = Vector3.zero;
- recordingManagerObj.transform.localRotation = Quaternion.identity;
- }
- }
- if (RecordingManager.Instance != null)
- RecordingManager.Instance.HandleScreenCapture();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="data">Start/End</param>
- private void OnReceiveReset(string data)
- {
- Debug.Log("UnityBroadcastReceiver OnReceiveReset: OnInstructionsReceived_Reset + " + data);
- if (data == "End")
- {
- if (OnInstructionsReceived_EZXRServerResetEnd != null)
- {
- OnInstructionsReceived_EZXRServerResetEnd();
- }
- }
- }
- /// <summary>
- /// 接收系统广播实现打开SystemUI
- /// </summary>
- /// <param name="obj">1为进入待机,2为退出待机(收不到,不要用)</param>
- private void OnReceive_SCREEN_STATE(string obj)
- {
- Debug.Log("OnReceive_SCREEN_STATE: " + obj);
- if (OnSystemEnterStandbyMode != null)
- {
- OnSystemEnterStandbyMode();
- }
- }
- /// <summary>
- /// 设置系统级交互的状态,包括:食指指尖Home、手腕菜单、系统级手的显示状态
- /// </summary>
- /// <param name="state">false表示禁用系统级交互</param>
- public static void SetSystemInteractionState(bool state)
- {
- Utilities.Android.SendIntent("ToSystem", "com.ezxr.glass.systemui", "SetSystemHandState", state.ToString());
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="data">0是head,1是hand,2是controllers</param>
- private void OnReceive_SetSDKDynamicControllerType(string data)
- {
- Debug.Log("UnityBroadcastReceiver OnReceiveReset: OnReceive_SetSDKDynamicControllerType + " + data);
- ControllerType controllerType = (ControllerType)int.Parse(data);
- InputSystem.Instance.InitDynamicMode(controllerType);
- }
- }
- }
|