为啥我尝试保存游戏时出现错误?统一 C#

Posted

技术标签:

【中文标题】为啥我尝试保存游戏时出现错误?统一 C#【英文标题】:Why I have got an error when try save the game ? Unity C#为什么我尝试保存游戏时出现错误?统一 C# 【发布时间】:2016-07-20 05:45:18 【问题描述】:

为什么我在尝试保存游戏时遇到错误?

下面是我的脚本:

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...........*

这个脚本只是保存和加载。

有什么想法吗?

谢谢

【问题讨论】:

【参考方案1】:

我自己找到了解决方案。

这个错误是因为在我的项目类中有一个游戏对象类型,而在 playerData 类中也有一个游戏对象数据类型。

这在保存游戏二进制格式中是不允许的。

从 item 类和 playerData 类中移除游戏对象类型。

谢谢

【讨论】:

嗨@JoeBlow,是的,它现在正在工作..问题出在游戏对象数据类型上。保存数据使用二进制格式化程序使用游戏对象数据类型是不允许的。

以上是关于为啥我尝试保存游戏时出现错误?统一 C#的主要内容,如果未能解决你的问题,请参考以下文章

为啥在 Laravel 中保存数据时出现方法不允许错误?

为啥在尝试更新数据库时出现语法错误?

为啥在 C# .Net Core 3 中连接到 MySql 时出现错误

为啥在尝试安装 FiPy 时出现错误?

为啥我在尝试用 110 渲染站点时出现错误?

为啥我尝试在真实设备中运行代码时出现以下错误?