为什么我有一个错误,当试图挽回败局?统一C# [英] Why I have got an error when try save the game ? Unity C#

查看:929
本文介绍了为什么我有一个错误,当试图挽回败局?统一C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么尝试保存游戏的时候,我得到一个错误

Why i got an error when try saving the game ?

下面是我的脚本:

播放器的.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;

        }
    }
}

和我还有一个脚本类序列化的:

And i have another script class serializable :

playerData.cs

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> ();


}



我得到这个错误,而方法保存()或load()

I got this error while the method save() or 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...........*

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

This script just save and load.

任何想法?

感谢

推荐答案

我得到了解决自己。

这个错误,因为在我的项目类有一个游戏物体类型在playerData类此外,还有一个游戏对象数据类型。

This error because in my item class there is a gameobject type and in playerData class there also a gameobject data type.

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

That is not permitted in Saving games binary formated.

删除从项目类别和playerData类游戏对象类型。

Remove the gameobject type from item class and playerData class.

感谢

这篇关于为什么我有一个错误,当试图挽回败局?统一C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆