- Home /
Question by
kevinamoin23 · Sep 11, 2016 at 04:43 PM ·
gameobjectsaveload
Hello I have a script in which saving a character position but how can I save a text value also or gameobject? when the player save the game? This is my code for saving the position of the player.
public void SaveState()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/PlayerData.dat");
PlayerData data = new PlayerData();
data.posx = transform.position.x;
data.posy = transform.position.y;
data.posz = transform.position.z;
data.rotx = transform.eulerAngles.x;
data.roty = transform.eulerAngles.y;
data.rotz = transform.eulerAngles.z;
bf.Serialize(file, data);
file.Close();
}
public void LoadState()
{
if(File.Exists(Application.persistentDataPath + "/PlayerData.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/PlayerData.dat", FileMode.Open);
PlayerData data = (PlayerData) bf.Deserialize(file);
file.Close();
transform.position = new Vector3(data.posx, data.posy, data.posz);
transform.rotation = Quaternion.Euler(data.rotx, data.roty, data.rotz);
}
}
[Serializable]
class PlayerData
{
public float posx;
public float posy;
public float posz;
public float rotx;
public float roty;
public float rotz;
}
Comment
Please use the title space reasonably. It's irritating to have the whole question condensed into the title.
To save a string ("text value"), add a string variable to the PlayerData class and assign it as with any of the other variables.
GameObject, and all other Unity-specific Types, can't be serialized directly without implementing SerializationSurrogates. $$anonymous$$SDN is your friend :)
You might find my free SerializeHelper utility helpful. SerializeHelper - Free save and load utility. (De)Serialize all objects in your scene.
hello please eloborate your answer I cant really understand clearly because Im a newbie. $$anonymous$$aybe you a sample code in which I can clearly understand . Btw thanks for the response :D godbless :)
public void SaveState()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/PlayerData.dat");
PlayerData data = new PlayerData();
data.posx = transform.position.x;
data.posy = transform.position.y;
data.posz = transform.position.z;
data.rotx = transform.eulerAngles.x;
data.roty = transform.eulerAngles.y;
data.rotz = transform.eulerAngles.z;
data.myText = "Hello World!";//added line
bf.Serialize(file, data);
file.Close();
}
public void LoadState()
{
if(File.Exists(Application.persistentDataPath + "/PlayerData.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/PlayerData.dat", File$$anonymous$$ode.Open);
PlayerData data = (PlayerData) bf.Deserialize(file);
file.Close();
transform.position = new Vector3(data.posx, data.posy, data.posz);
transform.rotation = Quaternion.Euler(data.rotx, data.roty, data.rotz);
Debug.Log("Text saved in loaded data: " + data.myText);//added line
}
}
[Serializable]
class PlayerData
{
public float posx;
public float posy;
public float posz;
public float rotx;
public float roty;
public float rotz;
public string myText;//added line
}