我已经成功地使用 Winform Series 1 将内容构建到我的 winform 中和 Winform Series 2但我的问题是,下次打开编辑器时如何将内容加载回编辑器。内容 .xnb 文件当前正在保存到临时文件夹中。有没有办法将内容加载回编辑器,而不必重新构建每个文件?
我可以将它保存到 bin/文件夹内的 Content 文件夹,然后在启动时查看该文件夹并查找 .xnb 文件并加载它们吗?或者有更简单的方法吗?
最佳答案
在第二个 WinForms 示例中,有一个 HTML 自述文件,它描述了应用程序如何将构建的内容保存到临时目录,然后在程序关闭时将其删除。
这是重要的一点:
Depending on your application, you might prefer to always use the same temporary directory name, and never delete it. This will leave files lying around on your hard drive. The content build process is incremental. If your program tries to load the same content files that were already built during a previous run, you will not need to carry out any actual processing work. This can speed up loading times for programs such as level editors that are likely to want to load the same files each time they start up.
它表示删除临时目录“由 ContentBuilder.DeleteTempDirectory
处理,它由 Dispose
调用”。因此,只需找到对 DeleteTempDirectory
的调用并将其删除即可。
自述文件更详细地描述了如何选择临时目录(以及为什么)。您可以修改 CreateTempDirectory
以更好地适应您的应用程序。例如,如果您的编辑器有“关卡”文件,您可能希望将构建的内容(.xnb 文件)保存在关卡旁边的同名子目录中,以便您的游戏可以轻松打开构建的内容。
一旦您的文件在 session 之间保存 - 您所要做的就是重新加载它们。两种明显的方法是存储打开的文件列表,并在下一个 session 中重新加载它。或者直接打开输出目录中的所有内容:
这是执行后者的一些粗略代码(假设没有子目录):
string folder = @"C:\TemporaryXNAFilesOrWhatever";
List<Texture2D> textures = new List<Texture2D>();
ContentManager content = new ContentManager(serviceProvider, folder);
string[] files = Directory.GetFiles(folder, "*.xnb");
foreach(string file in files)
{
string assetName = Path.GetFileNameWithoutExtension(file);
textures.Add(content.Load<Texture2D>(assetName));
}
关于c# - 在 Winform XNA 中构建内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3349761/