How to upload all files to a folder with XNA?

I want to load all the files in the "Content / textures" folder into the game using Content.Load ("filename");

However, I cannot "find" the files located inside the Content in this way (the program rather looks at "bin / debug /../ Content / textures , but I get an error when I try to download jpg / png files using Content.Load .

How can I achieve what I am trying to do? I want to upload all the files to a folder inside the Content (which content folder is the right one?) Into the game, so I don’t need to specify each texture.

Thanks.

+6
source share
3 answers

You can use this helper class to do this. It works by taking the given directory and using the GetFiles() method to create a list of all the textures that need to be loaded. He then downloads them as usual with the ContentManager and puts them into the dictionary so you can use them.

  public static class TextureContent { public static Dictionary<string, T> LoadListContent<T>(this ContentManager contentManager, string contentFolder) { DirectoryInfo dir = new DirectoryInfo(contentManager.RootDirectory + "/" + contentFolder); if (!dir.Exists) throw new DirectoryNotFoundException(); Dictionary<String, T> result = new Dictionary<String, T>(); FileInfo[] files = dir.GetFiles("*.*"); foreach (FileInfo file in files) { string key = Path.GetFileNameWithoutExtension(file.Name); result[key] = contentManager.Load<T>(contentFolder + "/" + key); } return result; } } 

Create a dictionary to store textures, not a line after a line Texture2D s

  public Dictionary<string, Texture2D> spriteContent; 

... and call the method in your LoadContent method

  spriteContent = TextureContent.LoadListContent<Texture2D>(content, "textures"); 

Now that you need texture, just do:

 Whatever.Image = spriteContent["WhateverTexture"] 

Make sure TextureName is the asset name of your texture.

+8
source

Content in XNA must be built with a content pipeline. It turns your .png files (etc.) into .xnb files. These are processed binary content files that the ContentManager can load into Texture2D instances at runtime.

(Note that you can upload image files directly using Texture2D.FromStream - MSDN . However, this does not pre-multiplex alpha, which XNA rendering modes use by default.)

So, first of all, you need to collect all your content files. XNA Content Console uses MSBuild. The project file in Visual Studio is an MSBuild file. So what you need to do is edit this project file manually to create a set of files using a wildcard. There are instructions on this blog post that is related to this very similar question that I asked quite a while.

XNA's embedded content files are simply stored in the file system with respect to the game output directory. Therefore, use Directory.GetFiles to list .xnb files in a directory. Then create a path to load them from the content directory ( ContentManager.RootDirectory ) using your file name without the extension (which you can get with Path.GetFileNameWithoutExtension ). Then just pass your constructed path to Content.Load<Texture2D> to load each one.

(Note that .xnb files may contain objects other than textures, so you should make sure that your directory contains only textures. Otherwise, you need to catch an exception that will be ContentManager.Load<Texture2D> .)

+1
source

With XNA, you need to specify each texture because you want them to have a unique and easy name. if you want to load a variable number of textures from a folder, you can use this example: http://www.csharp-examples.net/get-files-from-directory/

0
source

Source: https://habr.com/ru/post/927836/


All Articles