错误修复矩阵

时间:2016-09-16 08:14:42

标签: c# matrix unity3d file-io io

我正在做一个统一项目,我使用mapMatrix(字符矩阵)作为一个地图工具来告诉哪个gameObject实例化为一个环境。 首先,我调用函数populateMapMatrixwriteTextFile来创建一个包含矩阵内容的txt文件。但现在,我需要从该文件读取到矩阵然后实例化环境。我调试了项目和矩阵的一些元素得到了' \ n'即使我有办法处理chars。有人能帮我吗? 这是代码:

using UnityEngine;
using System.Collections;
using System.IO;

public class EnviromentBuilder : MonoBehaviour {

    const int NUMBER_OF_ROWS = 50;
    const int NUMBER_OF_COLUMNS = 50;
    const char GRASS_MATRIX_FIELD = 'G';
    const char TREE_MATRIX_FIELD = 'T';

    const string pathToFile = "C:\\Users\\Darko\\Desktop\\Unity\\WAR GAME\\enviromentMapMatrix.txt";

    public char[,] mapMatrix;

    public GameObject grass;
    public GameObject tree;

    // Use this for initialization
    void Start ()
    {       
        mapMatrix = new char[NUMBER_OF_ROWS, NUMBER_OF_COLUMNS];

        //populateMapMatrix ();
        readTextFile();

        InstantiateEnviroment ();

        //writeTextFile ();
    }


    bool isGrassMatrixField(int i, int j)
    {
        return mapMatrix [i, j] == GRASS_MATRIX_FIELD ? true : false; 
    }


    bool isTreeMatrixField(int i, int j)
    {
        return mapMatrix [i, j] == TREE_MATRIX_FIELD ? true : false;
    }


    void populateMapMatrix()
    {
        for (int i = 0; i < NUMBER_OF_ROWS; i++)
            for (int j = 0; j < NUMBER_OF_COLUMNS; j++) 
            {

                mapMatrix[i,j] = 'G';

                if (i > 28 && i < 45 && j > 20 && j < 38) {
                    mapMatrix[i,j] = 'T';
                }

            }
    }


    void InstatiateGrassField(int i, int j)
    {
        if (isGrassMatrixField(i,j))
        {
            Instantiate (grass, new Vector3 (i, 0, j), Quaternion.identity);
        }
    }


    void InstantiateTreeField(int i, int j)
    {
        if (isTreeMatrixField(i,j)) {
            Instantiate (tree, new Vector3 (i, 0, j), Quaternion.identity);
        }
    }


    void InstantiateEnviroment()
    {
        for (int i = 0; i < NUMBER_OF_ROWS; i++)
            for (int j = 0; j < NUMBER_OF_COLUMNS; j++) 
            {
                //Instantiate a grass prefab
                InstatiateGrassField(i, j);
                //Instantiate a tree prefab
                InstantiateTreeField(i, j);
            }
    }

    void writeTextFile()
    {
        using (StreamWriter streamWriter = new StreamWriter (pathToFile))
        {
            for (int i = 0; i < NUMBER_OF_ROWS; i++)
            {
                for (int j = 0; j < NUMBER_OF_COLUMNS; j++)
                {
                    streamWriter.Write (mapMatrix [i, j]);

                    //streamWriter.Write (' ');
                }

                streamWriter.WriteLine();
            }
        }
    }

    void readTextFile()
    {
        using (StreamReader streamReader = new StreamReader(pathToFile))
        {
            for (int i = 0; i < NUMBER_OF_ROWS; i++)
                for (int j = 0; j < NUMBER_OF_COLUMNS; j++) 
                {
                    char karakter = (char)streamReader.Read ();
                    if (karakter == '\n')
                    {
                        karakter = (char)streamReader.Read ();
                    }

                    mapMatrix [i, j] = karakter;
                }

        }
    }


}

0 个答案:

没有答案