0
\$\begingroup\$

I know the title is a bit confusing because I don't really know how to phrase this. This is what I have:

[System.Serializable]
public class Sector
{
    public string name;
    public Vector3 sectorPosition;
    public float sectorRadius;

    public buildingComponent[] components;

}
[System.Serializable]
public class buildingComponent : ScriptableObject
{
    public string componentType;
    public Vector3 componentPosition;
    public Vector3 compenentRotation;


}

When I try converting this into a json string like this:

        Sector ssector = new Sector();
        ssector.name = "sTom";
        ssector.sectorPosition = Vector3.up;
        ssector.sectorRadius = 2;
        ssector.components = new buildingComponent[3];
        print("component length " + ssector.components.Length);
        ssector.components[1] = new buildingComponent();

        ssector.components[1].componentPosition = new Vector3 (1, 2, 3);


        string sdata = JsonUtility.ToJson(ssector);

I end up with the following:

{"name":"sTom","sectorPosition":{"x":0.0,"y":1.0,"z":0.0},"sectorRadius":2.0,"components":[{"instanceID":0},{"instanceID":-341752},{"instanceID":0}]}`

The components array is not actually being formatted, it's only saved with an instanceId or whatever this does. Is there any solution to this or will I have to go with an alternative formatting. I just want to save this data on a server but I only get one string to do so, so I have to format everything into one string. Thanks in advance!

\$\endgroup\$
8
  • 1
    \$\begingroup\$ I am not sure what JSON serializer you are using but with NewtonSoft when you convert it to a string it is a single string, single line. You may be needing to search for a specific function. Can you provide the serializer you are using? \$\endgroup\$
    – Slagmoth
    Apr 2, 2018 at 12:51
  • \$\begingroup\$ Oh I forgot to mention that I am using Unity3d and just the standard unity serializer which is part of the UnityEngine library. \$\endgroup\$
    – volairs22
    Apr 2, 2018 at 16:01
  • \$\begingroup\$ I think what you are are trying to ask is why does ssector.components[] not show all the properties in the JSON output instead of the instance information, correct? \$\endgroup\$
    – Slagmoth
    Apr 2, 2018 at 16:40
  • \$\begingroup\$ Let me give you some more context. I am trying to make a space multiplayer game in which players can build bases. Every base is a sector and players can claim a sector. Thing is, I want to save the players' bases when they leave. And I want to save the entire world with all the bases and sectors in one string, which is what I mean by formatting it. I just tried using newtonsoft but for some reason I get the following error: "JsonSerializationException: Self referencing loop detected for property 'normalized' with type 'UnityEngine.Vector3'. Path 'sectorPosition'." \$\endgroup\$
    – volairs22
    Apr 2, 2018 at 16:42
  • \$\begingroup\$ The reason I can only have one string is because I am saving this on a server which only allows me to save on string. I want other players to find other players' bases even when these players are not online. \$\endgroup\$
    – volairs22
    Apr 2, 2018 at 16:44

1 Answer 1

0
\$\begingroup\$

I am really new to Unity but have a C# developer so I did some digging and found the this forum question on Unity's site.

I think this is what you are looking for as an answer. It would appear that Unity's serialization of Monobehaviour has some magic on the backend that tells it what it requires only through those instance IDs.

This may also be of some help. He recommends saving the information explicitly.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .