为了账号安全,请及时绑定邮箱和手机立即绑定

unity serializer.Deserialize(stream)

unity serializer.Deserialize(stream)

C#
慕的地6264312 2022-10-15 14:33:32
谁能告诉我我做错了什么?当我在编辑器中玩游戏时,一切正常。当我构建游戏并在那里尝试保存游戏作品时,但在加载游戏数据时出现此错误:ArgumentNullException: Value cannot be null. 我一直在寻找解决方案,但似乎找不到任何解决方案。public void SaveGame(){    checkGameData();    XmlSerializer serializer = new XmlSerializer(typeof(PoliceSave));    FileStream stream = new FileStream(Application.persistentDataPath + "/gameData.xml", FileMode.Create);    serializer.Serialize(stream, policeSave);    stream.Close();    policeSave.GameData.Clear();    policeSave.Characters.Clear();    policeSave.Vehicles.Clear();    vehicleDetails.passenger.characters.Clear();    Debug.Log("[Server] Your game has been saved.");}public void LoadGame(){    clearGameObjects();    XmlSerializer serializer = new XmlSerializer(typeof(PoliceSave));    FileStream stream = new FileStream(Application.persistentDataPath + "/gameData.xml", FileMode.Open);    //try    //{        policeSave = serializer.Deserialize(stream) as PoliceSave;    //}catch    //{        Debug.Log("[Server] Error while loading game data!");    //}    stream.Close();    loadGameObjects();    policeSave.GameData.Clear();    policeSave.Characters.Clear();    policeSave.Vehicles.Clear();    vehicleDetails.passenger.characters.Clear();    Debug.Log("[Server] Your game has been loaded.");}[System.Serializable]public class GameData{    [XmlAttribute("Funds")]    public int funds;    [XmlAttribute("MissionID")]    public int missionId;}[System.Serializable]public class Passenger{    [XmlAttribute("Name")]    public List<string> characters;}[System.Serializable]public class Vec2Position{    [XmlAttribute("x")]    public float x;    [XmlAttribute("y")]    public float y;}[System.Serializable]public class Rotation{    [XmlAttribute("z")]    public float z;}[System.Serializable]public class CharacterDetails{    [XmlAttribute("Name")]    public string characterName;    [XmlAttribute("IsCameraSubject")]    public bool isCameraSubject;    public Vec2Position Position;    public Rotation Rotation;}
查看完整描述

1 回答

?
繁星coding

TA贡献1797条经验 获得超4个赞

在您的 XML 中,“乘客”是小写的。你的班级名称是大写的。将您的 XML 属性更改为<Passenger Name=""/>


您还应该将其作为数组/列表,如下所示:


<Passengers>

    <Passenger Name="Gary" Position="Driver" />

    <Passenger Name="Mary" Position="Front Passenger" />

    <Passenger Name="Jorgen" Position="Back Left Passenger" />

    <Passenger Name="Alien 1" Position="Gary's Stomach" />

</Passengers>

额外的

为角色和乘客提供一个 ID,以便他们可以在保存文件中的任何上下文中被引用。“名称”是模棱两可的。那是唯一标识符吗?是显示它们时使用的字符串吗?


<Characters>

    <Character Name="Chuck" ID="1" />

    ...

</Characters>


...


<Passengers>

    <Passenger ID="1" />

    ...

</Passengers>


查看完整回答
反对 回复 2022-10-15
  • 1 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信