如何在Unity中单击时从十六进制映射获取坐标

问题描述 投票:1回答:1

我正在把棋盘游戏称为Hex。当我点击一个图块时,它会变为蓝色或黄色,但我还需要知道该图块的坐标是什么。

我不能用...

rend.transform.position;

...因为我想要接收的坐标看起来类似于左下角的(0,0)和它上面的(0,1):enter image description here

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

enter image description here

我在生成十六进制映射时使用列和行变量将这些打印出来:

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

我想我需要能够在实例化时为每个Hex Model分配列和行的值,但我不知道该怎么做。我尝试了几种我在网上找到的解决方案以及使用GetComponent,但我无法让它们工作。如果有人知道如何做到这一点我会非常感激!

c# unity3d gameobject hexagonal-tiles
1个回答
1
投票

GetComponent在您的情况下不起作用的原因是因为Coordinates脚本位于十六进制预制件的子对象上。您可以通过调用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
// ...

有许多可能的方法来解决这个问题。但是请记住组件在预制层次结构中的位置,你会没事的。

© www.soinside.com 2019 - 2024. All rights reserved.