using AOT; using SC.XR.Unity; using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; public class Module_CommonEvent : MonoBehaviour { static Module_CommonEvent instance; private static GameObject m_EventCameraCrashInstance; private static GameObject EventCameraCrashInstance { get { if (m_EventCameraCrashInstance == null) { m_EventCameraCrashInstance = (GameObject)Instantiate(Resources.Load("Prefabs/CameraCrashUI")); } return m_EventCameraCrashInstance; } } static int InitLeftCameraCullingMask; static int InitRightCameraCullingMask; Coroutine waitSlamInit = null; public static int UILayer = 16; void Awake() { if (instance) { DestroyImmediate(gameObject); return; } DebugMy.Log("Module_CommonEvent Awake", this, true); instance = this; DontDestroyOnLoad(gameObject); if (EventCameraCrashInstance) { EventCameraCrashInstance.layer = UILayer; EventCameraCrashInstance.transform.parent = transform; EventCameraCrashInstance.SetActive(false); } } // Start is called before the first frame update void Start() { if (waitSlamInit == null) { waitSlamInit = StartCoroutine(WaitSlamInitAction()); } } // Update is called once per frame void Update() { //if (API_GSXR_Slam.GSXR_Is_SlamDataLost == false) { // DebugMy.Log("Module_CommonEvent " + API_GSXR_Slam.GSXR_Is_SlamDataLost, this, true); //} //if (Input.GetMouseButtonDown(0)) { // //API_GSXR_Slam.GSXR_Reset_Slam(); // EventCameraCrash(API_GSXR_Slam.SlamManager); //} //if (Input.GetMouseButtonDown(1)) { // EventCameraReset(API_GSXR_Slam.SlamManager); //} } IEnumerator WaitSlamInitAction() { // yield return new WaitUntil(() => API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.IsRunning); yield return null; GSXR_Unity_Set_CommonEventCallBack(CommonEvent); DebugMy.Log("Module_CommonEvent SetCommonEventCallBack Finish !",this,true); InitLeftCameraCullingMask = Camera.main.cullingMask; InitRightCameraCullingMask = Camera.main.cullingMask; waitSlamInit = null; } void OnDestroy() { if (instance != this) return; DebugMy.Log("Module_CommonEvent OnDestroy", this, true); if (waitSlamInit != null) { StopCoroutine(waitSlamInit); waitSlamInit = null; } GSXR_Unity_Set_CommonEventCallBack(null); } [MonoPInvokeCallback(typeof(Action))] public static void CommonEvent(int eventID) { Debug.Log("Module_CommonEvent EventID:" + eventID); //if (API_GSXR_Slam.SlamManager == null || API_GSXR_Slam.plugin == null) // return; //if (eventID == (int)CommonEventID.TYPE_CAMERA_CRASH) { // EventCameraCrash(API_GSXR_Slam.SlamManager); //} else if (eventID == (int)CommonEventID.YTPE_CAMERA_RESET) { // EventCameraReset(API_GSXR_Slam.SlamManager); //} } static void EventCameraCrash(GSXRManager slam) { //if (slam.leftCamera) { // slam.leftCamera.cullingMask = 1 << UILayer; //} //if (slam.rightCamera) { // slam.rightCamera.cullingMask = 1 << UILayer; //} //if (EventCameraCrashInstance) // EventCameraCrashInstance.SetActive(true); //AudioListener.pause = true; //if (instance) // instance.StartReset(slam); //API_GSXR_Slam.GSXR_Reset_Slam(); } static void EventCameraReset(GSXRManager slam) { //if (EventCameraCrashInstance) // EventCameraCrashInstance.SetActive(false); //AudioListener.pause = false; //if (slam.leftCamera) { // slam.leftCamera.cullingMask = InitLeftCameraCullingMask; //} //if (slam.rightCamera) { // slam.rightCamera.cullingMask = InitRightCameraCullingMask; //} if (instance) instance.StartReset(slam); } private const string DLLName = "gsxrplugin"; [DllImport(DLLName)] private static extern void GSXR_Set_CommonEventCallBack(Action func); public static void GSXR_Unity_Set_CommonEventCallBack(Action func) { if (Application.platform == RuntimePlatform.Android) { GSXR_Set_CommonEventCallBack(func); } } public enum CommonEventID { TYPE_CAMERA_CRASH = 0, YTPE_CAMERA_RESET = 1, } Coroutine runResetCor; void StartReset(GSXRManager slam) { if (runResetCor == null) { runResetCor = StartCoroutine(RunReset(slam)); } } IEnumerator RunReset(GSXRManager slam) { Debug.Log("Module_CommonEvent RunReset"); if (slam.leftCamera) { slam.leftCamera.cullingMask = 1 << UILayer; } if (slam.rightCamera) { slam.rightCamera.cullingMask = 1 << UILayer; } if (EventCameraCrashInstance) EventCameraCrashInstance.SetActive(true); AudioListener.pause = true; //yield return new WaitUntil(()=> slam.IsTrackingValid); yield return new WaitForSeconds(3f); if (EventCameraCrashInstance) EventCameraCrashInstance.SetActive(false); AudioListener.pause = false; if (slam.leftCamera) { slam.leftCamera.cullingMask = InitLeftCameraCullingMask; } if (slam.rightCamera) { slam.rightCamera.cullingMask = InitRightCameraCullingMask; } runResetCor = null; } }