簡體   English   中英

JsonUtility 將結構序列化為 JSON Unity

[英]JsonUtility serializing a struct to JSON Unity

我在獲取使用 Unity 的 JasonUtility 序列化結構時遇到了一些問題。 我讀到它可以序列化它們,但它似乎沒有這樣做。

這是我的結構:

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
    }    
}

當我使用統一實用程序將其序列化為 json 時,它會使我的數據結構生成一個 null 數組。

我將它們存儲在玩家偏好中,我認為這可能是它的原因。 但是,即使只是在實際保存之前記錄它,它也是空的。 幾天來,我一直試圖弄清楚這一點。 我試圖只使用 2D arrays 但它完全忽略了這一點。 我去了結構,似乎有些工作。 它在我的播放器 class 中正確聲明。 它只是它的序列化。

編輯:忘記了我實際這樣做的地方

   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);
    }

我相信出現您的問題是因為所有 integer arrays 都被聲明為屬性。 從我的測試看來,屬性不會被序列化。 要進行序列化,數據應聲明為公共字段或標有 [SerializedField] 屬性的私有字段。

我做了一個小測試來驗證:

[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));

    }
...

}

在控制台中 fieldArray 和 propertyArray2(私有字段)都被序列化了,而 data.PropertyArray 根本沒有被序列化。 由於它沒有被序列化,如果結構是從 json 加載的,它將被初始化為其默認值(null)。

我希望這可以幫助您解決問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM