如何在没有usind索引的情况下选择/调用数组中的实例化GameObject [英] How do I select/call an Instantiated GameObject within an array without usind index

查看:87
本文介绍了如何在没有usind索引的情况下选择/调用数组中的实例化GameObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为游戏地图创建随机网格生成基础,并且对如何根据其游戏坐标(而不是Vector3)调用实例化对象有些困惑.

I'm currently working on a random grid generation base for game map and I'm kinda stuck at how do I call an instantiated object based on their in game coordinates (not Vector3).

这是我的十六进制生成脚本:

This is my hex generation script:

public HexCell cellPrefab;
HexCell[] cells;

void CreateCell (int x, int z, int i) {

    Vector3 position;
    position.x = (x + z * 0.5f - z / 2) * (HexMetrics.innerRadius * 2f);
    position.y = 0f;
    position.z = z * (HexMetrics.outerRadius * 1.5f);

    HexCell cell = cells[i] = Instantiate<HexCell>(cellPrefab);
    cell.coordinates = HexCoordinates.FromOffsetCoordinates(x, z);
}

我可以使用对象数组的索引来称呼对象,但是我无法根据其给定的坐标来称呼它.

I can call the object fine using index of objects' array but I can't wrap my head around the idea of calling it based on its given coordinates.

这是我根据每个十六进制位置给他们坐标的方式;

This is how I give them coordinates based on each hex position;

public class HexCell : MonoBehaviour {
public HexCoordinates coordinates;
}

public struct HexCoordinates {

[SerializeField]
private int x, z;

public int X {
    get { return x; }
}

public int Y {
    get { return -X - Z; }
}

public int Z {
    get { return z; }
}

public HexCoordinates (int x, int z) {
    this.x = x;
    this.z = z;
}

public static HexCoordinates FromOffsetCoordinates (int x, int z) {
    return new HexCoordinates(x - z / 2, z);
}
}

那么我如何根据HexCell坐标调用/选择所需的十六进制?

So how do I call/select desired hex based on HexCell coordinates?

推荐答案

那么我如何根据HexCell坐标调用/选择所需的十六进制?

So how do I call/select desired hex based on HexCell coordinates?

这很容易做到,因为您使用了int而不是float来表示坐标.只需遍历cells数组.在每个循环中,从HexCell组件访问coordinates变量,然后比较x,y和z值.如果它们匹配,则在循环中返回当前的HexCell.如果不是,只需返回null.您也可以使用linq来做到这一点,但要避免使用它.

This is easy to do since you used int instead of float to represent the coordinate. Just loop over the cells array. In each loop, access the coordinates variable from the HexCell component then compare x, y and z values. If they match, return the current HexCell in the loop. If not, simply return null. You can also do this with linq but avoid it.

您的单元格数组:

HexCell[] cells;

从坐标获取HexCell:

HexCell getHexCellFromCoordinate(int x, int y, int z)
{
    //Loop thorugh each HexCell
    for (int i = 0; i < cells.Length; i++)
    {
        HexCoordinates hc = cells[i].coordinates;

        //Check if coordinate matches then return it
        if ((hc.X == x) && (hc.Y == y) && (hc.Z == z))
        {
            return cells[i];
        }
    }

    //No match. Return null
    return null;
}

由于坐标位于int而不是float中,因此您也可以使用 Vector3Int (需要Unity 2017.2及更高版本)代替x,y和z来表示它们.请注意,这不同于 Vector3 .

Since the coordinate is in int instead of float, you can also use Vector3Int(Requires Unity 2017.2 and above) to represent them instead of x, y and z. Note that this is different from Vector3.

HexCell getHexCellFromCoordinate(Vector3Int coord)
{
    //Loop thorugh each HexCell
    for (int i = 0; i < cells.Length; i++)
    {
        HexCoordinates hc = cells[i].coordinates;

        //Check if coordinate matches then return it
        if ((hc.X == coord.x) && (hc.Y == coord.y) && (hc.Z == coord.z))
        {
            return cells[i];
        }
    }

    //No match. Return null
    return null;
}

这篇关于如何在没有usind索引的情况下选择/调用数组中的实例化GameObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆