【发布时间】: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);
}
【问题讨论】: