How do I create an empty struct for Texture2D without throwing Unity into a river?

I have byte series that contain data for a structure, I’m planning to read from these bytes to recreate an image. Problem is that I get following error:

JsonSerializationException: Unable to find a constructor to use for type UnityEngine.Texture2D.

I did what I always do, go look it up, aaaaaand.

1st: c# - JsonSerializationException: Unable to find a constructor to use for type ... Path 'data', line 1, position 83 - Stack Overflow
No answer.

2nd: JSON .NET for Unity page-4
It’s because the JSON was invalid.

3rd: c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class - Stack Overflow
Finally an answer. I get a mention of empty constructor, post below tells me how to do it, but I don’t know which method/class to place it on, further more I’m afraid that if I put something inside Texture2D’s class that it will permanently modify every Texture2D’s behavior and I will get errors/complications that I wouldn’t otherwise get.

So the question is, how do I give Texture2D an “empty construct” without it snapping on me? Or otherwise how do I turn binary data into Texture2D data?

Not sure if I understand your issue here, but usually you create a Texture2D with correct dimensions and texture format, then use SetPixel/SetPixels/SetPixels32 to insert the image data to the texture. Just get your binary data to Unity first, then write the data content to the texture. Wouldn’t this work for you?

That’s exactly what I’m trying to do and this fails:

return JsonConvert.DeserializeObject<T>(data);
Where T is Texture2D.
Where data is the data.

And then:

        Texture2D t2d = new Texture2D(mapData.mapRenderMeta.width, mapData.mapRenderMeta.height);
        t2d.LoadImage(mapData.mapRenderData);

But that’s after, the StackTrace points directly to the first piece of code.

Note however that the mapRenderData doesn’t look pure binary to me, it gives me impresson of base64:

iVBORw0KGgoAAAANSUhEUgAAH6AAAB+gCAYAAAAnV7sHAAAgAElEQVR4AezdwQ0AIAwDMWBo1geJJbiHO0Hk9J859jnDESBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgMB3gfU9gQAECBAgQIAAAQIECBAgQIAAA

This is just a sample. I encoded it using:

byte[] bytes = File.ReadAllBytes($"{_demoDataDir_}/demomap.png");
        Texture2D trueData = new Texture2D(8096, 8096);
        trueData.LoadImage(bytes);

        ND nd = new ND
        {
            mapRenderData = trueData.EncodeToPNG(),
            mapRenderMeta = trueData,
            mapSize = 130
        };

For a reason its unnecessary to explain, I take a working image, then write its binaries down in a file, then want to read that file back, which now fails.

if you read this post, I wrote another one above it as well