Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save and load entire scene in Unity

Okay, so I've been struggling around and searching for a while, saw a lot of different posts, but I did not find answer to my question.

My problem: I have a scene in Unity with nothing in it, everything is created proceduraly and randomly on game start, and of course I want the player to be able to save his progress. I have found ways of saving progress in Unity, but everything was about writing a script for each class or object I want to save, but these seem to me inefficient, since in my game, there are randomly generated houses and buildings (which would be relatively easy to save), but there can also be objects placed inside these buildings and so on. Also later I plan on adding characters, which also need to be saved (like where they are, what are they holding and such). And as I mentioned, writing a save and load script for each object seems inefficient to me, since I'm used to Java's serializtaion, where I just write my Main Object containing all data to a file, so I'm looking for some easier ways to do so. Possibly a way to save entire scene state and then on loading just load the scene instead of generating it from scratch.

My Question: Is there a way to save whole scene with all objects and information about them and then load it?

Thank you in advance!

like image 523
Coreggon Avatar asked Oct 26 '25 23:10

Coreggon


1 Answers

There's no built-in method by which you can "save a scene" during runtime and then reload it later. In a Unity build, scenes are stored in a non-editable format, meaning that whenever you load a scene it will load up with the same format as it was built. This is a good thing, because you don't want to edit the contents of your build in a deployed game.

Now, that doesn't mean that a scene can't contain logic to configure itself differently. In fact, it sounds like that's what you're doing. The goal instead is to store the contents into a save file.

Consider switching your mentality from "I want to load a scene that generates random gameplay" to "I want to load a scene that configures itself based on a file." This is a layer of abstraction that gives greater control over what happens when you load your scene.

I would suggest creating a JSON configuration file that stores your important information, something like this might do:

{
  "house_locations": [
    {
      "position": "(0, 0, 0)",
      "objects": []
    },
    {
      "position": "(10, 10, 10)",
      "objects": []
    }
  ],
  "characters": [
    {
      "position": "(0, 0, 0)",
      "inventory": [
        {
          "item_name": "knife"
        },
        {
          "item_name": "shovel"
        }
      ]
    }
  ]
}

This is just a simple example, as you'll have to add the important data you want to represent your game.

Next, all you have to do when you want to start your game is to do one of the following things:

  1. Are you starting a new game? => Generate a random configuration file, then use that to populate your scene.
  2. Are you loading a saved game? => Use the saved configuration file to populate your scene.

You'll need some kind of WorldBuilder script to handle this. In your scene, you can have something like the following:

public class WorldBuilder : MonoBehaviour
{
  // This is the actual contents of the world, represented as a JSON string.
  private string _json = "";

  public void BuildWorld(string configFilePath)
  {
    _json = LoadConfiguration(configFilePath);
    BuildWorld(_json);
  }

  public void GenerateWorld()
  {
    _json = GenerateConfiguration();
    BuildWorld(_json);
  }

  public void SaveWorld(string targetFilePath)
  {
    // Save the contents of _json out to a file so that it can be loaded
    // up again later.
  }

  private string LoadConfiguration(string configFilePath)
  {
    // Load the actual file and return the file contents, which is a JSON string.
  }

  private void BuildWorld(string json)
  {
    // Actually build the world using the supplied JSON.
  }

  private string GenerateConfiguration()
  {
    // Return a randomly generated configuration file.
  }
}

This approach separates the problem of saving the contents of the scene and generating the contents of the scene, making the code easier to write and maintain.

like image 156
Aaron Cheney Avatar answered Oct 30 '25 08:10

Aaron Cheney



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!