- Home /
Saving Data for a Class
Hello,
I'm creating an app for demos and I'm still fairly new to programming. I have a class that will have all my demo information and I am using ".GetComponent" to get the text entered into the input field to assign it to the appropriate information (really hoping this is right). What I need help with is how to save this class information to a file and then read the information later (will also need to rewrite when necessary).
Can someone point me to a good tutorial for saving a class to a file & reading it again later? TIA!
P.s. Let me know if you need more information.
Answer by LINKENN · Jan 20, 2018 at 11:00 PM
Hello, I am not sure what are you looking for namely, but I suppose something like this. When you character travels between different scenes, you can you use maybe this method. If you character travels from one scene to another then all information is going to save in one file together with changes and than you can load all information in another scene.
https://unity3d.com/ru/learn/tutorials/topics/scripting/persistence-saving-and-loading-data Persistence - Saving and Loading Data
I send 2 example scripts
first allows you to save and load data. There were you will see a bla stuff, you shoud need to add all you parametrs
// https://unity3d.com/ru/learn/tutorials/topics/scripting/persistence-saving-and-loading-data Persistence - Saving and Loading Data
// script works + public class AdjustScript : MonoBehaviour using System.Collections; using UnityEngine; using System; using System.Runtime.Serialization.Formatters.Binary; using System.IO;
public class GameControl : MonoBehaviour { public static GameControl control;
public float health;
public float experience;
public float bla; /// you need to add here all what you need
void Awake () // cames for Stort ()
{
if(control == null)
{
DontDestroyOnLoad (gameObject);
control = this;
}
else if(control != this)
{
Destroy (gameObject);
}
}
void OnGUI ()
{
GUI.Label (new Rect (10, 10, 100, 30), "Health: " + health);
GUI.Label (new Rect (10, 40, 100, 30), "Experience: " + experience);
}
// Update is called once per frame
public void Save ()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData ();
data.health = health;
data.experience = experience;
data.bla = bla; /// you need to add here all what you need
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);
PlayerData data = (PlayerData)bf.Deserialize(file);
file.Close ();
health = data.health;
experience = data.experience;
bla = data.bla; /// you need to add here all what you need
}
}
[Serializable] class PlayerData
{ public float health; public float experience; public float bla; /// you need to add here all what you need } }
now comes second script, this second script will allow you to change parameters while you are playing the game by GUI buttons
using System.Collections; using UnityEngine;
public class AdjustScript : $$anonymous$$onoBehaviour { // scrip works +public class GameControl : $$anonymous$$onoBehaviour :) // this buttons you can use for manipulation of paramets void OnGUI () { if(GUI.Button(new Rect (10, 100, 100, 30), "Health UP")) { GameControl.control.health += 10; } if(GUI.Button(new Rect (10, 140, 100, 30), "Health Down")) { GameControl.control.health -= 10; } if(GUI.Button(new Rect (10, 180, 100, 30), "Experience Up")) { GameControl.control.experience += 10; } if(GUI.Button(new Rect (10, 220, 100, 30), "Experience Down")) { GameControl.control.experience -= 10; } if(GUI.Button(new Rect (10, 340, 100, 30), "Bla")) // if(GUI.Button(new Rect ( 1200, 340, 100, 30), "Bla")) { GameControl.control.bla -= 10; /// you need to add here all what you need } if(GUI.Button(new Rect (10, 260, 100, 30), "Save")) { GameControl.control.Save(); } if(GUI.Button(new Rect (10, 300, 100, 30), "Load")) { GameControl.control.Load(); } }
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612150929im_/https://answers.unity.com/themes/thub/images/avi.jpg)