1
\$\begingroup\$

I'm trying to get my head around using JSON files to store level data . Specifically in this example is to make my walls. (its a top-down game like pacman). Each wall prefab is a 1x1x1 cube. I just want the JSON to hold the x,y,z. I have seen tutorials about how best to deserialise the data into Unity and I think im fine doing that, but I am unsure of how exactly to word the JSON file.

I see the example on Unity docs says the format is like this:

{"level":1,"timeElapsed":47.5,"playerName":"Dr Charles Francis"}

so, I could just do something like:

{"index":0,"x":5,"y":1,"z":0}

but I know that there needs to be a bit above, similar to XML and of this im clueless of what it needs to contain.

I looked on Json.org but i didnt really understand it still. This is one example:

{
"glossary": {
    "title": "example glossary",
    "GlossDiv": {
        "title": "S",
        "GlossList": {
            "GlossEntry": {
                "ID": "SGML",
                "SortAs": "SGML",
                "GlossTerm": "Standard Generalized Markup Language",
                "Acronym": "SGML",
                "Abbrev": "ISO 8879:1986",
                "GlossDef": {
                    "para": "A meta-markup language, used to create markup languages such as DocBook.",
                    "GlossSeeAlso": ["GML", "XML"]
                },
                "GlossSee": "markup"
            }
        }
    }
}

}

But what is 'Glossary' and 'title' in the context of my game? All I currently have is a cube called Wall, with a script attached called Wall.cs that currently does nothing, but I will be making an array of Wall and plan on using the deserialised data from the json file to lay the Walls[] out using my GameController.cs

Once I know what to put above and below to open and close the JSON file off, im pretty sure I can get it working how I need it to. I hope that made some sense.

\$\endgroup\$
1
\$\begingroup\$

What you are looking for is probably an Array, unless you want to name each of your wall parts.

{
  "walls": [
    {
      "x": 1,
      "y": 1,
      "z": 1
    },
    {
      "x": 2,
      "y": 1,
      "z": 1
    },
    {
      "x": 3,
      "y": 1,
      "z": 1
    },
    {
      "x": 3,
      "y": 1,
      "z": 2
    }
  ]
}

Couple of quick tools to help:
https://jsonlint.com/
http://www.json.org/
http://json2csharp.com/

As for the C# serialization, getting this data into a class. You may be able to make a class that holds an array of Vector3 objects. I haven't done serialization with Unity specifically, but with C# alone.

\$\endgroup\$
  • \$\begingroup\$ Hi there, thanks this is just what i meant. However I'm still not sure of what to put above and below the actual data lines. I will check out those links now though and im still trrying some youtube videos to fill in the blanks fo rme, but its slow progress so far :\ \$\endgroup\$ – Big T Larrity Aug 15 '17 at 15:20
  • 1
    \$\begingroup\$ If that's all the data you want in that file, then that is a complete JSON object and should be valid syntax (after I remove the one last trailing comma on the last object). JSON is very lax, there's no required formality of defining what it holds. One of those coordinate systems could contain a fourth variable "w" that holds a secret recipe for making sausages. However, depending on how you code the serialized class, C# may complain when loading the data. \$\endgroup\$ – Chris Aug 15 '17 at 15:25
  • 2
    \$\begingroup\$ You could give Visual Studio Code a try. It's a free, lighweight editor that mirrors VS2015/2017, but is more in line with dealing with text based coding. Javascript, Html, JSON, etc. I typically use both side by side. \$\endgroup\$ – Chris Aug 15 '17 at 15:33
  • 1
    \$\begingroup\$ Yea, any text editor can make it. It's literally just a text file. I don't know if Np++ has tools that will validate as you write it, and format it for readability. Good luck. \$\endgroup\$ – Chris Aug 15 '17 at 15:37
  • 1
    \$\begingroup\$ You'd probably create a class that holds the three float values as x, y, z. And then when you've loaded all of those, convert them into Vector3 objects. \$\endgroup\$ – Chris Aug 15 '17 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.