我有一个游戏,在这个游戏中,玩家会拾取武器,并将其作为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.
我到底做错了什么?