动态添加切片到基于网格的贴图

时间:2015-12-22 19:28:11

标签: unity3d unity5

我希望有一张无限可探索的地图。计划是创建游戏牌块(道路,障碍物,建筑物)的类别,并随机选择当玩家接近现有牌组边缘时要添加的游戏牌的类别。一旦玩家距离该牌块2格的方格,也会销毁牌。目前我使用的是需要大小初始值设定项的多维数组。

到目前为止我所拥有的:

public class GameManager : MonoBehaviour
{
    private GameObject[,] tileArray;
    public GameObject groundTile;
    public GameObject player;

    private int tileSize = 80;
    private int nextFarX = 1;
    private int nextFarZ = 1;
    private int nextNearX = -1;
    private int nextNearZ = -1;
    private float padding = .1f;

    private int arrayOffset;
    private int arrayDimension;

    // Use this for initialization
    void Start ()
    {
        arrayDimension = 200;
        arrayOffset = arrayDimension / 2;
        tileArray = new GameObject[,];
        this.AddCubeAt(0, 0);
    }

    // Update is called once per frame
    void Update () {
        var x = Convert.ToInt32(player.transform.position.x / tileSize);
        var z = Convert.ToInt32(player.transform.position.z / tileSize);

        for (int i = -1; i < 2; i++)
        {
            for (int j = -1; j < 2; j++)
            {
                var checkX = x + i;
                var checkZ = z + j;
                if (tileArray[checkX + arrayOffset, checkZ + arrayOffset] == null)
                {
                    //player is less than 2 tiles away from this grid, add a tile
                    this.AddCubeAt(checkX, checkZ);
                }
            }
        }
        // feels like a hack, but it will remove tiles that are not touching the tile that the player occupies
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 6; j++)
            {
                if (i == 0 | i == 5 | j == 0 | j == 5)
                {
                    if (tileArray[x + (i-2) + arrayOffset, z + (j-2) + arrayOffset] != null)
                    {
                        Destroy(tileArray[x + (i - 2) + arrayOffset, z + (j - 2) + arrayOffset]);
                        tileArray[x + (i - 2) + arrayOffset, z + (j - 2) + arrayOffset] = null;
                    } 
                }
            }
        }
    }

    private void AddCubeAt(int x, int z)
    {
        var pos = new Vector3(x * tileSize, 0, z * tileSize);
        var rot = Quaternion.identity;
        GameObject newCube = (GameObject)Instantiate(groundTile, pos, rot);
        tileArray[x + arrayOffset, z + arrayOffset] = newCube;
    }
}

有什么更好的方法来解决这个问题?

2 个答案:

答案 0 :(得分:1)

您应该熟悉图形数据结构(而不​​是邻接矩阵实现)。它更适合这项任务。而且,我会解决这个问题

  

一旦玩家距离该区块2格子方格,也会销毁瓷砖

以另一种方式:每当玩家改变位置时,我会在目标深度(在你的情况下是2)开始DFS并移除找到的牌。

答案 1 :(得分:0)

决定使用简单的词典和方法来查询/更新它:

private GameObject RetrieveTileAt(int x, int z)
{
    string key = string.Format("{0}.{1}", x, z);
    if (tileDictionary.ContainsKey(key))
    {
        return tileDictionary[key];
    }
    else
    {
        return null;
    }
}

private void InsertTileAt(int x, int z, GameObject tile)
{
    string key = string.Format("{0}.{1}", x, z);
    tileDictionary[key] = tile;
}

它不是一个无限大小的网格,(int min + int max)平方,但它应该远远超过我的需要。