Module_ShadowSystem.cs 5.5 KB

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