在 Unity 中保存和加载整个场景 [英] Save and load entire scene in Unity

查看:85
本文介绍了在 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.

我的问题:我在 Unity 中有一个场景,里面什么都没有,一切都是在游戏开始时按程序和随机创建的,当然我希望玩家能够保存他的进度.我已经找到了在 Unity 中保存进度的方法,但一切都是关于为我想要保存的每个类或对象编写脚本,但在我看来这些效率低下,因为在我的游戏中,有随机生成的房屋和建筑物(这将是比较容易保存),但也可以在这些建筑物内放置物体等等.稍后我还计划添加角色,这些角色也需要保存(比如他们在哪里,他们拿着什么等等).正如我所提到的,为每个对象编写一个保存和加载脚本对我来说似乎效率低下,因为我习惯于 Java 的序列化,我只是将包含所有数据的主对象写入文件,所以我正在寻找一些更简单的方法这样做的方法.可能是保存整个场景状态然后在加载时加载场景而不是从头开始生成它的方法.

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?

先谢谢你!

推荐答案

没有内置方法可以让您在运行时保存场景",然后再重新加载它.在 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.

这篇关于在 Unity 中保存和加载整个场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆