为什么我在尝试保存游戏时出错?
以下是我的剧本:
player.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class player : MonoBehaviour {
public List<item> itemRaw = new List<item> ();
public List<item> itemVal = new List<item> ();
public List<item> itemAdm = new List<item> ();
public upgradeStorage OnUpStorage1Raw;
public upgradeStorage OnUpStorage2Raw;
public upgradeStorage OnUpStorage3Raw;
public int RawStorage1Level = 0;
public int totalSlotRawStorage1;
public int RawStorage2Level = 0;
public int totalSlotRawStorage2;
public int RawStorage3Level = 0;
public int totalSlotRawStorage3;
public bool RawStorage2Unlock = false;
public bool RawStorage3Unlock = false;
public string name;
public int level;
public int exp;
public int coin = 1000000;
public int gem = 30000;
public List<GameObject> slotRaw = new List<GameObject> ();
public List<GameObject> slotVal = new List<GameObject> ();
public List<GameObject> slotAdm = new List<GameObject> ();
public List<item> margaretItemSell = new List<item>();
public List<item> margaretItemBuy = new List<item>();
public List<productMerchant> productMargaretSell = new List<productMerchant> ();
public List<productMerchant> productMargaretBuy = new List<productMerchant> ();
public Dictionary <string, Dictionary <string, int> > productSellMargaret;
public Dictionary <string, Dictionary <string, int> > productBuyMargaret;
public player () {
}
void Awake () {
Load ();
}
void Update () {
Save ();
Debug.Log ("Coin : " + coin);
}
public void Save() {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/saveGame.gd");
playerData data = new playerData ();
data.itemRaw = itemRaw;
data.itemVal = itemVal;
data.itemAdm = itemAdm;
bf.Serialize (file, data);
file.Close ();
}
public void Load() {
if(File.Exists(Application.persistentDataPath + "/saveGame.gd")) {
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/saveGame.gd", FileMode.Open);
playerData data = (playerData) bf.Deserialize (file);
file.Close ();
itemRaw = data.itemRaw;
itemVal = data.itemVal;
itemAdm = data.itemAdm;
}
}
}
我还有另一个可序列化的脚本类:
playerData.cs
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class playerData {
public List<item> itemRaw = new List<item> ();
public List<item> itemVal = new List<item> ();
public List<item> itemAdm = new List<item> ();
}
我在方法save()或load()
时遇到此错误*EndOfStreamException: Failed to read past end of stream.
System.IO.BinaryReader.ReadByte () (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.IO/BinaryReader.cs:293)
System.Runtime.Serialization.Formatters.Binary.ObjectReader.ReadValue (System.IO.BinaryReader reader, System.Object parentObject, Int64 parentObjectId, System.Runtime.Serialization.SerializationInfo info, System.Type valueType, System.String fieldName, System.Reflection.MemberInfo memberInfo, System.Int32[] indices) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectReader.cs:727) and bla bla blab bla...........*
此脚本只保存并加载。
有什么想法吗?
由于
答案 0 :(得分:1)
我自己得到了解决方案。
这个错误,因为在我的item类中有一个gameobject类型,在playerData类中还有一个gameobject数据类型。
保存二进制格式游戏时不允许这样做。
从item class和playerData类中删除gameobject类型。
由于