如何在 Unity 中点击 Hex Map 获取坐标
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);
}
}
我想我需要能够在实例化时为每个 Hex 模型分配列和行的值,但我不确定该怎么做。我尝试了在网上找到的几种解决方案以及使用 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
// ...
这个问题有很多可能的解决方法。但是请记住组件在预制件层次结构中的位置,你会没事的。
我正在制作名为 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);
}
}
我想我需要能够在实例化时为每个 Hex 模型分配列和行的值,但我不确定该怎么做。我尝试了在网上找到的几种解决方案以及使用 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
// ...
这个问题有很多可能的解决方法。但是请记住组件在预制件层次结构中的位置,你会没事的。