Hello everyone i am building a building game in unity in which the player can build a city i want the player to be able to save the scene that he has build so he can go back to it when he plays next time,how would i be able to build the saving mechanism.Should I use a database like mongodb to save the position,rotation,scale,etc for the objects that the player has made and placed(but i think that would be too lengthy) or should i do something else.Pls Help
This for me is one of the best tutorials for it.
The ‘proper’ way to save in Unity is to serialise every element of data that needs to be saved, and write it to disk. You can in theory use a text file as above, or PlayerPrefs for quick prototypes, but PlayerPrefs is designed (as the class name suggests) to hold a small amount of information on preferred settings rather than a whole savegame.
You basically need to distil the core information into classes you can serialise, which means they need to be serialisable. This means breaking the data back down towards basic types (or serialisable ones like Vector3). Using GetComponentsOfType can be a big time-saver (e.g. you can get all rigidbodies then iterate through them saving position/rotation).
Saving (properly) in general is one of the trickier things to do in Unity and it’s best if you make it a priority when starting development, especially if ‘save anytime’ is a required feature. It’s particularly complicated if you’re adding/instantiating a lot of things at runtime that need to persist, since you need to handle tracking them and respawning them in the correct state when the game loads. This is because you need to consider when adding any new variable ‘will I need to save this?’ and update the savegame code accordingly.
You could write to a text file containing the position, type, and transform of each block in the building game and then extract that data every time you reload the scene by reading that text file. I’ve created text files thousands of lines long (when converting STL files to meshes) containing data such as this and it’s read almost instantly if you use the right approach.
Warning: This isn’t really the way to go if you want secure, safe data saving as the files aren’t encrypted, and if the files are deleted or corrupted, all your data could be lost.
If you want to use a database, that seems like a more logical, efficient, industrial approach rather than the easy way out of writing and reading a text file. It’s your choice @Sidadi657