I have looked many times and asked in many different ways but I have not been able to find a way to save a scene as a XML or JSON file. I’m hoping to make a save load system for my game. I know that something like this is possible from example of unity games I have played.
Unity itself doesn’t have any built-in save system for gameobject structures. So in any case you either need to roll your own, or use a third party solution. Such a system is going to be quite complex.
You either need a way to track from which prefabs dynamical objects have been created, or your save system need to be able to recreate everything from scratch. That means you would need to serialize “everything” which would include even meshes, textures, …
To actually save and restore the state of all components / classes you would need to use reflection in order to read and write all fields of a class. You have to track instances and give them some sort of instance ID so you are able to restore all references between all objects.
Writing such a system from scratch is quite complicated. If you’re skilled enough in all those fields, go ahead, otherwise you might want to look for a third party solution.
In any case your question is not specific enough as you just asked for a ready-to-use solution for a way too complex problem.
If you want an out-of-box solution go to the Unity Store, you will prolly find something for a small fee. If you are committed to building something from scratch the process MIGHT look something like:
- Create a set XML/Json Files for each scene or collection of scenes for each save slot.
- Read from those files as scenes are loaded.
- Instantiate/Enable/Disable/Destroy GameObjects into/on/from the scene as needed.
- Before a scene is unloaded, collect all information from the GameObjects that have ‘persistance’.
- Write that information into XML/Json files as the scene is closed.
This process will look very ugly unless you build your own loading screens. Good luck. Its not a simple, nor short, undertaking.