2016-07-20 1 views
1

게임을 저장하려고 할 때 오류가 발생하는 이유는 무엇입니까? 다음은 게임을 저장하려고 할 때 오류가 발생하는 이유는 무엇입니까? Unity C#

내 스크립트입니다

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

나는 해결책을 얻었습니다.

내 항목 클래스에는 gameobject 유형이 있고 playerData 클래스에는 gameObject 데이터 유형이 있기 때문에이 오류가 발생합니다.

이진 게임을 저장하는 것은 허용되지 않습니다.

항목 클래스 및 playerData 클래스에서 gameobject 유형을 제거하십시오.

감사합니다.

+0

안녕하세요 @dennisliu, 작동 했습니까? – Fattie

+0

안녕하세요 @JoeBlow, 네, 지금은 작동하고 있습니다 .. 문제는 gameobject 데이터 형식에 있습니다. 저장 데이터 사용 바이너리 포맷터 사용 gameobject 데이터 형식은 허용되지 않습니다. –

관련 문제