【问题标题】:JsonUtility serializing a struct to JSON UnityJsonUtility 将结构序列化为 JSON Unity
【发布时间】:2019-10-30 00:50:20
【问题描述】:

我在使用 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
    }    
}

当我使用 unity 实用程序将其序列化为 json 时,它会使我的数据结构产生一个空数组。

我将它们存储在玩家偏好中,我认为这可能是它的原因。但是,即使只是在实际保存之前记录它,它也是空的。几天来,我一直试图弄清楚这一点。我试图只使用二维数组,但它完全忽略了这一点。我去了结构,似乎有些工作。它在我的播放器类中正确声明。只是它的序列化而已。

编辑:忘记了我实际这样做的地方

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

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    我相信您的问题出现是因为所有整数数组都被声明为属性。从我的测试看来,属性不会被序列化。要进行序列化,数据应声明为公共字段或标有 [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)。

    我希望这可以帮助您解决问题。

    【讨论】:

    • 哇,我什至不认为这是个问题。非常感谢它现在运行良好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 1970-01-01
    • 2017-03-18
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多