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\$
1

3 Answers 3

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.

\$\endgroup\$
3
  • \$\begingroup\$ It's not static, but there is a Content property on the Game class which is commonly used in XNA. \$\endgroup\$ Commented Jun 16, 2011 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\$ Commented Jun 17, 2011 at 1:22
  • \$\begingroup\$ @Andrew Thank you! I've deleted my false and pointless note. \$\endgroup\$
    – Anko
    Commented Jun 17, 2011 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.

\$\endgroup\$
0
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.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .