Is it possible to save a whole scene in a file and then load it again? As a save/load system?
if it’s not possible, what’s the best way and easiest to make a save and load system for a game?
Is it possible to save a whole scene in a file and then load it again? As a save/load system?
if it’s not possible, what’s the best way and easiest to make a save and load system for a game?
You can serialize all objects that you have and deserialize them - that’s how Unity expects us to Save/Load. Search for “Unity Serialization” and such. Scenes are big “prefabs” with resources needed to load them; you can’t save them runtime.
I watched a youtube video explaining how to save, i tested and it works. but it only shows how to save a float,int etc
now my question is: How to save a game object?
And if I save a game object, which stats will go with that save? Transform(positions, rotations, etc), physics,scripts + script variables with each saved variable?
or do I need to save each of these individually?
for loading this gameobject, do I need to instantiate it?
Oh, right. You must’ve looked at just serialization without saving/loading.This tutorial explains saving/loading some data into file. You will still need to manually pass each and every object you want saved to it by calling bf.Serialize(file, object); and calling bf.Deserialize(file) with typecast of what you’ve written there.
yes, I’m doing that!
here’s my Save script
BinaryFormatter binary = new BinaryFormatter ();
FileStream fStream = File.Create (filePath);// parecido ao ini_open
// new reference
SaveManager saver = new SaveManager();
saver.minerals = GameObject.FindGameObjectWithTag ("Solar Station").GetComponent<solarStationController>().Minerals;
saver.energy = GameObject.FindGameObjectWithTag ("Solar Station").GetComponent<solarStationController>().Energy;
binary.Serialize (fStream,saver);
fStream.Close ();
but my problem now is saving gameobjects, and not variables
You can’t save out GameObjects in this way, you can, however, add any non-MonoBehaviour class marked as [System.Serializable] as an object and reload it.
I wrote this real quick and didn’t test it but I’ve written full serialization for 3 games and this is basically what I do. I store data in classes so it gives me full control over what should be serialized reducing unnecessary serialization which is great for performance.
public class SaverObjects
{
public static List<object> savedData = new List<object>();
public static void Save()
{
SaveManager saver = new SaveManager("Solar Station");
BinaryFormatter bf = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream();
bf.Serialize(memoryStream, saver.savedObjects);
string tmp = System.Convert.ToBase64String(memoryStream.ToArray());
PlayerPrefs.SetString("savedData", tmp);
}
public static void Load()
{
BinaryFormatter bf = new BinaryFormatter();
string tmp = string.Empty;
string prefsString = PlayerPrefs.GetString("savedData", tmp);
MemoryStream memoryStream = new MemoryStream(System.Convert.FromBase64String(prefsString));
List<object> objectList = (List<object>)bf.Deserialize(memoryStream);
SaveManager loader = new SaveManager("Solar Station");
loader.Reload(objectList);
}
}
[System.Serializable]
public class SaveManager
{
public SolarStationController solarStationController;
public List<object> savedObjects = new List<object>();
public SaveManager(string tagName)
{
solarStationController = GameObject.FindGameObjectWithTag(tagName).GetComponent<SolarStationController>();
savedObjects.Add(solarStationController.SaveData);
}
public void Reload(List<object> objData)
{
savedObjects = objData;
solarStationController.LoadData = objData[0];
}
}
public class SolarStationController : MonoBehaviour
{
private SolarControllerSaveData data = new SolarControllerSaveData();
public void DepleteEnergy(float amt)
{
data.energy -= amt;
}
public object SaveData
{
get
{
return data;
}
}
public object LoadData
{
set { data = (SolarControllerSaveData)value; }
}
[System.Serializable]
private class SolarControllerSaveData
{
public float minerals = 50;
public float energy = 25;
}
}
I fixed it up and ran a test it should work now. I just wanted to demonstrate really how you can encapsulate data in classes (or structs but that’s another topic) and save those classes out as objects. It gives you a much finer control over what exactly you want to be saving and loading in your game. By saving and loading only what’s necessary, you save hard disk space for the player and you increase performance.
That sounds kinda complex for me to understand haha
1 question:
I come from Game Maker, and to save objects there, for example if I wanted to save All the asteroids in my map, I would first save a variable that would store the total number of asteroids. Then I would save the x,y position and other variables needed.
then for loading, do a for() loop, from 1 to asteroidsNumber, and create an asteroid instance and load its variables(x,y,etc).
I think this can work for Unity too, what do you say? it sounds easier for me than saving directly gameobjects
It will work if you save all its variables(and load too). If you forget some… Depends.
mhmm which variables are you refering to?
Transform’s position, rotation, scale; if objects have other components… Any variables they have?
On my game, rotations aren’t needed! I guess I’ll try to do it! Thank you for the answers