【问题标题】:Isometric Tiles Of Different Sizes Not Rendering Correctly不同尺寸的等距平铺未正确渲染
【发布时间】:2015-06-30 19:57:25
【问题描述】:

我正在使用 Unity 5 创建等距游戏。我已经生成了一个瓷砖网格,效果很好。但是,当我使用两个不同的图块填充网格时(它们的图像大小略有不同),我会在图块之间出现间隙。显而易见的解决方案是创建图块,使它们的图像大小都相同,但这会阻止我在图块上创建大于图块大小的任何东西(例如树)。

这里有一些图片来演示:

只有一种类型的图块:

有两种类型的瓷砖:

这是我用来创建地图的代码:

private void CreateMap() {

    float tileWidth;
    float tileHeight;

    int orderInLayer = 0;

    SpriteRenderer r = floorTiles [0].GetComponent<SpriteRenderer> (); 

    tileWidth = r.bounds.max.x - r.bounds.min.x;
    tileHeight = r.bounds.max.y - r.bounds.min.y;

    for (int i = 0; i < map.GetLength(0); i++) {
        orderInLayer += 1;
        for (int j = 0; j < map.GetLength (1); j++) {
            Vector2 position = new Vector2 ((j * tileWidth / 2) + (i * tileWidth / 2) + (tileWidth / 2), (j * tileHeight / 2) - (i * tileHeight / 2) + (tileHeight/ 2));
            r = map[i,j].GetComponent<SpriteRenderer>();
            r.sortingOrder = orderInLayer;
            Instantiate(map[i, j], position, Quaternion.identity);              

        }
    }

}

任何帮助将不胜感激,我似乎无法修复它!

【问题讨论】:

  • 您可以创建一些重叠。另一种选择是拉伸较小的图像。
  • 这两个选项可以工作,但是这会使地图看起来很奇怪,我正在寻找一个完美的解决方案,坏的瓷砖只需要稍微调整,但我找不到办法。

标签: c# unity3d isometric


【解决方案1】:

每次创建一个图块时,您似乎都是从头开始计算每个图块的位置。如果您有 2 个不同大小的图块,那么您的计算结果会有所不同,因此图块中的间隙会有所不同。这是因为您只使用当前图块的宽度/高度,而没有考虑任何可能更短/更长的高度/宽度的先前图块。

鉴于您有不同的高度和宽度,您需要一种方法来计算两者的正确位置,以防止 X 和 Y 方向上的间隙。我在这里模拟了一些东西,但它未经测试。更多的是一个概念(?)我猜。

float tileHeight = 0;
float tileWidth = 0;

Vector2 position = new Vector2(0,0);

Dictionary<int, float> HeightMap = new Dictionary<int, float>();

for (int iRow = 0; iRow < map.GetLength(0); iRow++)
{
    position.x = 0;
    orderInLayer += 1;

    for (int jColumn = 0; jColumn < map.GetLength (1); jColumn++)
    {
        position.y = HeightMap[jColumn];

        r = map[iRow, jColumn].GetComponent<SpriteRenderer>();

        tileWidth = r.bounds.max.x - r.bounds.min.x;
        tileHeight = r.bounds.max.y - r.bounds.min.y;
        r.sortingOrder = orderInLayer;

        position.x += tileWidth / 2;

        position.y += tileHeight / 2;

        Instantiate(map[iRow, jColumn], position, Quaternion.identity);

        HeightMap[jColumn] = position.y;
    }
}

我保留了存储高度的最佳方式,或者将 HeightMap 字典的内容实例化为您认为合适的方式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 1970-01-01
    • 2020-08-24
    相关资源
    最近更新 更多