簡體   English   中英

如何在 Unity 中單擊時從十六進制地圖獲取坐標

[英]How to Get Coordinates from Hex Map on Click in Unity

我正在制作名為 Hex 的棋盤游戲。 當我單擊一個磁貼時,它會變為藍色或黃色,但我還需要知道該磁貼的坐標是什么。

我不能用...

rend.transform.position;

...因為我想要接收的坐標看起來類似於左下角的 (0,0) 和其上方的 (0,1): 在此處輸入圖片說明

控制台中的坐標是我需要接收的坐標。

在此處輸入圖片說明

我在生成十六進制映射時使用列和行變量打印出來:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HexMap : MonoBehaviour
{
 // Use this for initialization
 void Start()
 {
    GenerateMap();
 }

public GameObject HexPrefab;

public void GenerateMap()
{
    for (int column = 0; column < 11; column++)
    {
        for (int row = 0; row < 11; row++)
        {
            // Instantiate a Hex
            Hex h = new Hex(column, row);

            Instantiate(HexPrefab, h.Position(), Quaternion.identity, this.transform);

            Debug.Log(column + "," + row);
        }
    }
}
}

當我在此處使用此腳本單擊十六進制磁貼時,我希望能夠獲取坐標:

using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Net;
using UnityEngine;
using UnityEngine.Networking;

public class ColorChange : MonoBehaviour {

public Color[]colors; // allows input of material colors in a set sized array
public SpriteRenderer rend;  // what are we rendering? the hex

public enum Player {ONE, TWO};
public static Player currentPlayer = Player.ONE;

// Use this for initialization
void Start () {
    rend = GetComponent<SpriteRenderer> (); // gives functionality for the renderer
}

void NextPlayer() {

   if( currentPlayer == Player.ONE ) {
      currentPlayer = Player.TWO;
   }
   else if( currentPlayer == Player.TWO) {
      currentPlayer = Player.ONE;
   }
}

// Update is called once per frame
void OnMouseDown () {
    // if there are no colors present nothing happens
    if (colors.Length == 0)
        return;

    if (currentPlayer == Player.ONE)
        rend.color = colors [0];
    else if (currentPlayer == Player.TWO)
        rend.color = colors [1];

    NextPlayer();
}

這是我用來確定六邊形瓷磚位置的腳本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Hex{

public Hex (int q, int r){
    this.Q = q;
    this.R = r;
}

public readonly int Q; // x
public readonly int R;  // y

static readonly float WIDTH_MULTIPLIER = Mathf.Sqrt(3) / 2;

public Vector2 Position(){
    float radius = 0.513f;
    float height = radius * 2;
    float width = WIDTH_MULTIPLIER * height;

    float vert = height * 0.75f;
    float horiz = width;

    return new Vector2(horiz * (this.Q - this.R/2f), vert * this.R);
}
}

我想我需要能夠在實例化時為每個十六進制模型分配列和行的值,但我不確定如何做到這一點。 我嘗試了幾種我在網上找到的解決方案以及使用 GetComponent,但我無法使它們工作。 如果有人知道這可能是什么,我將不勝感激!

GetComponent在您的情況下GetComponent的原因是因為坐標腳本位於十六進制預制件的子對象上。 您可以通過調用GetComponentInChildren訪問子對象上的組件。 它看起來像下面這樣:

// Instantiate a Hex
Hex h = new Hex(column, row);

// Instantiate the prefab
GameObject instance = Instantiate(HexPrefab, h.Position(), Quaternion.identity, this.transform);

// Let's find the coorinates component on the hex prefab instance.
Coordinates coordinates = instance.GetComponentInChildren<Coordinates>();

// now you may assign the column and row values to it
// ...

有許多可能的方法來解決這個問題。 但是只要記住組件在預制層次結構中的位置,你就會沒事的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM