Texture2D load PNG with LoadRawTextureData

Hi everyone,

I try to load a png (2048px x 2560px) file into a Texture2D with the function LoadRawTextureData :

private Texture2D ImportModelTexture(string filePath)
        {
            Texture2D texture = new Texture2D(2048, 2560, TextureFormat.RGB24, false);
            byte[] bytes = m_localStorage.LoadFileAsBytes(filePath);
            texture.LoadRawTextureData(bytes);
            texture.Apply();
            return texture;
        }

But this exception append:

UnityException: LoadRawTextureData: not enough data provided (will result in overread).

I had tested different TextureFormat and still the same exception… I think i miss something, but I don’t know what.

Someone can help me?

Best,
Alexis

PS : I don’t used LoadImage because this function is to slow…

A PNG file is not raw RGB24 data. The error message is correct. And if LoadImage seems slow, that’s probably because it’s doing things like unpacking PNG data.

Hello,

Thanks for the response!
I have try some others TextureFormat to Load the PNG files but I have the same error. Because the PNG format isn’t a raw data?

To be more precise, I have around 60 images to load. I pull them for distant server, and I save it on local storage. After that I load the images (LoadImage function), and at this point the application freeze.
On computer, no problem, the application freeze and after few seconds keep going.
But on Android device, the application freeze and Android kill it…

After a lot of investigation, the problem come from memory (the device have just 2 Gb)…
So I need to redesign a part of me application!

Thanks again for your help!

Best,
Alexis

1 Like