XNA 中的实时资源更新 [英] Realtime Resource Updating in XNA

查看:25
本文介绍了XNA 中的实时资源更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在 XNA 中在运行时更新纹理和其他内容文件的方法,这样如果它在应用程序之外被修改,它会在游戏中重新获得焦点时自动更新.

I'm looking for a way to update textures and other content files at runtime in XNA, such that if it's modified outside of the application, it will automatically update in the game when it regains focus.

这似乎可以通过 Texture2D 和从 Stream 加载,但我无法获得到给定资源的直接路径.

This seems to be possible with Texture2D and loading from a Stream, but I'm unable to get a direct path to a given resource.

我意识到这不适用于内容管道和 .xnb,但对于调试和开发目的,它会非常有用.

I realize that this won't work with the content pipeline and .xnb's, but for debugging and development purposes it would be extremely useful.

有没有人见过这样的东西用 XNA 成功实现?

Has anyone seen something such as this implemented successfully with XNA?

推荐答案

首先,你们需要决定是采用推还是拉模型.在推送模型中,更改资产的应用程序/环境需要以某种方式通知正在运行的游戏资产已更改并需要重新加载.这可以通过一个非常简单的消息队列来完成.

First all of you'll need to decide whether to adopt a push or pull model. In a push model, the application/environment that changed the asset will need to somehow notify the running game that the asset has changed and needs to be reloaded. This could be done with a very simple message queue.

在拉式模型中,游戏会定期检查所有游戏资产以查看它们是否已被修改,如果已修改,则重新加载它们.如果性能较低,这将是一个更简单的解决方案.

In a pull model the game would periodically check all the game assets to see if they have been modified, and if they have, reload them. This would be a simpler, if less performant, solution.

一个更大的问题是您的游戏是否可以在应用运行时优雅地处理重新加载资源.您可能需要实现某种后台线程来加载资产,然后将它们与游戏中的实时"资产交换".或者,您的游戏可能会在每次重新加载资产时短暂暂停.在任何一种情况下,这很容易最终成为流程中最复杂的部分,具体取决于您的内容管道当前的工作方式(您可能已经有一个后台加载线程等).

A bigger concern is whether or not your game can gracefully handle re-loading assets while the app is running. You'd probably need to implement some kind of background thread that loads the assets and then "swaps" them in with "live" assets in game. Alternatively your game could pause briefly each time an asset is being reloaded. In either case, this could easily end up being the most complicated part of the process, depending on how your content pipeline works currently (you might already have a background loader thread etc)..

编辑以回应评论:

您可以使用此方法获取内容文件夹的完全限定路径:)

You can get the fully qualified path to the content folder using this method :)

static public String FullContentDirectory(this ContentManager content)
{
    var appPath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

    return System.IO.Path.Combine(appPath, content.RootDirectory);
}

现在,我认为您的下一个问题可能是,您如何从资产名称中知道文件扩展名?或者,如果资产名称与文件名不同怎么办?

Now, I think your next problem might be, how do you know the file extension from the asset name? Or, what if the asset name is different to the filename?

这篇关于XNA 中的实时资源更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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