views:

38

answers:

1

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 back 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?

+1  A: 

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));
}
Andrew Russell
Thanks for that statement. This does help although it does not solve my problem. I was wondering if it is possible to have those files that have already been built to be loaded back up into the editor without having to specify, or could I just look through the folder and load them that way?
Chris Watts
@Chris: I have updated my answer to describe how to do that.
Andrew Russell