Module_CommonEvent.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. using AOT;
  2. using SC.XR.Unity;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. using UnityEngine;
  8. public class Module_CommonEvent : MonoBehaviour
  9. {
  10. static Module_CommonEvent instance;
  11. private static GameObject m_EventCameraCrashInstance;
  12. private static GameObject EventCameraCrashInstance {
  13. get {
  14. if (m_EventCameraCrashInstance == null) {
  15. m_EventCameraCrashInstance = (GameObject)Instantiate(Resources.Load("Prefabs/CameraCrashUI"));
  16. }
  17. return m_EventCameraCrashInstance;
  18. }
  19. }
  20. static int InitLeftCameraCullingMask;
  21. static int InitRightCameraCullingMask;
  22. Coroutine waitSlamInit = null;
  23. public static int UILayer = 16;
  24. void Awake() {
  25. if (instance) {
  26. DestroyImmediate(gameObject);
  27. return;
  28. }
  29. DebugMy.Log("Module_CommonEvent Awake", this, true);
  30. instance = this;
  31. DontDestroyOnLoad(gameObject);
  32. if (EventCameraCrashInstance) {
  33. EventCameraCrashInstance.layer = UILayer;
  34. EventCameraCrashInstance.transform.parent = transform;
  35. EventCameraCrashInstance.SetActive(false);
  36. }
  37. }
  38. // Start is called before the first frame update
  39. void Start()
  40. {
  41. if (waitSlamInit == null) {
  42. waitSlamInit = StartCoroutine(WaitSlamInitAction());
  43. }
  44. }
  45. // Update is called once per frame
  46. void Update()
  47. {
  48. //if (API_GSXR_Slam.GSXR_Is_SlamDataLost == false) {
  49. // DebugMy.Log("Module_CommonEvent " + API_GSXR_Slam.GSXR_Is_SlamDataLost, this, true);
  50. //}
  51. //if (Input.GetMouseButtonDown(0)) {
  52. // //API_GSXR_Slam.GSXR_Reset_Slam();
  53. // EventCameraCrash(API_GSXR_Slam.SlamManager);
  54. //}
  55. //if (Input.GetMouseButtonDown(1)) {
  56. // EventCameraReset(API_GSXR_Slam.SlamManager);
  57. //}
  58. }
  59. IEnumerator WaitSlamInitAction() {
  60. // yield return new WaitUntil(() => API_GSXR_Slam.SlamManager != null && API_GSXR_Slam.SlamManager.IsRunning);
  61. yield return null;
  62. GSXR_Unity_Set_CommonEventCallBack(CommonEvent);
  63. DebugMy.Log("Module_CommonEvent SetCommonEventCallBack Finish !",this,true);
  64. InitLeftCameraCullingMask = Camera.main.cullingMask;
  65. InitRightCameraCullingMask = Camera.main.cullingMask;
  66. waitSlamInit = null;
  67. }
  68. void OnDestroy() {
  69. if (instance != this)
  70. return;
  71. DebugMy.Log("Module_CommonEvent OnDestroy", this, true);
  72. if (waitSlamInit != null) {
  73. StopCoroutine(waitSlamInit);
  74. waitSlamInit = null;
  75. }
  76. GSXR_Unity_Set_CommonEventCallBack(null);
  77. }
  78. [MonoPInvokeCallback(typeof(Action<int>))]
  79. public static void CommonEvent(int eventID) {
  80. Debug.Log("Module_CommonEvent EventID:" + eventID);
  81. //if (API_GSXR_Slam.SlamManager == null || API_GSXR_Slam.plugin == null)
  82. // return;
  83. //if (eventID == (int)CommonEventID.TYPE_CAMERA_CRASH) {
  84. // EventCameraCrash(API_GSXR_Slam.SlamManager);
  85. //} else if (eventID == (int)CommonEventID.YTPE_CAMERA_RESET) {
  86. // EventCameraReset(API_GSXR_Slam.SlamManager);
  87. //}
  88. }
  89. static void EventCameraCrash(GSXRManager slam) {
  90. //if (slam.leftCamera) {
  91. // slam.leftCamera.cullingMask = 1 << UILayer;
  92. //}
  93. //if (slam.rightCamera) {
  94. // slam.rightCamera.cullingMask = 1 << UILayer;
  95. //}
  96. //if (EventCameraCrashInstance)
  97. // EventCameraCrashInstance.SetActive(true);
  98. //AudioListener.pause = true;
  99. //if (instance)
  100. // instance.StartReset(slam);
  101. //API_GSXR_Slam.GSXR_Reset_Slam();
  102. }
  103. static void EventCameraReset(GSXRManager slam) {
  104. //if (EventCameraCrashInstance)
  105. // EventCameraCrashInstance.SetActive(false);
  106. //AudioListener.pause = false;
  107. //if (slam.leftCamera) {
  108. // slam.leftCamera.cullingMask = InitLeftCameraCullingMask;
  109. //}
  110. //if (slam.rightCamera) {
  111. // slam.rightCamera.cullingMask = InitRightCameraCullingMask;
  112. //}
  113. if (instance)
  114. instance.StartReset(slam);
  115. }
  116. private const string DLLName = "gsxrplugin";
  117. [DllImport(DLLName)]
  118. private static extern void GSXR_Set_CommonEventCallBack(Action<int> func);
  119. public static void GSXR_Unity_Set_CommonEventCallBack(Action<int> func) {
  120. if (Application.platform == RuntimePlatform.Android) {
  121. GSXR_Set_CommonEventCallBack(func);
  122. }
  123. }
  124. public enum CommonEventID {
  125. TYPE_CAMERA_CRASH = 0,
  126. YTPE_CAMERA_RESET = 1,
  127. }
  128. Coroutine runResetCor;
  129. void StartReset(GSXRManager slam) {
  130. if (runResetCor == null) {
  131. runResetCor = StartCoroutine(RunReset(slam));
  132. }
  133. }
  134. IEnumerator RunReset(GSXRManager slam) {
  135. Debug.Log("Module_CommonEvent RunReset");
  136. if (slam.leftCamera) {
  137. slam.leftCamera.cullingMask = 1 << UILayer;
  138. }
  139. if (slam.rightCamera) {
  140. slam.rightCamera.cullingMask = 1 << UILayer;
  141. }
  142. if (EventCameraCrashInstance)
  143. EventCameraCrashInstance.SetActive(true);
  144. AudioListener.pause = true;
  145. //yield return new WaitUntil(()=> slam.IsTrackingValid);
  146. yield return new WaitForSeconds(3f);
  147. if (EventCameraCrashInstance)
  148. EventCameraCrashInstance.SetActive(false);
  149. AudioListener.pause = false;
  150. if (slam.leftCamera) {
  151. slam.leftCamera.cullingMask = InitLeftCameraCullingMask;
  152. }
  153. if (slam.rightCamera) {
  154. slam.rightCamera.cullingMask = InitRightCameraCullingMask;
  155. }
  156. runResetCor = null;
  157. }
  158. }