123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace Rokid.UXR.Module
- {
- /// <summary>
- /// Manage the initialization of the Android module
- /// </summary>
- public class ModuleManager : MonoSingleton<ModuleManager>
- {
- public class ModuleInfo
- {
- public string moduleName;
- public AndroidJavaObject module;
- public bool init;
- }
- /// <summary>
- /// This module can be initialized directly.
- /// </summary>
- /// <typeparam name="string"></typeparam>
- /// <typeparam name="ModuleInfo"></typeparam>
- /// <returns></returns>
- [SerializeField]
- private Dictionary<string, ModuleInfo> modules = new Dictionary<string, ModuleInfo>();
- /// <summary>
- /// This module needs to be initialized after the Slam initialization is completed.
- /// </summary>
- /// <typeparam name="string"></typeparam>
- /// <typeparam name="ModuleInfo"></typeparam>
- /// <returns></returns>
- [SerializeField]
- private Dictionary<string, ModuleInfo> afterSlamInitModuels = new Dictionary<string, ModuleInfo>();
- /// <summary>
- /// Is Slam initialization completed?
- /// </summary>
- private bool slamInit = false;
- private Action OnSlamInit;
- private Action OnAfterSlamInit;
- public void Initialze()
- {
- //TODO nothing
- }
- protected override void OnSingletonInit()
- {
- OnSlamInit += onSlamInit;
- DontDestroyOnLoad(this.gameObject);
- }
- protected override void OnDestroy()
- {
- base.OnDestroy();
- OnSlamInit -= onSlamInit;
- //清空事件...
- OnSlamInit = null;
- }
- private void onSlamInit()
- {
- foreach (var module in afterSlamInitModuels.Values)
- {
- if (!module.init)
- {
- RKLog.Info("====ModuleManager==== new moduleInfo: " + module.moduleName);
- module.module = new AndroidJavaObject(module.moduleName);
- module.init = true;
- }
- }
- }
- void Update()
- {
- if (slamInit == false)
- {
- //TODO Slam Init...
- slamInit = true;
- RKLog.Info("====ModuleManager==== Slam Inited !!!");
- OnSlamInit?.Invoke();
- OnAfterSlamInit?.Invoke();
- }
- }
- /// <summary>
- /// Retrieve the initialized module.
- /// </summary>
- /// <param name="moduleName"></param>
- /// <returns></returns>
- public AndroidJavaObject GetModule(string moduleName)
- {
- ModuleInfo moduleInfo;
- if (modules.TryGetValue(moduleName, out moduleInfo))
- {
- if (moduleInfo.init)
- {
- RKLog.Info($"====ModuleManager==== GetModule In modules {moduleName} ");
- return moduleInfo.module;
- }
- }
- if (afterSlamInitModuels.TryGetValue(moduleName, out moduleInfo))
- {
- if (moduleInfo.init)
- {
- RKLog.Info($"====ModuleManager==== GetModule In afterSlamInitModuels {moduleName} ");
- return moduleInfo.module;
- }
- }
- RKLog.Info($"====ModuleManager==== 模块未注册请注册模块 {moduleName} ");
- return null;
- }
- /// <summary>
- /// Register the module.
- /// </summary>
- /// <param name="moduleName"></param>
- /// <param name="afterSlamInit"></param>
- public void RegistModule(string moduleName, bool afterSlamInit = false, Action registCallBack = null)
- {
- if (afterSlamInit)
- {
- if (!afterSlamInitModuels.ContainsKey(moduleName) && !modules.ContainsKey(moduleName))
- {
- if (slamInit)
- {
- RKLog.Info("====ModuleManager==== SalmInit 注册模块");
- afterSlamInitModuels.Add(moduleName, new ModuleInfo()
- {
- moduleName = moduleName,
- module = new AndroidJavaObject(moduleName),
- init = true
- });
- registCallBack?.Invoke();
- }
- else
- {
- RKLog.Info($"====ModuleManager==== 添加到afterSlamInitModules集合中 {moduleName}");
- afterSlamInitModuels.Add(moduleName, new ModuleInfo()
- {
- moduleName = moduleName,
- init = false
- });
- OnAfterSlamInit += registCallBack;
- }
- }
- else
- {
- RKLog.Info($"====ModuleManager==== afterSlamInit 该模块已经注册 {moduleName}");
- ModuleInfo info = null;
- if (afterSlamInitModuels.TryGetValue(moduleName, out info))
- {
- if (info.init)
- {
- registCallBack?.Invoke();
- }
- else
- {
- OnAfterSlamInit += registCallBack;
- }
- }
- }
- }
- else
- {
- if (!modules.ContainsKey(moduleName) && !afterSlamInitModuels.ContainsKey(moduleName))
- {
- modules.Add(moduleName, new ModuleInfo()
- {
- moduleName = moduleName,
- module = new AndroidJavaObject(moduleName),
- init = true
- });
- }
- else
- {
- RKLog.Info($"====ModuleManager==== 该模块已经注册 {moduleName}");
- }
- registCallBack?.Invoke();
- }
- }
- }
- }
|