在我的 unity3d 游戏中,我有以下字典:
public Dictionary<string, List<UpgradeData>> upgradeList;
其中 UpgradeData 是一个非常简单的类:
public class UpgradeData
{
public bool lockStatus;
public bool purchased;
}
我在这里读到您不能将值类型用作 Dictionary 键,但据我所知 sting 不是值类型。因此问题必须来自使用 List 作为字典值。知道如何解决这个问题。该代码适用于 Windows 平台和 MacOS。我正在尝试使用二进制格式化程序序列化此字典。我在序列化其他类时遇到了类似的问题,但我设法通过添加来解决这个问题:
if (Application.platform == RuntimePlatform.IPhonePlayer)
{
System.Environment.SetEnvironmentVariable("MONO_REFLECTION_SERIALIZER", "yes");
}
我需要做什么来序列化字典?
编辑:忘记包含与字典一起使用的整个类
[Serializable]
public class CharacterUpgradeList
{
private UpgradeData[] _upgrade_Data;
private List<UpgradeData>[] upgData;
public Dictionary<string, List<UpgradeData>> upgradeList;
public CharacterUpgradeList()
{
upgData = new List<UpgradeData>[4];
for (int i = 0; i < upgData.Length; i++)
{
upgData[i] = new List<UpgradeData> {
new UpgradeData(),
new UpgradeData(),
new UpgradeData(),
new UpgradeData(),
new UpgradeData(),
new UpgradeData()
};
}
upgradeList = new Dictionary<string, List<UpgradeData>>
{
{"Man",upgData[0]},
{"Woman",upgData[1]},
{"Boy",upgData[2]},
{"Girl",upgData[3]}
};
}
}
然后我在这个类中从上面提到的类创建一个对象:
[Serializable]
class GameData
{
public int _coinAmount, _upgradeLevel;
public Level_Data[] _level_Data;
public CharacterUpgradeList _charUpgradeList;
public SerialVector2 serialVector;
public GameData()
{
_charUpgradeList = new CharacterUpgradeList();
}
}
我正在尝试序列化 GameData。