Module_SDKSystem.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. }
  44. void OnEnable() {
  45. if(updateWaitForEndOfFrame == null) {
  46. updateWaitForEndOfFrame = StartCoroutine(UpdateWaitForEndOfFrame());
  47. }
  48. if(isStart == true) {
  49. ModuleStart();
  50. }
  51. }
  52. void Start() {
  53. isStart = true;
  54. ModuleStart();
  55. }
  56. void Update() {
  57. ModuleUpdate();
  58. }
  59. void LateUpdate() {
  60. ModuleLateUpdate();
  61. }
  62. void OnApplicationPause(bool pause) {
  63. if(isStart) {
  64. if(pause) {
  65. ModuleStop();
  66. } else {
  67. ModuleStart();
  68. }
  69. }
  70. }
  71. IEnumerator UpdateWaitForEndOfFrame() {
  72. while(true) {
  73. yield return new WaitForEndOfFrame();
  74. if(InputSystem && InputSystem.IsModuleStarted) {
  75. InputSystem.ModuleEndOfFrame();
  76. }
  77. }
  78. }
  79. void OnDisable() {
  80. if(updateWaitForEndOfFrame != null) {
  81. StopCoroutine(updateWaitForEndOfFrame);
  82. }
  83. ModuleStop();
  84. }
  85. void OnDestroy() {
  86. ModuleDestroy();
  87. isStart = false;
  88. }
  89. #endregion
  90. #region Module Behavoir
  91. public override void OnSCAwake() {
  92. base.OnSCAwake();
  93. if(Instance != null) {
  94. DestroyImmediate(gameObject);
  95. return;
  96. }
  97. DontDestroyOnLoad(gameObject);
  98. Instance = this;
  99. DebugMy.Log("Awake", this, true);
  100. DebugMy.Log("SDK Version:"+API_Module_SDKVersion.Version,this,true);
  101. DebugMy.Log("BatteryLevel: " + Module_BatteryStatus.getInstance.BatteryLevel + "% IsCharging: " + Module_BatteryStatus.getInstance.IsCharging, this, true);
  102. Module_Device.Module_Device.getInstance.Current.ShowInfo();
  103. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ShowDebugLog")) {
  104. DebugMy.isShowNormalLog = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ShowDebugLog", 0);
  105. } else {
  106. DebugMy.isShowNormalLog = DebugLog;
  107. }
  108. API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  109. AddModule(InputSystem);
  110. }
  111. public override void OnSCStart() {
  112. base.OnSCStart();
  113. API_GSXR_Slam.SlamManager?.gameObject.SetActive(true);
  114. if (waitSlam == null) {
  115. waitSlam = StartCoroutine(WaitSlamAction());
  116. }
  117. if (isRunningCoroutine == null) {
  118. isRunningCoroutine = StartCoroutine(Running());
  119. }
  120. }
  121. IEnumerator WaitSlamAction() {
  122. yield return new WaitUntil(() => API_GSXR_Slam.SlamManager.IsRunning);
  123. InputSystem?.ModuleStart();
  124. }
  125. public override void OnSCDisable() {
  126. base.OnSCDisable();
  127. if (waitSlam != null) {
  128. StopCoroutine(waitSlam);
  129. waitSlam = null;
  130. }
  131. if (isRunningCoroutine != null) {
  132. StopCoroutine(isRunningCoroutine);
  133. isRunningCoroutine = null;
  134. }
  135. IsRunning = false;
  136. //不能操作 灭屏唤醒否则起不来
  137. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  138. }
  139. public override void OnSCDestroy() {
  140. base.OnSCDestroy();
  141. if (Instance != this)
  142. return;
  143. if (waitSlam != null) {
  144. StopCoroutine(waitSlam);
  145. waitSlam = null;
  146. }
  147. if (isRunningCoroutine != null) {
  148. StopCoroutine(isRunningCoroutine);
  149. isRunningCoroutine = null;
  150. }
  151. API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  152. }
  153. #endregion
  154. IEnumerator Running() {
  155. ///SLam model
  156. yield return new WaitUntil(() => API_GSXR_Slam.SlamManager.IsRunning);
  157. if (InputSystem) {
  158. yield return new WaitUntil(() =>InputSystem.IsRunning);
  159. }
  160. IsRunning = true;
  161. isRunningCoroutine = null;
  162. DebugMy.Log("SDKSystem Module IsRuning !", this,true);
  163. }
  164. }
  165. }