Search code examples
c#xna

C# + XNA: Location of Sprite Folder and Sprites after Compile


I am trying to program an automatic Resource Loader for my project and so it is going well. But I am stuck on the loading of the sprites. I used this for checking the file exists

if (File.Exists(contentManager.RootDirectory + "/Sprites/" + spriteName + ".png"))

However it never finds the file, even though the file exists in the Content of the project. I have checked the folders and found that the .pngs are replaced by .xnb files. So I tried changing the file extension and then was given an error in the Sprite Load function

return contentManager.Load<Texture2D>(assetName);

It only happens if I change it from being a typical image file (png, bmp, etc). Can someone explain why this is the problem and how I can get around this? I have had no experience in the xna file extensions and can not find the answers I need. What I want is to load a new image into the project providing it exists in the Content Directory after compile

While on the topic, will the Sounds and Music be effected by the Compiler?


Solution

  • When you use a content project, the resources are imported, processed and write to disk again as xnb files.

    If you want to dynamically load resources you have to compile files on the fly through the content builder or avoid using the content project.

    Microsoft provides examples like this to load content dynamically: http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_2

    if you want to distribute the application, the runtime xna is not enoigh, you need full xna framework and visual studio installed to work.

    if you want the original files in the content project, you have to change their "copy to output folder" property to "copy always", and realize that the compiled results will be at "project folder\bin\debug\content"