简体   繁体   English

C#XNA-读取.txt文件并创建2D数组

[英]C# XNA - Read .txt file and create 2D array

I'm trying to create a 2D array in XNA which I'll be using as a tile map for a game I'm working on. 我正在尝试在XNA中创建2D数组,并将其用作正在开发的游戏的图块贴图。 I've read various solutions but none of them seem to be working for me. 我已经阅读了各种解决方案,但是似乎没有一个对我有用。 One of the main issues I'm having is an error: 我遇到的主要问题之一是错误:

Cannot autodetect which importer to use for "map.txt". There are no importers which handle this file type. Specify the importer that handles this file type in your project.

This appears to be caused by the StreamReader class that I'm attempting to use. 这似乎是由我尝试使用的StreamReader类引起的。

I'm using XNA 4.0. 我正在使用XNA 4.0。

My .txt file looks like this (example): 我的.txt文件如下所示(示例):

0,0,0,0,0
0,0,0,0,0
0,0,1,0,0
0,1,1,1,0
1,1,1,1,1

My C# and XNA looks like this: 我的C#和XNA如下所示:

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

If anyone could help me, or point me in the direction of a working example that would be great. 如果有人可以帮助我,或者让我指出一个可行的例子,那将是很好的。

Change the build action to "None" in the properties window for that file, if you're manually reading it with StreamReader. 如果您正在使用StreamReader手动读取文件,请在该文件的属性窗口中将其生成操作更改为“无”。 The message comes from the content pipeline trying to import it for you. 该消息来自内容管道,试图为您导入它。

Specify the importer that handles this file type in your project. 指定在项目中处理此文件类型的导入器。

Find the file in your content project, open the properties menu, and select an importer. 在内容项目中找到文件,打开属性菜单,然后选择一个导入器。

According to MSDN: Verifying the Content Importer 根据MSDN:验证内容导入器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM