Module_DetectorSystem.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace SC.XR.Unity.Module_DetectorSystem
  5. {
  6. public class Module_DetectorSystem : SCModuleMono
  7. {
  8. private static Module_DetectorSystem instance;
  9. private static object lockObj = new object();
  10. public static Module_DetectorSystem Instance
  11. {
  12. get
  13. {
  14. if (instance == null)
  15. {
  16. lock (lockObj)
  17. {
  18. if (instance == null)
  19. {
  20. Module_DetectorSystem prefab = Resources.Load<Module_DetectorSystem>("Prefabs/DetectorSystem");
  21. instance = GameObject.Instantiate(prefab);
  22. instance.Init();
  23. }
  24. }
  25. }
  26. return instance;
  27. }
  28. }
  29. private List<SCBaseDetector> m_SCDetectorList;
  30. public List<SCBaseDetector> SCDetectorList
  31. {
  32. get
  33. {
  34. if (m_SCDetectorList == null)
  35. {
  36. m_SCDetectorList = new List<SCBaseDetector>();
  37. SCBaseDetector[] scBaseDetectorList = GetComponentsInChildren<SCBaseDetector>(true);
  38. foreach (var baseDetector in scBaseDetectorList)
  39. {
  40. m_SCDetectorList.Add(baseDetector);
  41. }
  42. }
  43. return m_SCDetectorList;
  44. }
  45. }
  46. private bool activeBatteryDetector = false;
  47. private bool activeLanguageDetector = false;
  48. private bool activeNoNetworkDetector = false;
  49. private bool activeNoticeDetector = false;
  50. private bool activeRecorderDetector = false;
  51. private bool activeVolumeDetector = false;
  52. public override void OnSCAwake()
  53. {
  54. base.OnSCAwake();
  55. instance = this;
  56. if (API_Module_SDKConfiguration.HasKey("Module_DetectorSystem", "ActiveBatteryDetector"))
  57. {
  58. activeBatteryDetector = API_Module_SDKConfiguration.GetBool("Module_DetectorSystem", "ActiveBatteryDetector", 0);
  59. }
  60. if (API_Module_SDKConfiguration.HasKey("Module_DetectorSystem", "ActiveLanguageDetector"))
  61. {
  62. activeLanguageDetector = API_Module_SDKConfiguration.GetBool("Module_DetectorSystem", "ActiveLanguageDetector", 0);
  63. }
  64. if (API_Module_SDKConfiguration.HasKey("Module_DetectorSystem", "ActiveNoNetworkDetector"))
  65. {
  66. activeNoNetworkDetector = API_Module_SDKConfiguration.GetBool("Module_DetectorSystem", "ActiveNoNetworkDetector", 0);
  67. }
  68. if (API_Module_SDKConfiguration.HasKey("Module_DetectorSystem", "ActiveNoticeDetector"))
  69. {
  70. activeNoticeDetector = API_Module_SDKConfiguration.GetBool("Module_DetectorSystem", "ActiveNoticeDetector", 0);
  71. }
  72. if (API_Module_SDKConfiguration.HasKey("Module_DetectorSystem", "ActiveRecorderDetector"))
  73. {
  74. activeRecorderDetector = API_Module_SDKConfiguration.GetBool("Module_DetectorSystem", "ActiveRecorderDetector", 0);
  75. }
  76. if (API_Module_SDKConfiguration.HasKey("Module_DetectorSystem", "ActiveVolumeDetector"))
  77. {
  78. activeVolumeDetector = API_Module_SDKConfiguration.GetBool("Module_DetectorSystem", "ActiveVolumeDetector", 0);
  79. }
  80. foreach (var detector in SCDetectorList)
  81. {
  82. if (GetFlag(detector.detectorType))
  83. {
  84. AddModule(detector);
  85. }
  86. }
  87. }
  88. public override void OnSCStart()
  89. {
  90. base.OnSCStart();
  91. SetActiveSCDetector(SCDetectorType.Battery, activeBatteryDetector);
  92. SetActiveSCDetector(SCDetectorType.Language, activeLanguageDetector);
  93. SetActiveSCDetector(SCDetectorType.NoNetwork, activeNoNetworkDetector);
  94. SetActiveSCDetector(SCDetectorType.Notice, activeNoticeDetector);
  95. SetActiveSCDetector(SCDetectorType.Recorder, activeRecorderDetector);
  96. SetActiveSCDetector(SCDetectorType.Volume, activeVolumeDetector);
  97. }
  98. public T GetSCBaseDetector<T>(SCDetectorType type) where T : SCBaseDetector
  99. {
  100. foreach (var baseDetector in SCDetectorList)
  101. {
  102. if (type == baseDetector.detectorType)
  103. {
  104. return (T)baseDetector;
  105. }
  106. }
  107. return null;
  108. }
  109. public void SetActiveSCDetector(SCDetectorType type, bool active)
  110. {
  111. SCBaseDetector baseDetector = GetSCBaseDetector<SCBaseDetector>(type);
  112. if (baseDetector == null)
  113. return;
  114. SetFlag(type, active);
  115. if (active)
  116. {
  117. baseDetector.ModuleStart();
  118. }
  119. else
  120. {
  121. baseDetector.ModuleStop();
  122. }
  123. }
  124. private bool GetFlag(SCDetectorType type)
  125. {
  126. switch (type)
  127. {
  128. case SCDetectorType.Battery:
  129. return activeBatteryDetector;
  130. case SCDetectorType.Language:
  131. return activeLanguageDetector;
  132. case SCDetectorType.NoNetwork:
  133. return activeNoNetworkDetector;
  134. case SCDetectorType.Notice:
  135. return activeNoticeDetector;
  136. case SCDetectorType.Recorder:
  137. return activeRecorderDetector;
  138. case SCDetectorType.Volume:
  139. return activeVolumeDetector;
  140. default:
  141. return false;
  142. }
  143. }
  144. private void SetFlag(SCDetectorType type, bool active)
  145. {
  146. switch (type)
  147. {
  148. case SCDetectorType.Battery:
  149. activeBatteryDetector = active;
  150. break;
  151. case SCDetectorType.Language:
  152. break;
  153. case SCDetectorType.NoNetwork:
  154. activeNoNetworkDetector = active;
  155. break;
  156. case SCDetectorType.Notice:
  157. activeNoticeDetector = active;
  158. break;
  159. case SCDetectorType.Recorder:
  160. activeRecorderDetector = active;
  161. break;
  162. case SCDetectorType.Volume:
  163. activeVolumeDetector = active;
  164. break;
  165. }
  166. }
  167. private void Init()
  168. {
  169. instance.transform.SetParent(Module_SDKSystem.Module_SDKSystem.Instance.transform);
  170. instance.transform.name = "DetectorSystem";
  171. instance.transform.position = Vector3.zero;
  172. instance.transform.rotation = Quaternion.identity;
  173. instance.transform.localScale = Vector3.one;
  174. }
  175. }
  176. }