using LightGlue.Unity.Config; using LightGlue.Unity.Networking; using LightGlue.Unity.Python; using LightGlue.Unity.Sdk.Core; using UnityEngine; namespace LightGlue.Unity.Sdk.Unity { /// /// SDK 原型版:供第三方/当前工程直接挂在场景中的桥接组件。 /// 内部持有 LightGlueClient,并在 Unity 生命周期中驱动它。 /// public sealed class LightGlueBridgeBehaviour : MonoBehaviour { [Header("配置管理")] [Tooltip("是否从NetworkConfigManager自动加载网络配置")] public bool autoLoadNetworkConfig = true; [Header("Hardware UDP (input: hardware -> Unity)")] public string hardwareBindIp = "192.168.0.105"; public int hardwarePort = 12346; public float hardwareTimeoutSeconds = 2.0f; public int maxQueuedFrames = 2; [Header("Hardware Control UDP (Unity -> Hardware)")] public string hardwareControlIp = "192.168.0.106"; public int hardwareControlPort = 8888; [Header("Python Output Mode")] public LightGlueClient.TransferMode transferMode = LightGlueClient.TransferMode.Stdin; [Header("Python UDP (legacy mode: Unity -> Python)")] public string pythonIp = "127.0.0.1"; public int pythonPort = 12347; [Header("Python Process (stdin mode: Unity -> Python)")] public PythonProcessController pythonProcessController; [Header("Python Result Receiver (Python -> Unity)")] public bool enableResultReceiver = true; public string pythonResultBindIp = "127.0.0.1"; public int pythonResultPort = 12348; public int maxResultQueueSize = 10; [Header("Image Transmission Config")] [Tooltip("图像传输配置(可由 UI 写入)")] public ImageTransmissionConfig transmissionConfig; /// /// 最新的算法结果(每帧由 TryGetLatestResult 刷新) /// public LightGlueResult LatestResult { get; private set; } private LightGlueClient _client; private void Start() { if (autoLoadNetworkConfig) { TryLoadNetworkConfig(); } var options = new LightGlueClient.Options { HardwareBindIp = hardwareBindIp, HardwarePort = hardwarePort, HardwareTimeoutSeconds = hardwareTimeoutSeconds, MaxQueuedFrames = maxQueuedFrames, HardwareControlIp = hardwareControlIp, HardwareControlPort = hardwareControlPort, Mode = transferMode, PythonIp = pythonIp, PythonPort = pythonPort, PythonController = pythonProcessController, EnableResultReceiver = enableResultReceiver, PythonResultBindIp = pythonResultBindIp, PythonResultPort = pythonResultPort, MaxResultQueueSize = maxResultQueueSize }; _client = new LightGlueClient(options); try { _client.Start(); } catch { // 端口冲突等错误已经在 client 内部打 log,这里防止异常中断游戏。 } if (transmissionConfig != null) { _client.SetTransmissionConfig(transmissionConfig); } } private void TryLoadNetworkConfig() { try { var config = NetworkConfigManager.LoadConfig(); if (config != null && config.Validate()) { hardwareBindIp = config.hardwareBindIp; hardwarePort = config.hardwarePort; hardwareTimeoutSeconds = config.hardwareTimeoutSeconds; pythonIp = config.pythonIp; pythonPort = config.pythonPort; pythonResultBindIp = config.pythonResultBindIp; pythonResultPort = config.pythonResultPort; hardwareControlIp = config.hardwareControlIp; hardwareControlPort = config.hardwareControlPort; Debug.Log($"[SDK][Bridge] Loaded network config: hardware={hardwareBindIp}:{hardwarePort}, python={pythonIp}:{pythonPort}, result={pythonResultBindIp}:{pythonResultPort}, hwCtrl={hardwareControlIp}:{hardwareControlPort}"); } } catch (System.Exception ex) { Debug.LogWarning($"[SDK][Bridge] Load NetworkConfig failed, use Inspector values. {ex.Message}"); } } private void Update() { if (_client == null) return; _client.Tick(); if (enableResultReceiver && _client.TryGetLatestResult(out var result)) { LatestResult = result; } } private void OnDisable() { if (_client != null) { _client.Dispose(); _client = null; } } /// /// 供 UI 调用:更新图像传输配置(Unity->Python)。 /// public void SetTransmissionConfig(ImageTransmissionConfig config) { transmissionConfig = config; if (_client != null) { _client.SetTransmissionConfig(config); } } /// /// 供 UI 调用:下发硬件图像参数(0x40 协议)。 /// public void ApplyHardwareConfig(ImageTransmissionConfig config) { if (_client != null) { _client.ApplyHardwareConfig(config); } } /// /// 获取最新 JPEG 图(拷贝),方便外部 Viewer 使用。 /// public byte[] GetLatestJpeg() { return _client != null ? _client.GetLatestJpegCopy() : null; } } }