简体   繁体   中英

JsonUtility serializing a struct to JSON Unity

I am some issues getting a struct to get serialized using Unity's JasonUtility. I read that it can serialize them, but it seems it is not doing so.

Here is my structure:

using System;
using UnityEngine;
// Classes Array
// Index:
// 0 Level,         1 Strength,     2 Endurance,    3 Intellect
// 4 Resistence,    5 Dexterity,    6 Evasion,      7 Haste
[Serializable]
public struct AllClasses
{
                            // Default Values
    public int[] Adventuer { get; set; } // { 1, 1, 1, 1, 1, 1, 1, 1 }
    public int[] Warrior { get; set; }   // { 0, 3, 1, 0, 0, 2, 0, 0 }
    public int[] Guardian { get; set; }  // { 0, 0, 3, 0, 2, 0, 1, 0 }
    public int[] Archer { get; set; }    // { 0, 2, 0, 0, 0, 3, 0, 1 }
    public int[] Rogue { get; set; }     // { 0, 1, 0, 0, 0, 0, 2, 3 }
    public int[] Cleric { get; set; }    // { 0, 0, 2, 1, 3, 0, 0, 0 }
    public int[] Wizard { get; set; }    // { 0, 0, 0, 3, 1, 0, 0, 2 }
    public int[] Tactician { get; set; } // { 0, 0, 0, 0, 2, 1, 3, 0 }

    public AllClasses(int[,] data)
    {
        // I correctly assign them here, consoling them shows correct values
        // removed this block for readability
    }    
}

When I use the unity utility to serialize it to json, it makes my data structure produce a null array.

I am storing them in player preferences, and I thought that might have been the cause of it. However, even just logging it before the actual save it is empty. I have been trying to figure this out for a couple days now. I tried to just use 2D arrays but it just flat out ignore that. I went to structs and it seems to some what work. It gets declared correctly in my player class. It is just the serialization of it.

EDIT: Forgot the place where I actually do this

   public static void CreateCharacter (Player player)
    {
        Text txtResults = GameObject.Find("CharacterCreationResultsText").GetComponent<Text>();
        Debug.Log(player.name);
        PlayerData playerData = new PlayerData(player);
        Debug.Log(playerData.id);
        Saves data = CharacterList();
        // Checks if a character already exists
        if (CheckSumCharacter(playerData, data))
            return;
        // Adds in the character to the list, and saves it.
        data.saves.Add(playerData);
        Debug.Log(JsonUtility.ToJson(data));
        ZPlayerPrefs.SetString("characters", JsonUtility.ToJson(data));
        ZPlayerPrefs.Save();
        txtResults.color = Color.green;
        txtResults.text = "Character Creation Successful!";
        MainMenu.AddCharacter(player.id);
    }

I believe that your problem arises because all of the integer arrays are declared as properties. From my tests it appears that properties will not be serialized. To be serialized the data should either be declared as a public field or a private field marked with the [SerializedField] attribute.

I did a little test to verify:

[System.Serializable]
public struct testStruct{
    public int[] fieldArray;

    public int[] propertyArray{get;set;}

    [SerializeField]
    //[HideInInspector]
    private int[] propertyArray2;
    public int[] PropertyArray2 { get => propertyArray2; set => propertyArray2 = value; }
}


public class JsonTest : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        testStruct data = new testStruct();
        data.fieldArray = new int[]{1,2,2,5};
        data.propertyArray = new int[]{1,1,1};
        data.PropertyArray2 = new int[]{2,2,2};
        Debug.Log(JsonUtility.ToJson(data));

    }
...

}

In the console fieldArray and propertyArray2(the private field) were both serialized, while data.PropertyArray was not serialized at all. Since it was not serialized, if the struct was loaded from json it would be initialized to its default value (null).

I hope this helps you with your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM