如何使用字典获取网格中的点位置?
How to get point position in the grid by using dictionary?
有人可以帮助我吗?
我为我的董事会制作了一个网格。当我试图获得棋盘上正方形的点位置时,控制台 returns only (0,0).
这是我的积分代码:
public struct Point
{
public int X {get; set;}
public int Y {get; set;}
public Point(int x, int y){
this.X = x;
this.Y = y;
}
}
这是脚本,其中每个方块在实例化时在网格中得到一个点:
public Point GridPosition { get; set; }
public void Setup(Point gridPos, Vector3 worldPos)
{
this.GridPosition = gridPos;
transform.position = worldPos;
}
private void OnMouseDown(){
Debug.Log (GridPosition.X + ", "+ GridPosition.Y );
}
这是我的主脚本,其中包含字典部分:
public static Dictionary<Point,Grid> Tiles { get; set; }
void Start()
{
CreateLevel ();
}
void CreateLevel()
{
Tiles = new Dictionary<Point,Grid> ();
}
private void PlaceTilesColliders(Vector3 tileStart, float tileOffset){
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
TileCollider.GetComponent<Grid> ().Setup (new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));
Tiles.Add (new Point (x, y), Instantiate(TileCollider).GetComponent<Grid>());
}
}
}
因此,每次 (0,0) 时控制台 return,与单击哪个方块无关。
谁能解释一下如何获得网格中正方形的真实点位置?
首先尝试 Instantiate
,然后配置生成的新网格并添加到字典中。
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
GameObject newGrid = Instantiate(TileCollider);
newGrid.GetComponent<Grid>().Setup(new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));
Tiles.Add(new Point (x, y), newGrid.GetComponent<Grid>());
}
}
不过,我建议您注意父子关系,因为现在实例化的对象没有父对象。
avariant 的回答基本上是正确的,但我想指出您的代码 实际在做什么 以及为什么您会得到这些值。
让我们看看这个循环:
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
TileCollider.GetComponent<Grid> ().Setup (new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));
Tiles.Add (new Point (x, y), Instantiate(TileCollider).GetComponent<Grid>());
}
}
我们遍历 Y 和 X(这很好)然后我们调用 TileCollider.GetComponent<Grid> ()
。等等,等等,TileCollider
?这是什么?这不可能是场景中的文件之一,我们还没有使用我们的 X 和 Y 坐标从场景中 fetch 一个 GameObject 来获取这个引用...
这意味着我们对它所做的任何事情都不会影响我们在游戏世界中的图块!而且因为引用没有更新,我们会不断更新它的值到新的X 和 Y 位置,覆盖我们已经完成的并且没有任何影响。
糟糕。
这就是为什么 avariant 说您需要调用 Instantiate
并创建一个新图块,然后从该 GameObject 获取组件并对其调用 Setup()
。
有人可以帮助我吗? 我为我的董事会制作了一个网格。当我试图获得棋盘上正方形的点位置时,控制台 returns only (0,0).
这是我的积分代码:
public struct Point
{
public int X {get; set;}
public int Y {get; set;}
public Point(int x, int y){
this.X = x;
this.Y = y;
}
}
这是脚本,其中每个方块在实例化时在网格中得到一个点:
public Point GridPosition { get; set; }
public void Setup(Point gridPos, Vector3 worldPos)
{
this.GridPosition = gridPos;
transform.position = worldPos;
}
private void OnMouseDown(){
Debug.Log (GridPosition.X + ", "+ GridPosition.Y );
}
这是我的主脚本,其中包含字典部分:
public static Dictionary<Point,Grid> Tiles { get; set; }
void Start()
{
CreateLevel ();
}
void CreateLevel()
{
Tiles = new Dictionary<Point,Grid> ();
}
private void PlaceTilesColliders(Vector3 tileStart, float tileOffset){
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
TileCollider.GetComponent<Grid> ().Setup (new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));
Tiles.Add (new Point (x, y), Instantiate(TileCollider).GetComponent<Grid>());
}
}
}
因此,每次 (0,0) 时控制台 return,与单击哪个方块无关。 谁能解释一下如何获得网格中正方形的真实点位置?
首先尝试 Instantiate
,然后配置生成的新网格并添加到字典中。
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
GameObject newGrid = Instantiate(TileCollider);
newGrid.GetComponent<Grid>().Setup(new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));
Tiles.Add(new Point (x, y), newGrid.GetComponent<Grid>());
}
}
不过,我建议您注意父子关系,因为现在实例化的对象没有父对象。
avariant 的回答基本上是正确的,但我想指出您的代码 实际在做什么 以及为什么您会得到这些值。
让我们看看这个循环:
for (int y = 0; y < 8; y++) {
for (int x = 0; x < 8; x++) {
TileCollider.GetComponent<Grid> ().Setup (new Point (x, y), new Vector3 (tileStart.x + (tileOffset * x), tileStart.y - (tileOffset * y), 0));
Tiles.Add (new Point (x, y), Instantiate(TileCollider).GetComponent<Grid>());
}
}
我们遍历 Y 和 X(这很好)然后我们调用 TileCollider.GetComponent<Grid> ()
。等等,等等,TileCollider
?这是什么?这不可能是场景中的文件之一,我们还没有使用我们的 X 和 Y 坐标从场景中 fetch 一个 GameObject 来获取这个引用...
这意味着我们对它所做的任何事情都不会影响我们在游戏世界中的图块!而且因为引用没有更新,我们会不断更新它的值到新的X 和 Y 位置,覆盖我们已经完成的并且没有任何影响。
糟糕。
这就是为什么 avariant 说您需要调用 Instantiate
并创建一个新图块,然后从该 GameObject 获取组件并对其调用 Setup()
。