SerializationException:类型UnityEngine.GameObject未标记为Serializable

时间:2014-10-08 05:19:18

标签: c# unity3d save unityscript

我想问一些事情。我正在制作一个游戏,我正试图像收获月亮一样保存和加载游戏数据,但我得到了这个错误。我在谷歌搜索过这种错误,但我不知道如何解决它。这是我的代码。

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未标记为   序列化。   System.Runtime.Serialization.Formatters.Binary.BinaryCommon.CheckSerializable   (System.Type类型,ISurrogateSelector选择器,StreamingContext   上下文)   /Users/builduser/buildslave/mono-runtime-and-classlibs/build/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)(at   /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.Binary.ObjectWriter,   System.IO.BinaryWriter,System.Object)   System.Runtime.Serialization.Formatters.Binary.ObjectWriter.WriteObject   (System.IO.BinaryWriter writer,Int64 id,System.Object obj)(at   /Users/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.WriteQueuedObjects   (System.IO.BinaryWriter writer)(at   /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)(at   /Users/builduser/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图,   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图)(at   /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Serialization.Formatters.Binary/BinaryFormatter.cs:211)   gameController.Save()(在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;
    }
}

请帮我解决这个问题。我从Unity的实时教程中了解了有关持久性保存和加载数据的知识,但制作此类代码的人并没有出现此错误。

1 个答案:

答案 0 :(得分:0)

我陷入了同样的错误...... 发现这是我的解决方案: (在你的情况下)

[System.NonSerialized]
private GameObject objPrince;

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

这意味着您无法保存(序列化)objPrince,但您可以在运行时使用实时引用。为了获得objPrince所需的预制件,我从资源中加载了#34; namePrince"每次我实例化。

除非玩家可以在游戏时间或其他类似的事情中编辑GameObject,否则除了玩家可以在其中保存游戏对象之外,其他原因并非如此。即便如此,你仍然可以保存使用过的"身体部位的名字"或者其他什么,并在需要时从Resources文件夹加载。

希望这仍有帮助。