且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 Unity 中保存和加载整个场景

更新时间:2023-12-01 22:40:52

没有内置方法可以让您在运行时保存场景",然后再重新加载它.在 Unity 构建中,场景以不可编辑的格式存储,这意味着无论何时加载场景,它都会以与构建时相同的格式加载.这是一件好事,因为您不想在已部署的游戏中编辑构建的内容.

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.

我建议创建一个 JSON 配置文件来存储您的重要信息,可能是这样的:

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. 您要开始新游戏了吗?=> 生成一个随机配置文件,然后使用它来填充您的场景.
  2. 您是否正在加载已保存的游戏?=> 使用保存的配置文件来填充您的场景.

您需要某种WorldBuilder 脚本来处理此问题.在你的场景中,你可以有如下内容:

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.