8
\$\begingroup\$

I want to ask, is there way to load in xna all content from folder ?
For example in my content I have:

Images/hero
Images/car
Images/tree


I want to make something like this

Dictionary<string,Model> models = new ...

foreach(string name in content.getNames("Images"))
{
    models[name] = content.load<Model>("Images\\" + name);
}
\$\endgroup\$
3
\$\begingroup\$

There's a great MSDN page about a useful directory access method and someone's asked a similar question on StackOverflow.

Basically, find the files that are in that directory, initialise an array of that size and then iterate through those files, loading in the resources.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ It's not static, but there is a Content property on the Game class which is commonly used in XNA. \$\endgroup\$ – Michael Coleman Jun 16 '11 at 23:25
  • \$\begingroup\$ Actually the answer that you linked is an extension method for the ContentManager class. So you'd use it as Content.LoadContent<Texture2D>("myFolder"). The Content property on Game is simply an instance of the ContentManager class - you could use any instance you like. (Nothing about this has changed between XNA versions.) \$\endgroup\$ – Andrew Russell Jun 17 '11 at 1:22
  • \$\begingroup\$ @Andrew Thank you! I've deleted my false and pointless note. \$\endgroup\$ – Anko Jun 17 '11 at 10:53
3
\$\begingroup\$

http://xbox.create.msdn.com/en-US/sample/contentmanifestextensions

It's a tutorial about loading content at runtime. It shows how to generate a list automatically by using the content pipeline.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You can easily list all the files inside a folder and then load them one by one. By default all content files are stored in a folder right beside your main application so you just need to check for the contents of that direction. You can also use a recursive function if you want to load all files from all the directories. here is a little sample:

string[] files = Directory.GetFiles("content\\Images");
for (int i = 0; i < files.Count(); i++)
{
    textures.Add(Content.load<Texture2D>(files[i].Remove(0,"Content\\".Length)));
}

notice that when you are asking content system to load a texture, you should drop "content" folder from file name.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.