当前位置:  开发笔记 > 编程语言 > 正文

在C#中将char更改为int或字符串

如何解决《在C#中将char更改为int或字符串》经验,为你挑选了1个好方法。

我对C#Sharp非常陌生,可能无法很好地提出这个问题,但是可以解决。

我目前正在生成一个包含更多游戏对象的关卡。通过解析文本文件(这是我的字典)来生成地图。目前,由于char声明,如果对象的索引未超过9,则我拥有的代码可以完美运行。

我想知道一种如何将其更改为int或字符串格式(AZ)的方法。我附上了我当前的“地图生成器”的照片,并突出显示了10,目前被读取为1和0。

private void CreateLevel() {

    Tiles = new Dictionary();

    string[] mapData = ReadLevelText();

    int mapX = mapData[0].ToCharArray().Length;
    int mapY = mapData.Length;

    Vector3 maxTile = Vector3.zero;

    // Calculates the world start point, this is the top left corner of the screen, rememeber to use on the sprite the top left pivot point
    Vector3 worldStart = Camera.main.ScreenToWorldPoint(new Vector3(0, Screen.height));

    for (int y = 0; y < mapY; y++) // the Y position
    {

        char[] newTiles = mapData[y].ToCharArray();

        for (int x = 0; x < mapX; x++) // the X position
        {
            // Places the tile into the level
            PlaceTile(newTiles[x].ToString(), x, y, worldStart);
        }
    }

    maxTile = Tiles[new Point(mapX-1, mapY-1)].transform.position;

    cameraMovement.SetLimit(new Vector3(maxTile.x + TileSize, maxTile.y - TileSize));
}

private void PlaceTile(string tileType, int x, int y, Vector3 worldStart) {

    int tileIndex = int.Parse(tileType);

    // Creates a new tile and makes a reference to that tile in the newTile variable
    TileScript newTile = Instantiate(tilePrefabs[tileIndex]).GetComponent();

    // Uses the new tile variable to change the position of the tile

    newTile.Setup(new Point(x, y), new Vector3(worldStart.x + (TileSize * x), worldStart.y - (TileSize * y), 0));

    Tiles.Add(new Point(x,y), newTile);

}

n234.. 6

您正在使用的数据格式无法解析。

考虑一下,没有办法说出应该将哪个数字解释为对“ 10”或单个“ 1”甚至三个“ 101”。

在这些情况下,我建议您重做文件格式以使用字符而不是数字,以便使用更多符号,并且/或者强烈建议在每个字符之间使用分隔符,例如,字符。CSV(逗号分隔值)文件格式正是这样做的。

You could create your levels in a spreadsheet program like Excel, Numbers (mac), LibreOffice, or an online spreadsheet tool like Google Sheets. You would do this by filling in the values in the cells, and then when you are done you would export / "save as" a .csv file.

You can parse the .csv file using a .NET library like CsvHelper (which I highly recommend using for reading CSV data in the real world)

Or, if you want to do it the quick and dirty way, you would read the file into memory using something like File.ReadAllLines, Trim() each line from the file, and then use Split(',') to split each line into its values. You can then iterate through your 2D array, and map it to another value if you prefer to do that!



1> n234..:

您正在使用的数据格式无法解析。

考虑一下,没有办法说出应该将哪个数字解释为对“ 10”或单个“ 1”甚至三个“ 101”。

在这些情况下,我建议您重做文件格式以使用字符而不是数字,以便使用更多符号,并且/或者强烈建议在每个字符之间使用分隔符,例如,字符。CSV(逗号分隔值)文件格式正是这样做的。

You could create your levels in a spreadsheet program like Excel, Numbers (mac), LibreOffice, or an online spreadsheet tool like Google Sheets. You would do this by filling in the values in the cells, and then when you are done you would export / "save as" a .csv file.

You can parse the .csv file using a .NET library like CsvHelper (which I highly recommend using for reading CSV data in the real world)

Or, if you want to do it the quick and dirty way, you would read the file into memory using something like File.ReadAllLines, Trim() each line from the file, and then use Split(',') to split each line into its values. You can then iterate through your 2D array, and map it to another value if you prefer to do that!

推荐阅读
pan2502851807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有