LoadRawTextureData()没有足够的数据在统一提供的错误(LoadRawTexture

2019-09-26 16:31发布

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?

Answer 1:

R8正当红的数据。 您可以使用TextureFormat.RGBA32 ,并设置缓冲区是这样的:

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


文章来源: LoadRawTextureData() not enough data provided error in Unity