Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试使用unity中的c#从文本文件生成迷宫_C#_Unity3d_Char - Fatal编程技术网

尝试使用unity中的c#从文本文件生成迷宫

尝试使用unity中的c#从文本文件生成迷宫,c#,unity3d,char,C#,Unity3d,Char,到目前为止,我能够读取我的文本文件。然后我继续尝试访问每条线路。从这里,我尝试访问行中的每个值。如果是1,我将在位置x=1上创建一个游戏对象,如果下一个是1,则该对象将位于位置x=2。一旦这一行被读取,我想将位置从y=0(第一行)更改为y=1,以此类推 我的问题是,当我尝试运行它时,我似乎只从每一行获得第一个值。有人能看看我的密码,告诉我我的脑屁在哪里吗?我将不胜感激:) //拆分每一行以便轻松访问 eachLine=File.ReadAllLines(“Maze1.txt”); //获取行数

到目前为止,我能够读取我的文本文件。然后我继续尝试访问每条线路。从这里,我尝试访问行中的每个值。如果是1,我将在位置x=1上创建一个游戏对象,如果下一个是1,则该对象将位于位置x=2。一旦这一行被读取,我想将位置从y=0(第一行)更改为y=1,以此类推

我的问题是,当我尝试运行它时,我似乎只从每一行获得第一个值。有人能看看我的密码,告诉我我的脑屁在哪里吗?我将不胜感激:)

//拆分每一行以便轻松访问
eachLine=File.ReadAllLines(“Maze1.txt”);
//获取行数
int lines=File.ReadAllLines(“Maze1.txt”).Length;
//每次访问一行
foreach(每条线中的字符串行)
{
//访问每行中的每个字符,一次访问一个字符
foreach(行中的字符c)
{
字符串currentNum=c.ToString();
thisNum=Convert.ToInt32(currentNum);
//WriteLine(“此编号:{0}”,thisNum)
如果(thisNum!=1)
{
//而我
while(i<行)
{
ObjectSpawnPosition=newvector3(i+1,0,0);
Console.WriteLine(“此数字为1”);
实例化(obj、ObjectSpawnPosition、四元数.identity);
i++;
}
}

您的问题在于如何循环。如果您读取1,您将循环行并为每行创建一个对象。相反,您应该执行以下操作:

int y = 0;

// Accesses each line one at a time
foreach (string line in eachLine)
{
    // reset the x position to the beginning of each line.
    int x = 0;

    // Accesses each character in each line one at a time
    foreach(char c in line)
    {
        string currentNum = c.ToString();
        thisNum = Convert.ToInt32(currentNum);

        if (thisNum == 1)
        {
            // Create a single object at x,y (no looping here)
            ObjectSpawnPosition = new Vector3(x,y,0);
            Instantiate(obj, ObjectSpawnPosition, Quaternion.identity);
        }
        // increment x inside the inner loop.
        x++;
    }
    // done with a line, so increment y to go to the next line.
    y++;
}

首先,你在第二次阅读该文件,只是为了得到可以从
eachLine.Length
中获得的行数。谢谢:)我对所有这些东西都很陌生,所以每一点建议都确实有用!我不是100%清楚你在文件中的每个位置想要做什么。你说如果c字符为1,但您有一个
if(thisNum!=1)
。然后你有一个while循环,使用一个
i
变量,你甚至没有显示它被声明的位置。哦,是的,这就是thisNum==1。我只是在代码中鬼混,试图找出问题所在。i在顶部被声明为public int i=0;这取决于
i
被声明的位置递增,直到它等于
,然后对于下一个字符,它将跳过
while
循环。我猜至少你需要在
while
循环之前有一个
I=0;
,以便为文件中的每个字符重置它。但是我不明白是否需要为f中的每一行循环一次文件中每个字符的ile。
int y = 0;

// Accesses each line one at a time
foreach (string line in eachLine)
{
    // reset the x position to the beginning of each line.
    int x = 0;

    // Accesses each character in each line one at a time
    foreach(char c in line)
    {
        string currentNum = c.ToString();
        thisNum = Convert.ToInt32(currentNum);

        if (thisNum == 1)
        {
            // Create a single object at x,y (no looping here)
            ObjectSpawnPosition = new Vector3(x,y,0);
            Instantiate(obj, ObjectSpawnPosition, Quaternion.identity);
        }
        // increment x inside the inner loop.
        x++;
    }
    // done with a line, so increment y to go to the next line.
    y++;
}