开发者

Building Content in Winform XNA

I have successfully been able to build content into my winform using Winform Series 1 and Winform Series 2 but my question is, how do you get the content to be loaded back into the editor the next time you open it. The content, .xnb, file is currently being saved to a temp folder. Is there anyway to have the content be loaded ba开发者_C百科ck into the editor with out having to go and build each file again?

Could I just save it to the Content folder inside the bin/ folder and then look through that folder at the start up and look for .xnb files and just load them? or is there an easier way to this?


In the second WinForms sample there is a HTML readme file that describes how the application saves built content to a temporary directory and then deletes it when the program closes.

This is the important bit:

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.

It says that deleting the temporary directory "is handled by ContentBuilder.DeleteTempDirectory, which is called by Dispose". So simply find the call to DeleteTempDirectory and remove it.

The readme file describes in more detail how the temporary directory is selected (and why). You could modify CreateTempDirectory to suit your application better. For example if your editor has "level" files, you might want to save your built content (.xnb files) in a subdirectory with the same name, next to your level, so that your game can easily open the built content.


Once your files are being kept between sessions - all you have to do is reload them. The two obvious ways are to store a list of the files that are open, and reload it next session. Or simply open everything that is in your output directory:

Here is some rough code to do the latter (assuming no subdirectories):

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));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜