using System; using LightGlue.Unity.Config; using LightGlue.Unity.Game; using LightGlue.Unity.Networking; using LightGlue.Unity.Roma; using LightGlue.Unity.Roma.Bridge; using LightGlue.Unity.UI; using UnityEngine; namespace LightGlue.Unity.Runtime { /// /// 默认适配器实现: /// - 不侵入插件核心,只调用现有 Manager / Bridge API /// - 适合业务侧通过 ILightGlueRuntime 统一调用 /// public sealed class LightGlueRuntimeAdapter : ILightGlueRuntime { public event Action OnPositionUpdate; //private LightGlueManager _lightGlueManager; private RomaManager _romaManager; private bool _resultSubscribed; public void Init() { // 若场景里有 LightGlueGameManager,它会在 Awake 里负责实例化系统预制。 // 这里仅做存在性检查与告警,不强制创建。 if (RomaManager.Instance == null) { Debug.LogWarning("[LightGlueRuntime] RomaManager not found. Ensure LightGlueGameManager or RomaSystem prefab exists in scene."); } if (LightGlueUIManager.Instance == null) { Debug.LogWarning("[LightGlueRuntime] LightGlueUIManager not found. Ensure LightGlueUI prefab exists in scene."); } BindResultSource(); } public void StartRuntime() { var romaMgr = RomaManager.Instance; if (romaMgr == null) return; romaMgr.StartPython(); romaMgr.StartForwardViewer(); // 优先桥接层结果接收,避免与 RomaManager 抢占端口 var bridge = UnityEngine.Object.FindObjectOfType(); if (bridge != null) bridge.EnsureResultReceiverStarted(); else romaMgr.StartResultReceiver(); } public void StopRuntime() { var romaMgr = RomaManager.Instance; if (romaMgr == null) return; romaMgr.StopForwardViewer(); romaMgr.StopResultReceiver(); romaMgr.StopPython(); } public void SetUIRuntimeMode(LightGlueUIRuntimeMode mode) { var uiMgr = LightGlueUIManager.Instance; if (uiMgr != null) { var uiType = mode == LightGlueUIRuntimeMode.Plugin ? LightGlueUIManager.UIType.Plugin : LightGlueUIManager.UIType.Demo; uiMgr.SetUIType(uiType); } } public void SetHardwareMode(LightGlueHardwareMode mode) { var cfg = NetworkConfigManager.LoadConfig() ?? NetworkConfig.CreateDefault(); cfg.romaHardwareMode = (mode == LightGlueHardwareMode.Esp32) ? NetworkConfig.RomaHardwareMode.Esp32 : NetworkConfig.RomaHardwareMode.OrangePi; NetworkConfigManager.SaveConfig(cfg); } public void ShowPluginUI() { LightGlueUIManager.Instance?.ShowUI(); } public void HidePluginUI() { LightGlueUIManager.Instance?.HideUI(); } private void BindResultSource() { if (_resultSubscribed) return; //_lightGlueManager = LightGlueManager.Instance; //if (_lightGlueManager != null) //{ // _lightGlueManager.OnResultUpdated -= HandleResultUpdated; // _lightGlueManager.OnResultUpdated += HandleResultUpdated; // _resultSubscribed = true; // if (_lightGlueManager.HasLatestResult) // HandleResultUpdated(_lightGlueManager.LatestResult); // return; //} _romaManager = RomaManager.Instance; if (_romaManager != null) { _romaManager.OnResultUpdated -= HandleResultUpdated; _romaManager.OnResultUpdated += HandleResultUpdated; _resultSubscribed = true; if (_romaManager.HasLatestResult) HandleResultUpdated(_romaManager.LatestResult); } } private void HandleResultUpdated(LightGlueResult result) { var update = new LightGluePositionUpdate( position: result.CameraPosition, cameraLocation: result.CameraPosition, isValid: result.IsValid, numMatches: result.NumMatches, inliersRatio: result.InliersRatio); try { OnPositionUpdate?.Invoke(update); } catch (Exception e) { Debug.LogError(e); } } } }