Module_SDKSystem.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. [SerializeField]
  12. private bool EnableDetectorSystem = true;
  13. [SerializeField]
  14. private string m_appKey = "";
  15. [SerializeField]
  16. private string m_appSecret = "";
  17. public string AppKey {
  18. get { return m_appKey; }
  19. set { m_appKey = value; }
  20. }
  21. public string AppSecret {
  22. get { return m_appSecret; }
  23. set { m_appSecret = value; }
  24. }
  25. bool isStart = false;
  26. private bool IsUseSafetyAreaHeight = true;
  27. private bool IsDontDestroyOnLoad=true;
  28. public bool IsRunning {
  29. get; private set;
  30. }
  31. public bool Initialized {
  32. get; private set;
  33. }
  34. private Module_InputSystem.Module_InputSystem mInputSystem;
  35. public Module_InputSystem.Module_InputSystem InputSystem {
  36. get {
  37. if(EnableInputSystem && mInputSystem == null) {
  38. mInputSystem = GetComponentInChildren<Module_InputSystem.Module_InputSystem>(true);
  39. }
  40. return mInputSystem;
  41. }
  42. }
  43. private Module_DetectorSystem.Module_DetectorSystem mDetectorSystem;
  44. public Module_DetectorSystem.Module_DetectorSystem DetectorSystem {
  45. get {
  46. if (EnableDetectorSystem && mDetectorSystem == null) {
  47. mDetectorSystem = Module_DetectorSystem.Module_DetectorSystem.Instance;
  48. }
  49. return mDetectorSystem;
  50. }
  51. }
  52. Coroutine waitSlamInit = null;
  53. Coroutine waitSlamRunning = null;
  54. private Coroutine updateWaitForEndOfFrame = null;
  55. #region MonoBehavior Driver
  56. void Awake() {
  57. DebugMy.Log("Awake", this, true);
  58. ModuleInit(false);
  59. Instantiate(Resources.Load("GatewayHttp"));
  60. }
  61. void OnEnable() {
  62. DebugMy.Log("OnEnable", this, true);
  63. if (updateWaitForEndOfFrame == null) {
  64. updateWaitForEndOfFrame = StartCoroutine(UpdateWaitForEndOfFrame());
  65. }
  66. if(isStart == true) {
  67. ModuleStart();
  68. }
  69. }
  70. void Start() {
  71. DebugMy.Log("Start", this, true);
  72. isStart = true;
  73. ModuleStart();
  74. }
  75. void Update() {
  76. Fps.StartALLLogicAndRenderTime = Time.realtimeSinceStartup;
  77. ModuleUpdate();
  78. }
  79. void LateUpdate() {
  80. ModuleLateUpdate();
  81. Fps.ALLLogicTime = Mathf.Lerp(Fps.ALLLogicTime, 1000 * (Time.realtimeSinceStartup - Fps.StartALLLogicAndRenderTime), Fps.lerp);
  82. }
  83. void OnApplicationPause(bool pause) {
  84. return;
  85. DebugMy.Log("OnApplicationPause:"+ pause,this,true);
  86. if(isStart) {
  87. if(pause) {
  88. ModuleStop();
  89. } else {
  90. ModuleStart();
  91. }
  92. }
  93. }
  94. IEnumerator UpdateWaitForEndOfFrame() {
  95. while(true) {
  96. yield return new WaitForEndOfFrame();
  97. if(InputSystem && InputSystem.IsModuleStarted) {
  98. InputSystem.ModuleEndOfFrame();
  99. }
  100. }
  101. }
  102. void OnDisable() {
  103. DebugMy.Log("OnDisable", this, true);
  104. if (updateWaitForEndOfFrame != null) {
  105. StopCoroutine(updateWaitForEndOfFrame);
  106. }
  107. ModuleStop();
  108. }
  109. void OnDestroy() {
  110. DebugMy.Log("OnDestroy", this, true);
  111. ModuleDestroy();
  112. isStart = false;
  113. }
  114. #endregion
  115. #region Module Behavoir
  116. public override void OnSCAwake() {
  117. base.OnSCAwake();
  118. if(Instance != null) {
  119. DestroyImmediate(gameObject);
  120. return;
  121. }
  122. Instance = this;
  123. Initialized = false;
  124. if (API_Module_SDKConfiguration.HasKey("Module_SDKSystem", "IsDontDestroyOnLoad"))
  125. {
  126. IsDontDestroyOnLoad = API_Module_SDKConfiguration.GetBool("Module_SDKSystem", "IsDontDestroyOnLoad", 1);
  127. }
  128. if (IsDontDestroyOnLoad )
  129. {
  130. DontDestroyOnLoad(gameObject);
  131. }
  132. DebugMy.Log("OnSCAwake", this, true);
  133. DebugMy.Log("SDK Version:" + API_Module_SDKVersion.Version, this, true);
  134. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ShowDebugLog")) {
  135. DebugMy.isShowNormalLog = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ShowDebugLog", 0);
  136. }
  137. if (waitSlamInit == null) {
  138. waitSlamInit = StartCoroutine(WaitSlamInitAction());
  139. } else {
  140. DebugMy.LogError("waitSlamInit !=null",this);
  141. }
  142. }
  143. IEnumerator WaitSlamInitAction() {
  144. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(true);
  145. //yield return new WaitUntil(() => API_GSXR_Slam.SlamManager!=null && API_GSXR_Slam.SlamManager.IsRunning);
  146. AddModule(InputSystem);
  147. AddModule(DetectorSystem);
  148. DebugMy.Log("Try Get SDKGlobalConfiguration.getInstance.IsInit", this, true);
  149. yield return new WaitUntil(()=>Module_SDKGlobalConfiguration.getInstance.IsInit==true);
  150. Module_Device.Module_Device.getInstance.Current.ShowInfo();
  151. Initialized = true;
  152. DebugMy.Log("SDKSystem Module Initialized !", this, true);
  153. DebugMy.Log("BatteryLevel: " + API_Module_Device.Current.BatteryLevel + "% IsCharging: " + Module_BatteryStatus.getInstance.IsCharging, this, true);
  154. }
  155. public override void OnSCStart() {
  156. base.OnSCStart();
  157. if (waitSlamRunning == null) {
  158. waitSlamRunning = StartCoroutine(WaitSlamRunningAction());
  159. }
  160. }
  161. IEnumerator WaitSlamRunningAction() {
  162. yield return new WaitUntil(() => Initialized==true);
  163. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(true);
  164. //yield return new WaitUntil(() => API_GSXR_Slam.SlamManager.IsRunning);
  165. InputSystem?.ModuleStart();
  166. if (InputSystem) {
  167. yield return new WaitUntil(() => InputSystem.IsRunning);
  168. }
  169. DetectorSystem?.ModuleStart();
  170. IsRunning = true;
  171. DebugMy.Log("SDKSystem Module IsRuning !", this, true);
  172. }
  173. public override void OnSCDisable() {
  174. base.OnSCDisable();
  175. if (waitSlamRunning != null) {
  176. StopCoroutine(waitSlamRunning);
  177. waitSlamRunning = null;
  178. }
  179. IsRunning = false;
  180. //不能操作 灭屏唤醒否则起不来
  181. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  182. }
  183. public override void OnSCDestroy() {
  184. base.OnSCDestroy();
  185. if (Instance != this)
  186. return;
  187. Initialized = false;
  188. if (waitSlamInit != null) {
  189. StopCoroutine(waitSlamInit);
  190. waitSlamInit = null;
  191. }
  192. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  193. }
  194. #endregion
  195. }
  196. }