XNA Framework Importers

2020-07-17 05:26发布

问题:

I am working on a game using the XNA framework. My game has several levels which I am storing the data in a plain old text file. In VS 2008 when I add the level file to the project and compile, I receive the following error message.

Error 1 Cannot autodetect which importer to use for "Levels\0.txt". There are no importers which handle this file type. Specify the importer that handles this file type in your project. F:\Projects\BrickBreaker\BrickBreaker\Content\Levels\0.txt BrickBreaker

The reason I am bringing this out is because if I change on of my levels and run the game, the level is not updated. I have found that the level is not updated because VS runs the game from the bin\debug folder and since the level files are not included in the project, they are not copied when they change. I also found that the platform sample that comes with the framework includes the level data in the project, that's where I got the technique from.

So, should I use a different file format or just deal with having to manually copy the new level files over?

Resolution - After digging into the answers on this post I found a solution. I added the text files to the project and set the build property to none. The error no longer occurs when compiling and the file is included in the project.

回答1:

You can have Visual Studio just copy over the files if you want to the output directory. Under the properties of the text file in the project, choose Build Action: None and change the copy to output directory to as needed.

You can also check out the platformer sample. They use text files as their level format.



回答2:

There is no content importer for text files. Ignore the content pipeline and just read the file in just as you would any other normal text file.

string line = string.empty;
using(StreamReader sr = new StreamReader("filename")){
    while((line = sr.ReadLine()) != null){
        //reads line by line until eof
        //do whatever you want with the text
    }
}


回答3:

I had similar problem, and found it easier to have the files added to content project, and build action set to none, than to skip content pipeline altogether.