Search Unity

Tiled TSX format and JsonUtility

Discussion in 'Scripting' started by Dafu, May 4, 2018.

  1. Dafu

    Dafu

    Joined:
    Aug 22, 2006
    Posts:
    124
    Hi all,

    Tiled can save its TileSets in json format, here is an example:

    Code (CSharp):
    1. { "columns":8,
    2. "image":"MySprites.png",
    3. "imageheight":128,
    4. "imagewidth":128,
    5. "margin":0,
    6. "name":"testtsx",
    7. "spacing":0,
    8. "tilecount":64,
    9. "tileheight":16,
    10. "tileoffset":
    11.     {
    12.      "x":5,
    13.      "y":4
    14.     },
    15. "tileproperties":
    16.     {
    17.      "0":
    18.         {
    19.          "test":"str1",
    20.          "test2":"str2",
    21.          "test3":11
    22.         },
    23.      "1":
    24.         {
    25.          "test":1
    26.         },
    27.      "19":
    28.         {
    29.          "test":0
    30.         }
    31.     },
    32. "tilepropertytypes":
    33.     {
    34.      "0":
    35.         {
    36.          "test":"string",
    37.          "test2":"string",
    38.          "test3":"int"
    39.         },
    40.      "1":
    41.         {
    42.          "test":"int"
    43.         },
    44.      "19":
    45.         {
    46.          "test":"int"
    47.         }
    48.     },
    49. "tilewidth":16,
    50. "type":"tileset"
    51. }
    I'm having trouble loading this format using UnityEngine.JsonUtility. The problem is the field "tileproperties" and "tilepropertytypes". Both fields contain a list of tile ids and their corresponding property info. The problem is that the list is not a json array (using "[ ]") and the tile ids are numbers so they can't possibly map to variable names in C# since they don't start with a valid variable name character. I was hoping I could load "tileproperties" as a Dictionary of some sort, but the builtin Dictionary is not serializable, and serializable Dictionary implementations I saw work differently than this format as well.

    Any one have any idea if its possible to decode this json format with JsonUtility?
     
    Last edited: May 4, 2018
  2. Nopease

    Nopease

    Joined:
    May 3, 2020
    Posts:
    3
    why no replies guys.... hello
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Probably because it's just JSON and the guy got it working and you can too.

    Problems with Unity "tiny lite" built-in JSON:

    In general I highly suggest staying away from Unity's JSON "tiny lite" package. It's really not very capable at all and will silently fail on very common data structures, such as Dictionaries and Hashes and ALL properties.

    Instead grab Newtonsoft JSON .NET off the asset store for free, or else install it from the Unity Package Manager (Window -> Package Manager).

    https://assetstore.unity.com/packages/tools/input-management/json-net-for-unity-11347

    Also, always be sure to leverage sites like:

    https://jsonlint.com
    https://json2csharp.com
    https://csharp2json.io

    PS: for folks howling about how NewtonSoft JSON .NET will "add too much size" to your game, JSON .NET is like 307k in size, and it has the important advantage that it actually works the way you expect a JSON serializer to work in the year 2021.