LoadRawTextureData() not enough data provided error in Unity

允我心安 提交于 2019-12-11 11:24:18

问题


I am working on a project using ARcore.

I need a real world screen that is visible on the ARcore camera, formerly using the method of erasing UI and capturing.

But it was so slow that I found Frame.CameraImage.Texture in Arcore API

It worked normally in a Unity Editor environment.

But if you build it on your phone and check it out, Texture is null.

Texture2D snap = (Texture2D)Frame.CameraImage.Texture;

What is the reason? maybe CPU problem?

and I tried to do a different function.

public class TestFrameCamera : MonoBehaviour
{
    private Texture2D _texture;
    private TextureFormat _format = TextureFormat.RGBA32;

    // Use this for initialization
    void Start()
    {
        _texture = new Texture2D(Screen.width, Screen.height, _format, false, false);
    }

    // Update is called once per frame
    void Update()
    {

        using (var image = Frame.CameraImage.AcquireCameraImageBytes())
        {
            if (!image.IsAvailable) return;

            int size = image.Width * image.Height;
            byte[] yBuff = new byte[size];
            System.Runtime.InteropServices.Marshal.Copy(image.Y, yBuff, 0, size);

            _texture.LoadRawTextureData(yBuff);
            _texture.Apply();

            this.GetComponent<RawImage>().texture = _texture;

        }
    }
}

But if I change the texture format, it will come out.

private TextureFormat _format = TextureFormat.R8;

it is work, but i don't want to red color image, i want to rgb color

what i should do?


回答1:


R8 Just red data. You can use TextureFormat.RGBA32, and set buffer like this:

IntPtr _buff = Marshal.AllocHGlobal(width * height*4);


来源:https://stackoverflow.com/questions/55159113/loadrawtexturedata-not-enough-data-provided-error-in-unity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!