问题
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.
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.
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.
Has anyone seen something such as this implemented successfully with XNA?
回答1:
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)..
EDIT in response to comment:
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?
来源:https://stackoverflow.com/questions/7314327/realtime-resource-updating-in-xna