Module_SDKSystem.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. using SC.XR.Unity;
  2. using SC.XR.Unity.Module_Device;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using UnityEngine;
  6. namespace SC.XR.Unity.Module_SDKSystem {
  7. public class Module_SDKSystem : SCModuleMono {
  8. public static Module_SDKSystem Instance { get; private set; }
  9. [SerializeField]
  10. private bool EnableInputSystem = true;
  11. public bool DebugLog;
  12. bool isStart = false;
  13. public bool IsRunning {
  14. get; private set;
  15. }
  16. public bool Initialized {
  17. get; private set;
  18. }
  19. private Module_InputSystem.Module_InputSystem mInputSystem;
  20. public Module_InputSystem.Module_InputSystem InputSystem {
  21. get {
  22. if(EnableInputSystem && mInputSystem == null) {
  23. mInputSystem = GetComponentInChildren<Module_InputSystem.Module_InputSystem>(true);
  24. }
  25. return mInputSystem;
  26. }
  27. }
  28. //private GSXRManager mSvrManager;
  29. //public GSXRManager GSXRManager {
  30. // get {
  31. // if (mSvrManager == null) {
  32. // mSvrManager = GetComponentInChildren<GSXRManager>(true);
  33. // }
  34. // return mSvrManager;
  35. // }
  36. //}
  37. Coroutine waitSlam = null;
  38. Coroutine isRunningCoroutine = null;
  39. private Coroutine updateWaitForEndOfFrame = null;
  40. #region MonoBehavior Driver
  41. void Awake() {
  42. ModuleInit(false);
  43. Instantiate(Resources.Load("GatewayHttp"));
  44. }
  45. void OnEnable() {
  46. if(updateWaitForEndOfFrame == null) {
  47. updateWaitForEndOfFrame = StartCoroutine(UpdateWaitForEndOfFrame());
  48. }
  49. if(isStart == true) {
  50. ModuleStart();
  51. }
  52. }
  53. void Start() {
  54. isStart = true;
  55. ModuleStart();
  56. }
  57. void Update() {
  58. ModuleUpdate();
  59. }
  60. void LateUpdate() {
  61. ModuleLateUpdate();
  62. }
  63. void OnApplicationPause(bool pause) {
  64. if(isStart) {
  65. if(pause) {
  66. ModuleStop();
  67. } else {
  68. ModuleStart();
  69. }
  70. }
  71. }
  72. IEnumerator UpdateWaitForEndOfFrame() {
  73. while(true) {
  74. yield return new WaitForEndOfFrame();
  75. if(InputSystem && InputSystem.IsModuleStarted) {
  76. InputSystem.ModuleEndOfFrame();
  77. }
  78. }
  79. }
  80. void OnDisable() {
  81. if(updateWaitForEndOfFrame != null) {
  82. StopCoroutine(updateWaitForEndOfFrame);
  83. }
  84. ModuleStop();
  85. }
  86. void OnDestroy() {
  87. ModuleDestroy();
  88. isStart = false;
  89. }
  90. #endregion
  91. #region Module Behavoir
  92. public override void OnSCAwake() {
  93. base.OnSCAwake();
  94. if(Instance != null) {
  95. DestroyImmediate(gameObject);
  96. return;
  97. }
  98. DontDestroyOnLoad(gameObject);
  99. Instance = this;
  100. DebugMy.Log("Awake", this, true);
  101. DebugMy.Log("SDK Version:"+API_Module_SDKVersion.Version,this,true);
  102. DebugMy.Log("BatteryLevel: " + Module_BatteryStatus.getInstance.BatteryLevel + "% IsCharging: " + Module_BatteryStatus.getInstance.IsCharging, this, true);
  103. Module_Device.Module_Device.getInstance.Current.ShowInfo();
  104. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ShowDebugLog")) {
  105. DebugMy.isShowNormalLog = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ShowDebugLog", 0);
  106. } else {
  107. DebugMy.isShowNormalLog = DebugLog;
  108. }
  109. API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  110. AddModule(InputSystem);
  111. }
  112. public override void OnSCStart() {
  113. base.OnSCStart();
  114. API_GSXR_Slam.SlamManager?.gameObject.SetActive(true);
  115. if (waitSlam == null) {
  116. waitSlam = StartCoroutine(WaitSlamAction());
  117. }
  118. if (isRunningCoroutine == null) {
  119. isRunningCoroutine = StartCoroutine(Running());
  120. }
  121. }
  122. IEnumerator WaitSlamAction() {
  123. yield return new WaitUntil(() => API_GSXR_Slam.SlamManager.IsRunning);
  124. InputSystem?.ModuleStart();
  125. }
  126. public override void OnSCDisable() {
  127. base.OnSCDisable();
  128. if (waitSlam != null) {
  129. StopCoroutine(waitSlam);
  130. waitSlam = null;
  131. }
  132. if (isRunningCoroutine != null) {
  133. StopCoroutine(isRunningCoroutine);
  134. isRunningCoroutine = null;
  135. }
  136. IsRunning = false;
  137. //不能操作 灭屏唤醒否则起不来
  138. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  139. }
  140. public override void OnSCDestroy() {
  141. base.OnSCDestroy();
  142. if (Instance != this)
  143. return;
  144. if (waitSlam != null) {
  145. StopCoroutine(waitSlam);
  146. waitSlam = null;
  147. }
  148. if (isRunningCoroutine != null) {
  149. StopCoroutine(isRunningCoroutine);
  150. isRunningCoroutine = null;
  151. }
  152. API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  153. }
  154. #endregion
  155. IEnumerator Running() {
  156. ///SLam model
  157. yield return new WaitUntil(() => API_GSXR_Slam.SlamManager.IsRunning);
  158. if (InputSystem) {
  159. yield return new WaitUntil(() =>InputSystem.IsRunning);
  160. }
  161. IsRunning = true;
  162. isRunningCoroutine = null;
  163. DebugMy.Log("SDKSystem Module IsRuning !", this,true);
  164. }
  165. }
  166. }