如何在Unity中的十六进制网格中一次只选择一个图块

时间:2016-08-31 01:42:15

标签: c# unity3d

正如标题所说!我正在研究一种文明类型的城市建设者游戏,作为即将到来的学年的练习(只有第二年的视频游戏编程学生!)。

我已经在游戏中生成了一个网格,如下所示:

This!

正如你所看到的,我已经建立了一个基本的选择系统,其中目前我只能一次选择一个图块,直到我取消选择它,然后我可以选择一个新的图块。使用与预制件上的对撞机相连的OnClick功能选择切片。 (将包括我目前最终的代码!)

我想知道怎样做的是每当我选择一个新图块时都会自动取消选择图块,所以我一次只选择一个图块。

这就是我现在所要选择的。

public void OnMouseDown() {
    if (GameManager.Instance.tileSelected == false) {
        if (enabled == false) {
            tileOutlineSprite.SetActive (true);
            enabled = true;
            GameManager.Instance.tileSelected = true;
            this.tileInfo.text = tileType;
        }
    }
    else if (enabled == true) {
        tileOutlineSprite.SetActive (false);
        enabled = false;
        GameManager.Instance.tileSelected = false;
        this.tileInfo.text = " ";
    }
}

这就是我目前用来生成网格的内容!我知道现在可能很麻烦,我打算清理它并随着我继续进行精炼!

void generateMap() {
    map = new List<List<TileSelect>>(); //generatign the playing field, making a grid of tile prefabs, and storing their positiosn in a 2d list
    for (int i = 0; i < mapSizeX; i++) {
        List <TileSelect> row = new List<TileSelect>();
        for (int j = 0; j < mapSizeY; j++) {
            if (i == 0) {
                iDiff = 0.8f;
            }

            if (j % 2 == 0) {
                iDiff = i + (.2f * (i+1));
            } else if (i != 0) {
                iDiff = i + 0.6f + (.2f * (i+1));
            }
            jDiff = j + (.04f * j);
            int rand = Random.Range (1, 101);
            if (rand <= 45) {
                TileSelect tile = ((GameObject)Instantiate (HeavyForestTile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
                tile.gridPosition = new Vector2 (i, j);
                tile.tileType = "Heavy Forest";
                tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
                row.Add (tile);
            } else if (rand >= 45 && rand <= 70) {
                TileSelect tile = ((GameObject)Instantiate (LightForestTile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
                tile.gridPosition = new Vector2 (i, j);
                tile.tileType = "Light Forest";
                tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
                row.Add (tile);
            } else if (rand >= 70 && rand <= 90 ) {
                TileSelect tile = ((GameObject)Instantiate (GrassTile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
                tile.gridPosition = new Vector2 (i, j);
                tile.tileType = "Grassland";
                tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
                row.Add (tile);
            } else if (rand >= 90 && rand <= 97) {
                TileSelect tile = ((GameObject)Instantiate (GrassRockTile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
                tile.gridPosition = new Vector2 (i, j);
                tile.tileType = "Light Rocks";
                tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
                row.Add (tile);
            } else if (rand >= 97 && rand <= 100) {
                TileSelect tile = ((GameObject)Instantiate (GrassRock2Tile, new Vector3 (iDiff, jDiff, 0), Quaternion.Euler (new Vector3 ()))).GetComponent<TileSelect> ();
                tile.gridPosition = new Vector2 (i, j);
                tile.tileType = "Heavy Rocks";
                tile.GetComponent<TileSelect> ().tileInfo = GameObject.Find ("InfoText").GetComponent<Text>();
                row.Add (tile);
            }

        }

        map.Add(row);
    }

哦!如果这对解决方案很重要,那么游戏就是2d!如果您需要更多信息,请告诉我,我很乐意提供!

1 个答案:

答案 0 :(得分:1)

一种简单的方法是在游戏管理器类中添加一个额外的成员:

select m.id, m.plotid, m.requirement, m.datetime
from member m
where exists (
  select 1
  from member m2
  where m.plotid = m2.plotid
  having max(m2.datetime) = m.datetime
  )

包含一个setter,以便每次更改选定的图块时,它都会取消选中所选图块并突出显示新选择的图块。

单击时单击更改选定的图块:

public class GameManager
{
    TileSelect _selectedTile;
    public TileSelect selectedTile
    {
        get { return _selectedTile; }
        set 
        {  
            //unhighlight the previous selected tile
            _selectedTile = value;
            //highlight the newly selected tile
        }
    }
    ...
}