Unity serializer.Deserialize(stream)提供一个ArgumentNullException

时间:2019-03-07 18:18:22

标签: c# unity3d

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

[System.Serializable]
public class VehicleDetails
{
    [XmlAttribute("Name")]
    public string vehicleName;
    [XmlAttribute("IsCameraSubject")]
    public bool isCameraSubject;

    public Vec2Position Position;
    public Rotation Rotation;
    public Passenger passenger;
}

[System.Serializable]
public class PoliceSave
{
    public List<GameData> GameData;
    public List<CharacterDetails> Characters = new List<CharacterDetails>();
    public List<VehicleDetails> Vehicles = new List<VehicleDetails>();
}

Xml文件:

<?xml version="1.0" encoding="utf-8"?>
<PoliceSave xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <GameData>
    <GameData Funds="900" MissionID="13" />
  </GameData>
  <Characters>
    <CharacterDetails Name="PoliceOfficer_01" IsCameraSubject="true">
      <Position x="0.4215863" y="-2.17915773" />
      <Rotation z="349.367126" />
    </CharacterDetails>
  </Characters>
  <Vehicles>
    <VehicleDetails Name="PoliceCar_01" IsCameraSubject="false">
      <Position x="1.17827833" y="-2.75721359" />
      <Rotation z="33.9092827" />
      <passenger Name="" />
    </VehicleDetails>
  </Vehicles>
</PoliceSave>

错误:Link to the crash report

    Uploading Crash Report
ArgumentNullException: Value cannot be null.
Parameter name: elementType
  at System.Array.CreateInstance (System.Type elementType, System.Int32[] lengths) [0x00009] in <c6bd535f6ab848b4a13f34d01b756eef>:0 
  at System.Array.CreateInstance (System.Type elementType, System.Int32 length) [0x0000b] in <c6bd535f6ab848b4a13f34d01b756eef>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadListString (System.Xml.Serialization.XmlTypeMapping typeMap, System.String values) [0x00033] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.GetValueFromXmlString (System.String value, System.Xml.Serialization.TypeData typeData, System.Xml.Serialization.XmlTypeMapping typeMap) [0x00009] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadAttributeMembers (System.Xml.Serialization.ClassMap map, System.Object ob, System.Boolean isValueList) [0x0004b] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, System.Boolean isValueList, System.Boolean readBySoapOrder) [0x00000] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob) [0x00007] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, System.Boolean isNullable, System.Boolean checkType) [0x000c4] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Boolean isNullable, System.Boolean checkType) [0x0002e] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObjectElement (System.Xml.Serialization.XmlTypeMapElementInfo elem) [0x00066] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, System.Boolean isValueList, System.Boolean readBySoapOrder) [0x007dc] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob) [0x00007] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, System.Boolean isNullable, System.Boolean checkType) [0x000c4] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Boolean isNullable, System.Boolean checkType) [0x0002e] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObjectElement (System.Xml.Serialization.XmlTypeMapElementInfo elem) [0x00066] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadListElement (System.Xml.Serialization.XmlTypeMapping typeMap, System.Boolean isNullable, System.Object list, System.Boolean canCreateInstance) [0x000de] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadMembers (System.Xml.Serialization.ClassMap map, System.Object ob, System.Boolean isValueList, System.Boolean readBySoapOrder) [0x00558] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstanceMembers (System.Xml.Serialization.XmlTypeMapping typeMap, System.Object ob) [0x00007] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadClassInstance (System.Xml.Serialization.XmlTypeMapping typeMap, System.Boolean isNullable, System.Boolean checkType) [0x000c4] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadObject (System.Xml.Serialization.XmlTypeMapping typeMap, System.Boolean isNullable, System.Boolean checkType) [0x0002e] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot (System.Xml.Serialization.XmlTypeMapping rootMap) [0x0005e] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializationReaderInterpreter.ReadRoot () [0x00022] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.Serialization.XmlSerializationReader reader) [0x0005e] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializer.Deserialize (System.Xml.XmlReader xmlReader) [0x00026] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at System.Xml.Serialization.XmlSerializer.Deserialize (System.IO.Stream stream) [0x00015] in <82aef17918d14de3a4a7ef2f6032adc1>:0 
  at DataHandle.LoadGame () [0x0002c] in <c4c97695b8fc4125a27e468658af250b>:0 
  at UnityEngine.Events.InvokableCall.Invoke () [0x00011] in <a5bfa1597b74492eaabf3efc1f9c334a>:0 
  at UnityEngine.Events.UnityEvent.Invoke () [0x00023] in <a5bfa1597b74492eaabf3efc1f9c334a>:0 
  at UnityEngine.UI.Button.Press () [0x00027] in <a7b3e9f85ecb4036a981910deb3e21aa>:0 
  at UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) [0x00011] in <a7b3e9f85ecb4036a981910deb3e21aa>:0 
  at UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) [0x00008] in <a7b3e9f85ecb4036a981910deb3e21aa>:0 
  at UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) [0x00070] in <a7b3e9f85ecb4036a981910deb3e21aa>:0 
UnityEngine.DebugLogHandler:Internal_LogException()
UnityEngine.DebugLogHandler:LogException(Exception, Object)
UnityEngine.Logger:LogException(Exception, Object)
UnityEngine.Debug:LogException(Exception)
UnityEngine.EventSystems.ExecuteEvents:Execute(GameObject, BaseEventData, EventFunction`1)
UnityEngine.EventSystems.StandaloneInputModule:ProcessMousePress(MouseButtonEventData)
UnityEngine.EventSystems.StandaloneInputModule:ProcessMouseEvent(Int32)
UnityEngine.EventSystems.StandaloneInputModule:ProcessMouseEvent()
UnityEngine.EventSystems.StandaloneInputModule:Process()
UnityEngine.EventSystems.EventSystem:Update()

1 个答案:

答案 0 :(得分:0)

在您的XML中,“ passenger”为小写。您的班级名称是大写的。将您的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>