简体   繁体   English

JsonUtility 将结构序列化为 JSON Unity

[英]JsonUtility serializing a struct to JSON Unity

I am some issues getting a struct to get serialized using Unity's JasonUtility.我在获取使用 Unity 的 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.当我使用统一实用程序将其序列化为 json 时,它会使我的数据结构生成一个 null 数组。

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.我试图只使用 2D arrays 但它完全忽略了这一点。 I went to structs and it seems to some what work.我去了结构,似乎有些工作。 It gets declared correctly in my player class.它在我的播放器 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.我相信出现您的问题是因为所有 integer arrays 都被声明为属性。 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.要进行序列化,数据应声明为公共字段或标有 [SerializedField] 属性的私有字段。

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.在控制台中 fieldArray 和 propertyArray2(私有字段)都被序列化了,而 data.PropertyArray 根本没有被序列化。 Since it was not serialized, if the struct was loaded from json it would be initialized to its default value (null).由于它没有被序列化,如果结构是从 json 加载的,它将被初始化为其默认值(null)。

I hope this helps you with your problem.我希望这可以帮助您解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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