【发布时间】:2016-08-19 12:44:54
【问题描述】:
我有一个游戏,玩家拿起武器,然后将它作为 GameObject 变量放置给我的玩家,称为“MainHandWeapon”,我试图通过场景更改来保存该武器,所以我试图保存它。我的处理方式如下:
public class Player_Manager : Character, Can_Take_Damage {
// The weapon the player has.
public GameObject MainHandWeapon;
public void Save()
{
// Create the Binary Formatter.
BinaryFormatter bf = new BinaryFormatter();
// Stream the file with a File Stream. (Note that File.Create() 'Creates' or 'Overwrites' a file.)
FileStream file = File.Create(Application.persistentDataPath + "/PlayerData.dat");
// Create a new Player_Data.
Player_Data data = new Player_Data ();
// Save the data.
data.weapon = MainHandWeapon;
data.baseDamage = BaseDamage;
data.baseHealth = BaseHealth;
data.currentHealth = CurrentHealth;
data.baseMana = BaseMana;
data.currentMana = CurrentMana;
data.baseMoveSpeed = BaseMoveSpeed;
// Serialize the file so the contents cannot be manipulated.
bf.Serialize(file, data);
// Close the file to prevent any corruptions
file.Close();
}
}
[Serializable]
class Player_Data
{
[SerializeField]
private GameObject _weapon;
public GameObject weapon{
get { return _weapon; }
set { _weapon = value; }
}
public float baseDamage;
public float baseHealth;
public float currentHealth;
public float baseMana;
public float currentMana;
public float baseMoveSpeed;
}
但我不断收到此设置错误:
SerializationException: Type UnityEngine.GameObject is not marked as Serializable.
我到底做错了什么?
【问题讨论】:
-
不是答案,但here 是指向已回答的类似问题的链接。我希望这会有所帮助。
-
乔伊,你必须“手工”完成。就是这么简单。 Unity 中根本没有序列化或“保存东西”——就这么简单。 (请注意,所谓的“序列化”类几乎完全没用;只是不是为了那个目的。)
-
注意。您说“我正在尝试通过场景更改来保存该武器……”为此,您只需使用 DontDestroyOnLoad。一个角色/任何东西会在场景中移动是完全司空见惯的。完全没有必要保存或序列化它。