unity serializer.Deserialize(stream)

谁能告诉我我做错了什么?当我在编辑器中玩游戏时,一切正常。当我构建游戏并在那里尝试保存游戏作品时,但在加载游戏数据时出现此错误: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;

}


慕的地6264312
浏览 108回答 1
1回答

繁星coding

在您的 XML 中,“乘客”是小写的。你的班级名称是大写的。将您的 XML 属性更改为<Passenger Name=""/>您还应该将其作为数组/列表,如下所示:<Passengers>&nbsp; &nbsp; <Passenger Name="Gary" Position="Driver" />&nbsp; &nbsp; <Passenger Name="Mary" Position="Front Passenger" />&nbsp; &nbsp; <Passenger Name="Jorgen" Position="Back Left Passenger" />&nbsp; &nbsp; <Passenger Name="Alien 1" Position="Gary's Stomach" /></Passengers>额外的为角色和乘客提供一个 ID,以便他们可以在保存文件中的任何上下文中被引用。“名称”是模棱两可的。那是唯一标识符吗?是显示它们时使用的字符串吗?<Characters>&nbsp; &nbsp; <Character Name="Chuck" ID="1" />&nbsp; &nbsp; ...</Characters>...<Passengers>&nbsp; &nbsp; <Passenger ID="1" />&nbsp; &nbsp; ...</Passengers>
打开App,查看更多内容
随时随地看视频慕课网APP