Search Unity

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

Discussion in 'Scripting' started by qcw27710, Nov 16, 2019.

  1. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    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: https://stackoverflow.com/questions...ble-to-find-a-constructor-to-use-for-type-pat
    No answer.

    2nd: https://forum.unity.com/threads/json-net-for-unity.200336/page-4
    It's because the JSON was invalid.

    3rd: https://stackoverflow.com/questions...xception-unable-to-find-constructor-to-use-fo
    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?
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    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?
     
  3. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    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:
    Code (CSharp):
    1.  
    2.         Texture2D t2d = new Texture2D(mapData.mapRenderMeta.width, mapData.mapRenderMeta.height);
    3.         t2d.LoadImage(mapData.mapRenderData);
    But that's after, the StackTrace points directly to the first piece of code.
     
  4. qcw27710

    qcw27710

    Joined:
    Jul 9, 2019
    Posts:
    139
    Note however that the mapRenderData doesn't look pure binary to me, it gives me impresson of base64:
    Code (CSharp):
    1. iVBORw0KGgoAAAANSUhEUgAAH6AAAB+gCAYAAAAnV7sHAAAgAElEQVR4AezdwQ0AIAwDMWBo1geJJbiHO0Hk9J859jnDESBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgAABAgQIECBAgMB3gfU9gQAECBAgQIAAAQIECBAgQIAAA
    This is just a sample. I encoded it using:

    Code (CSharp):
    1. byte[] bytes = File.ReadAllBytes($"{_demoDataDir_}/demomap.png");
    2.         Texture2D trueData = new Texture2D(8096, 8096);
    3.         trueData.LoadImage(bytes);
    4.  
    5.         ND nd = new ND
    6.         {
    7.             mapRenderData = trueData.EncodeToPNG(),
    8.             mapRenderMeta = trueData,
    9.             mapSize = 130
    10.         };
    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