2016-08-31 3 views
-1

タイトルが言うとおり!私は来年の練習として一種の文明型都市ビルダーゲームに取り組んでいます(2年目のビデオゲームプログラミングの学生のみ!)。Unityの16進法グリッドで一度に1つのタイルを選択する方法

私はすでにのように見えるゲームで生成されたグリッド、得ている

:私はすでに設定初歩的な選択システムを得ているあなたが見ることができるように

This!

は、私が唯一の特徴、現在選択することができますが一度に1つのタイルを選択解除するまで、新しいタイルを選択することができます。タイルは、プレハブ上のコライダーに関連付けられたOnClick関数を使用して選択されます。

私は新しいタイルを選択するたびにタイルを自動的に選択解除して、一度に1つのタイルしか選択しないようにしています。

これは私が今選択しているものです。

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

ああ!それが解決策のために重要であればゲームは2次元にあります!もう情報が必要な場合はお知らせください。喜んで提供します。

答えて

1

簡単な方法は、あなたのゲームマネージャクラスに追加メンバーを追加するには、次のようになります。

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

は毎回あなたが選択したタイルを変更するようなセッターを含めて、それが選択されたタイルをunhighlightsし、新しい選択されたタイルを強調します。それがクリックされたときに

だけで、選択したタイルを変更:

void onClick(...) 
{ 
    ... 
    //on raycast hit with the 2d tile (targetTile) 
    gameManager.selectedTile = targetTile; 
} 
+0

こんにちは!あなたの答えをどうもありがとう。私は自分自身を動作させることができませんでしたが、私はそれを把握することができると確信しています! 私は幾分簡単な解決策を見つけました(少なくとも私にとって):http://answers.unity3d.com/questions/826999/deselect-object-when-another-instance-is-selected.html。ちょうど誰かがGoogleの検索を介してこれを見つける場合は、代替オプションがあるので、投稿! – harrisoncrazy

関連する問題