不同尺寸的等距瓷砖无法正确渲染

时间:2015-06-30 19:57:25

标签: c# unity3d isometric

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

以下是一些要展示的图片:

只有一种类型的图块:enter image description here

使用两种类型的图块:enter image description here

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

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);              

        }
    }

}

任何帮助都会非常感激,我似乎无法解决它!

1 个答案:

答案 0 :(得分:0)

每次创建一个瓷砖时,您似乎都是从头开始计算每个瓷砖的位置。如果您有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字典的内容。

相关问题