Hololens - Unity:LoadImage 冻结主线程

Hololens - Unity: LoadImage freezes main thread

现在我有一个客户端和服务器。服务器正在截图并以字节为单位发送给我的客户端。


我在这里获取字节并将它们放入我的纹理中:

private IEnumerator RenderImage(byte[] values)
{
    var texture = new Texture2D(128, 128);
    texture.LoadImage(values); // <-- is running on main thread -> lag
    texture.Apply();
    RawImage.texture = texture;

    yield return null;
}

这可行,但它会导致全息透镜短暂冻结,大约 1/2 秒。

1. 我读过很多次 LoadImage 将 运行 on main thread 你不能把它移到别的地方。
2. 使用 Texture2D.LoadRawTextureData 应该可以更快地解决我的问题。但是使用它,它会抛出我没有提供足够数据的异常。写类似
var texture = new Texture2D(128, 128, TextureFormat.RGBA32, false); 的东西不会解决问题。

某人是否知道如何以更有效的方式编码字节,或者主线程不会因解码图像而卡住的方式?

--编辑-- 我忘了说我正在使用这个 Main-Thread-Dispatcher,但它没有帮助:

private void OnMessageReceived(byte[] values)
{
    UnityMainThreadDispatcher.Instance().Enqueue(RenderImage(values));
}

private IEnumerator RenderImage(byte[] values)
{
    var texture = new Texture2D(128, 128, TextureFormat.RGBA32, false);
    texture.LoadImage(values); // <-- is running on main thread -> lag
    texture.Apply();
    RawImage.texture = texture;

    yield return null;
}

好的,我通过使用服务器和这个 Nativ Render Plugin 解决了这个问题。渲染插件使用 Direct3D 和 RenderAPI 来设置纹理。