123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- 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<int>))]
- 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<int> func);
- public static void GSXR_Unity_Set_CommonEventCallBack(Action<int> 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;
- }
- }
|