2014-10-08 114 views
0

我想問一些問題。我正在製作一款遊戲,並且我正在嘗試保存和加載遊戲的數據,就像收穫月亮一樣,但是我得到了這個錯誤。我在谷歌搜索了這種錯誤,但我不知道如何解決它。這是我的代碼。SerializationException:類型UnityEngine.GameObject未標記爲可序列化

using UnityEngine; 
using System.Collections; 
using System; 
using System.Collections.Generic; 
using System.Runtime.Serialization.Formatters.Binary; 
using System.IO; 

public class gameController : MonoBehaviour { 
    public static gameController control; 
    public basicskill bs; 

    // Use this for initialization 
    void Awake() { 
     if(control == null) 
     { 
      DontDestroyOnLoad(gameObject); 
      control = this; 
     } 
     else if(control != this) 
     { 
      Destroy(gameObject); 
     } 

    } 

    void OnGUI(){ 
     GUI.Label (new Rect (10, 10, 100, 30), "Health: " + bs.charPrince.health); 
     GUI.Label (new Rect (10, 40, 140, 30), "Experience: " + bs.charPrince.exp); 
    } 

    public void Save(){ 

     BinaryFormatter bf = new BinaryFormatter(); 
     FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat"); 

     Prince data = new Prince(); 
     data.name = bs.charPrince.name; 
     data.birthday = bs.charPrince.birthday; 
     data.hadapAtas = bs.charPrince.hadapAtas; 
     data.hadapKanan = bs.charPrince.hadapKanan; 
     data.locationY = bs.charPrince.locationY; 
     data.locationX = bs.charPrince.locationX; 
     data.exp = bs.charPrince.exp; 
     data.health = bs.charPrince.health; 
     bf.Serialize (file, data); 
     file.Close(); 
    } 

    public void Load(){ 
     if(File.Exists(Application.persistentDataPath + "/playerInfo.dat")) 
     { 
      BinaryFormatter bf = new BinaryFormatter(); 
      FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open); 
      Prince data = (Prince)bf.Deserialize(file); 
      file.Close(); 

      bs.charPrince.name = data.name; 
      bs.charPrince.birthday = data.birthday ; 
      bs.charPrince.hadapAtas = data.hadapAtas ; 
      bs.charPrince.hadapKanan = data.hadapKanan; 
      bs.charPrince.locationY = data.locationY; 
      bs.charPrince.locationX = data.locationX; 
      bs.charPrince.exp = data.exp; 
      bs.charPrince.health = data.health; 
     } 
    } 

    // Update is called once per frame 
    void Update() { 

    } 
} 

從上面代碼中,我得到了錯誤

SerializationException:類型UnityEngine.GameObject沒有標記爲 Serializable接口。 System.Runtime.Serialization.Formatters.Binary.BinaryCommon.CheckSerializable (System.Type的類型,ISurrogateSelector選擇器,的StreamingContext 上下文)(在 /用戶/ builduser/buildslave /單運行時和 - classlibs /建造/ MCS/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryCommon.cs:119) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteValue (System.IO.BinaryWriter writer,System.Type valueType,System。 Object val)(在 /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:732) Prince__TypeMetadata.WriteObjectData (System.Runtime.Serialization.Formatters.Binar y.ObjectWriter, System.IO.BinaryWriter,System.Object的) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject (System.IO.BinaryWriter作家,Int64類型ID,System.Object的OBJ)(在 /用戶/ builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:360) System.Runtime.Serialization.Formatters.Binary.ObjectWriter .WriteObjectInstance (System.IO.BinaryWriter writer,System.Object obj,Boolean isValueObject)(at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime。 Serialization.Formatters.Binary/ObjectWriter.cs:293) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteQu euedObjects (System.IO.BinaryWriter writer)(位於 /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs: 271) System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObjectGraph (System.IO.BinaryWriter writer,System.Object obj, System.Runtime.Remoting.Messaging.Header [] headers)(在 /Users /建立/ buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/ObjectWriter.cs:256) System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Serialize (System.IO.Stream serializationStream,System.Object graph, System.Runtime.Remoting.Messaging.Header [] headers)(at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:232) System.Runtime.Serialization.Formatters.Binary .BinaryFormatter.Serialize (System.IO.Stream serializationStream,System.Object圖)(位於 /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization。 (在Assets/Scripts/gameController.cs中:45) adjustScript.OnGUI()(在Assets/Scripts/adjustScript中。CS:32)

,這是我的其他代碼

using UnityEngine; 
using System.Collections; 
using System; 

public class adjustScript : MonoBehaviour { 
    void OnGUI(){ 
     if(GUI.Button(new Rect(10,100,100,30),"Health Up")) 
     { 
      gameController.control.bs.charPrince.health += 10; 
     } 
     if(GUI.Button(new Rect(10,140,100,30),"Health Down")) 
     { 
      if(gameController.control.bs.charPrince.health >0) 
      { 
       gameController.control.bs.charPrince.health -= 10; 
      } 
     } 
     if(GUI.Button(new Rect(10,180,100,30),"Experience Up")) 
     { 
      gameController.control.bs.charPrince.exp += 10; 

     } 
     if(GUI.Button(new Rect(10,220,100,30),"Experience Down")) 
     { 
      if(gameController.control.bs.charPrince.exp >0) 
      { 
       gameController.control.bs.charPrince.exp -= 10; 
      } 
     } 
     if(GUI.Button(new Rect(10,260,100,30),"Save")) 
     { 
      gameController.control.Save(); 

     } 
     if(GUI.Button(new Rect(10,300,100,30),"Load")) 
     { 
      gameController.control.Load(); 

     } 
    } 
} 



using UnityEngine; 
using System.Collections; 
using System.Collections.Generic; 
using System; 

[Serializable] 
public class Prince : Character{ 
    public float health = 100f; 
    public float exp = 0f; 
    public float kecMaks = 0f; 
    public float moveV = 0f; 
    public float moveH = 0f; 
    public bool hadapKanan = false; 
    public bool hadapAtas = true; 
    public GameObject objPrince; 

    public Prince() 
    { 
     this.name = ""; 
     this.birthday = ""; 
     this.objPrince = null; 
    } 
    public Prince(GameObject objChar,string nama, string birthday){ 
     this.name = nama; 
     this.birthday = birthday; 
     this.objPrince = objChar; 
    } 
} 


using UnityEngine; 
using System.Collections; 
using System; 

[Serializable] 
public class Character { 
    public string name; 
    public string birthday; 
    public float locationX; 
    public float locationY; 

    public Character(){ 
     this.name = ""; 
     this.birthday = ""; 
     this.locationX = 0f; 
     this.locationY = 0f; 
    } 
} 

請幫我解決這個問題。我從統一的關於持久性保存和加載數據的實時教程中瞭解到這一點,但是製作這種代碼的人沒有這個錯誤。

+1

錯誤消息描述了什麼是錯的。你能將GameObject標記爲Serializable嗎?教程是否真的提供了Prince類必須具有GameObject屬性? – Reniuz 2014-10-08 05:29:12

+0

'this'是你引用當前的'Prince'實例 - 你不需要'GameObject objPrince'嗎? 'Prince'類標記爲[[Serializable]],但不能序列化'GameObject',因此您需要將其刪除或[使其可序列化](http://blogs.unity3d.com/2014/06/ 24 /串行化功能於統一/) – 2014-10-08 08:32:24

回答

0

我鬱悶到了同樣的錯誤...... 發現這對我來說是解決方案: (你的情況)

[System.NonSerialized] 
private GameObject objPrince; 

public GameObject ObjPrince{get{return objPrince;}set{objPrince = value;}} 

這並不意味着你不能保存(序列化)的objPrince,但您可以在運行時擁有實時參考。爲了得到所需的objPrince預製件,我每次實例化時都會通過「namePrince」從資源中加載它。

此外,大多數情況下,除非玩家可以在遊戲時間或類似情況下編輯遊戲對象,否則遊戲對象應該保存起來並不是真正的原因。即使這樣你仍然可以從使用的「身體部位」或其他任何東西中保存名字,並在需要時從Resources文件夾中加載。

希望這仍然有幫助。

相關問題