Module_SDKSystem.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. }
  60. void OnEnable() {
  61. DebugMy.Log("OnEnable", this, true);
  62. if (updateWaitForEndOfFrame == null) {
  63. updateWaitForEndOfFrame = StartCoroutine(UpdateWaitForEndOfFrame());
  64. }
  65. if(isStart == true) {
  66. ModuleStart();
  67. }
  68. }
  69. void Start() {
  70. DebugMy.Log("Start", this, true);
  71. isStart = true;
  72. ModuleStart();
  73. }
  74. void Update() {
  75. Fps.StartALLLogicAndRenderTime = Time.realtimeSinceStartup;
  76. ModuleUpdate();
  77. }
  78. void LateUpdate() {
  79. ModuleLateUpdate();
  80. Fps.ALLLogicTime = Mathf.Lerp(Fps.ALLLogicTime, 1000 * (Time.realtimeSinceStartup - Fps.StartALLLogicAndRenderTime), Fps.lerp);
  81. }
  82. void OnApplicationPause(bool pause) {
  83. return;
  84. DebugMy.Log("OnApplicationPause:"+ pause,this,true);
  85. if(isStart) {
  86. if(pause) {
  87. ModuleStop();
  88. } else {
  89. ModuleStart();
  90. }
  91. }
  92. }
  93. IEnumerator UpdateWaitForEndOfFrame() {
  94. while(true) {
  95. yield return new WaitForEndOfFrame();
  96. if(InputSystem && InputSystem.IsModuleStarted) {
  97. InputSystem.ModuleEndOfFrame();
  98. }
  99. }
  100. }
  101. void OnDisable() {
  102. DebugMy.Log("OnDisable", this, true);
  103. if (updateWaitForEndOfFrame != null) {
  104. StopCoroutine(updateWaitForEndOfFrame);
  105. }
  106. ModuleStop();
  107. }
  108. void OnDestroy() {
  109. DebugMy.Log("OnDestroy", this, true);
  110. ModuleDestroy();
  111. isStart = false;
  112. }
  113. #endregion
  114. #region Module Behavoir
  115. public override void OnSCAwake() {
  116. base.OnSCAwake();
  117. if(Instance != null) {
  118. DestroyImmediate(gameObject);
  119. return;
  120. }
  121. Instance = this;
  122. Initialized = false;
  123. if (API_Module_SDKConfiguration.HasKey("Module_SDKSystem", "IsDontDestroyOnLoad"))
  124. {
  125. IsDontDestroyOnLoad = API_Module_SDKConfiguration.GetBool("Module_SDKSystem", "IsDontDestroyOnLoad", 1);
  126. }
  127. if (IsDontDestroyOnLoad )
  128. {
  129. DontDestroyOnLoad(gameObject);
  130. }
  131. DebugMy.Log("OnSCAwake", this, true);
  132. DebugMy.Log("SDK Version:" + API_Module_SDKVersion.Version, this, true);
  133. if (API_Module_SDKConfiguration.HasKey("Module_InputSystem", "ShowDebugLog")) {
  134. DebugMy.isShowNormalLog = API_Module_SDKConfiguration.GetBool("Module_InputSystem", "ShowDebugLog", 0);
  135. }
  136. if (waitSlamInit == null) {
  137. waitSlamInit = StartCoroutine(WaitSlamInitAction());
  138. } else {
  139. DebugMy.LogError("waitSlamInit !=null",this);
  140. }
  141. }
  142. IEnumerator WaitSlamInitAction() {
  143. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(true);
  144. //yield return new WaitUntil(() => API_GSXR_Slam.SlamManager!=null && API_GSXR_Slam.SlamManager.IsRunning);
  145. AddModule(InputSystem);
  146. AddModule(DetectorSystem);
  147. DebugMy.Log("Try Get SDKGlobalConfiguration.getInstance.IsInit", this, true);
  148. yield return new WaitUntil(()=>Module_SDKGlobalConfiguration.getInstance.IsInit==true);
  149. Module_Device.Module_Device.getInstance.Current.ShowInfo();
  150. Initialized = true;
  151. DebugMy.Log("SDKSystem Module Initialized !", this, true);
  152. DebugMy.Log("BatteryLevel: " + API_Module_Device.Current.BatteryLevel + "% IsCharging: " + Module_BatteryStatus.getInstance.IsCharging, this, true);
  153. if (IsUseSafetyAreaHeight) {
  154. if (API_SDKGlobalConfiguration.HasKey("Module_SafetyArea", "Height")) {
  155. float getHeight = API_SDKGlobalConfiguration.GetFloat("Module_SafetyArea", "Height", 1.6f);
  156. DebugMy.Log("SDKSystem Module SetSafetyAreaHeight:" + getHeight, this, true);
  157. API_Module_SDKSystem.SetSDKSystemHeight(getHeight);
  158. }
  159. }
  160. }
  161. public override void OnSCStart() {
  162. base.OnSCStart();
  163. if (waitSlamRunning == null) {
  164. waitSlamRunning = StartCoroutine(WaitSlamRunningAction());
  165. }
  166. }
  167. IEnumerator WaitSlamRunningAction() {
  168. yield return new WaitUntil(() => Initialized==true);
  169. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(true);
  170. //yield return new WaitUntil(() => API_GSXR_Slam.SlamManager.IsRunning);
  171. InputSystem?.ModuleStart();
  172. if (InputSystem) {
  173. yield return new WaitUntil(() => InputSystem.IsRunning);
  174. }
  175. DetectorSystem?.ModuleStart();
  176. IsRunning = true;
  177. DebugMy.Log("SDKSystem Module IsRuning !", this, true);
  178. }
  179. public override void OnSCDisable() {
  180. base.OnSCDisable();
  181. if (waitSlamRunning != null) {
  182. StopCoroutine(waitSlamRunning);
  183. waitSlamRunning = null;
  184. }
  185. IsRunning = false;
  186. //不能操作 灭屏唤醒否则起不来
  187. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  188. }
  189. public override void OnSCDestroy() {
  190. base.OnSCDestroy();
  191. if (Instance != this)
  192. return;
  193. Initialized = false;
  194. if (waitSlamInit != null) {
  195. StopCoroutine(waitSlamInit);
  196. waitSlamInit = null;
  197. }
  198. //API_GSXR_Slam.SlamManager?.gameObject.SetActive(false);
  199. }
  200. #endregion
  201. }
  202. }