| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using LightGlue.Unity.Config;
- using LightGlue.Unity.Networking;
- using LightGlue.Unity.Python;
- using LightGlue.Unity.Sdk.Core;
- using UnityEngine;
- namespace LightGlue.Unity.Sdk.Unity
- {
- /// <summary>
- /// SDK 原型版:供第三方/当前工程直接挂在场景中的桥接组件。
- /// 内部持有 LightGlueClient,并在 Unity 生命周期中驱动它。
- /// </summary>
- 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;
- /// <summary>
- /// 最新的算法结果(每帧由 TryGetLatestResult 刷新)
- /// </summary>
- 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;
- }
- }
- /// <summary>
- /// 供 UI 调用:更新图像传输配置(Unity->Python)。
- /// </summary>
- public void SetTransmissionConfig(ImageTransmissionConfig config)
- {
- transmissionConfig = config;
- if (_client != null)
- {
- _client.SetTransmissionConfig(config);
- }
- }
- /// <summary>
- /// 供 UI 调用:下发硬件图像参数(0x40 协议)。
- /// </summary>
- public void ApplyHardwareConfig(ImageTransmissionConfig config)
- {
- if (_client != null)
- {
- _client.ApplyHardwareConfig(config);
- }
- }
- /// <summary>
- /// 获取最新 JPEG 图(拷贝),方便外部 Viewer 使用。
- /// </summary>
- public byte[] GetLatestJpeg()
- {
- return _client != null ? _client.GetLatestJpegCopy() : null;
- }
- }
- }
|