ModuleManager.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace Rokid.UXR.Module
  5. {
  6. /// <summary>
  7. /// Manage the initialization of the Android module
  8. /// </summary>
  9. public class ModuleManager : MonoSingleton<ModuleManager>
  10. {
  11. public class ModuleInfo
  12. {
  13. public string moduleName;
  14. public AndroidJavaObject module;
  15. public bool init;
  16. }
  17. /// <summary>
  18. /// This module can be initialized directly.
  19. /// </summary>
  20. /// <typeparam name="string"></typeparam>
  21. /// <typeparam name="ModuleInfo"></typeparam>
  22. /// <returns></returns>
  23. [SerializeField]
  24. private Dictionary<string, ModuleInfo> modules = new Dictionary<string, ModuleInfo>();
  25. /// <summary>
  26. /// This module needs to be initialized after the Slam initialization is completed.
  27. /// </summary>
  28. /// <typeparam name="string"></typeparam>
  29. /// <typeparam name="ModuleInfo"></typeparam>
  30. /// <returns></returns>
  31. [SerializeField]
  32. private Dictionary<string, ModuleInfo> afterSlamInitModuels = new Dictionary<string, ModuleInfo>();
  33. /// <summary>
  34. /// Is Slam initialization completed?
  35. /// </summary>
  36. private bool slamInit = false;
  37. private Action OnSlamInit;
  38. private Action OnAfterSlamInit;
  39. public void Initialze()
  40. {
  41. //TODO nothing
  42. }
  43. protected override void OnSingletonInit()
  44. {
  45. OnSlamInit += onSlamInit;
  46. DontDestroyOnLoad(this.gameObject);
  47. }
  48. protected override void OnDestroy()
  49. {
  50. base.OnDestroy();
  51. OnSlamInit -= onSlamInit;
  52. //清空事件...
  53. OnSlamInit = null;
  54. }
  55. private void onSlamInit()
  56. {
  57. foreach (var module in afterSlamInitModuels.Values)
  58. {
  59. if (!module.init)
  60. {
  61. RKLog.Info("====ModuleManager==== new moduleInfo: " + module.moduleName);
  62. module.module = new AndroidJavaObject(module.moduleName);
  63. module.init = true;
  64. }
  65. }
  66. }
  67. void Update()
  68. {
  69. if (slamInit == false)
  70. {
  71. //TODO Slam Init...
  72. slamInit = true;
  73. RKLog.Info("====ModuleManager==== Slam Inited !!!");
  74. OnSlamInit?.Invoke();
  75. OnAfterSlamInit?.Invoke();
  76. }
  77. }
  78. /// <summary>
  79. /// Retrieve the initialized module.
  80. /// </summary>
  81. /// <param name="moduleName"></param>
  82. /// <returns></returns>
  83. public AndroidJavaObject GetModule(string moduleName)
  84. {
  85. ModuleInfo moduleInfo;
  86. if (modules.TryGetValue(moduleName, out moduleInfo))
  87. {
  88. if (moduleInfo.init)
  89. {
  90. RKLog.Info($"====ModuleManager==== GetModule In modules {moduleName} ");
  91. return moduleInfo.module;
  92. }
  93. }
  94. if (afterSlamInitModuels.TryGetValue(moduleName, out moduleInfo))
  95. {
  96. if (moduleInfo.init)
  97. {
  98. RKLog.Info($"====ModuleManager==== GetModule In afterSlamInitModuels {moduleName} ");
  99. return moduleInfo.module;
  100. }
  101. }
  102. RKLog.Info($"====ModuleManager==== 模块未注册请注册模块 {moduleName} ");
  103. return null;
  104. }
  105. /// <summary>
  106. /// Register the module.
  107. /// </summary>
  108. /// <param name="moduleName"></param>
  109. /// <param name="afterSlamInit"></param>
  110. public void RegistModule(string moduleName, bool afterSlamInit = false, Action registCallBack = null)
  111. {
  112. if (afterSlamInit)
  113. {
  114. if (!afterSlamInitModuels.ContainsKey(moduleName) && !modules.ContainsKey(moduleName))
  115. {
  116. if (slamInit)
  117. {
  118. RKLog.Info("====ModuleManager==== SalmInit 注册模块");
  119. afterSlamInitModuels.Add(moduleName, new ModuleInfo()
  120. {
  121. moduleName = moduleName,
  122. module = new AndroidJavaObject(moduleName),
  123. init = true
  124. });
  125. registCallBack?.Invoke();
  126. }
  127. else
  128. {
  129. RKLog.Info($"====ModuleManager==== 添加到afterSlamInitModules集合中 {moduleName}");
  130. afterSlamInitModuels.Add(moduleName, new ModuleInfo()
  131. {
  132. moduleName = moduleName,
  133. init = false
  134. });
  135. OnAfterSlamInit += registCallBack;
  136. }
  137. }
  138. else
  139. {
  140. RKLog.Info($"====ModuleManager==== afterSlamInit 该模块已经注册 {moduleName}");
  141. ModuleInfo info = null;
  142. if (afterSlamInitModuels.TryGetValue(moduleName, out info))
  143. {
  144. if (info.init)
  145. {
  146. registCallBack?.Invoke();
  147. }
  148. else
  149. {
  150. OnAfterSlamInit += registCallBack;
  151. }
  152. }
  153. }
  154. }
  155. else
  156. {
  157. if (!modules.ContainsKey(moduleName) && !afterSlamInitModuels.ContainsKey(moduleName))
  158. {
  159. modules.Add(moduleName, new ModuleInfo()
  160. {
  161. moduleName = moduleName,
  162. module = new AndroidJavaObject(moduleName),
  163. init = true
  164. });
  165. }
  166. else
  167. {
  168. RKLog.Info($"====ModuleManager==== 该模块已经注册 {moduleName}");
  169. }
  170. registCallBack?.Invoke();
  171. }
  172. }
  173. }
  174. }