2

正如标题所示,我正在尝试使用 C# 在 Unity 中生成程序十六进制网格。

void Start () 
{
    //5 mid
     startRowSize = 5;

    for (int i = 0; i < startRowSize; i++)
    {
        GameObject newHex = (GameObject)Instantiate(hex);
        hexWidth = newHex.gameObject.renderer.bounds.size.z;
        newHex.transform.Rotate(new Vector3(0,30,0));
        newHex.transform.position = new Vector3((i * hexWidth),0,0);
    }

    for (int row = 0; row <= startRowSize-1; row ++)
    {
    for (int i = 0; i < row; i++)
    {
        GameObject newHex = (GameObject)Instantiate(hex);
        newHex.transform.Rotate(new Vector3(0,30,0));
        newHex.transform.position = new Vector3(((i*hexWidth)+((hexWidth/2))+(row*(hexWidth/2))),0,((startRowSize-row))*-(hexWidth/1.17f));
    }
    }
}

代码有效,但是行是“向后”生成的,这意味着外行包含更多的十六进制数,而内行包含最小的。

这显然是我试图达到的相反效果。我已经把这段代码弄乱了几个小时,我不知道为什么。

有什么想法吗?

4

2 回答 2

0

只是提示,唤醒发生在 Start() 之前。

void Awake() 
{
   //5 mid
  startRowSize = 10;

  for (int i = 0; i < startRowSize; i++)
  {
      GameObject newHex = HexCreator();
      hexWidth = newHex.gameObject.renderer.bounds.size.z;
      newHex.transform.position = new Vector3(i * hexWidth, 0, 0);
  }

  for (int row = 0; row < startRowSize; row++)
     for (int i = 0; i < startRowSize-1-row; i++)
     {
         GameObject newHex = HexCreator();

         if (row == 0)
            newHex.transform.position = new Vector3(i*hexWidth + hexWidth/2, 0, -(hexWidth/1.17f));

         else
            newHex.transform.position = new Vector3(i*hexWidth + ((row+1)*(hexWidth/2)), 0,(row+1)*-(hexWidth/1.17f));
    }

  for (int row = 0; row < startRowSize; row++)
     for (int i = 0; i < startRowSize-1-row; i++)
     {
        GameObject newHex = HexCreator();

        if (row == 0)
            newHex.transform.position = new Vector3(i*hexWidth+ hexWidth/2, 0, hexWidth/1.17f);

        else
            newHex.transform.position = new Vector3(i*hexWidth + ((row+1)*(hexWidth/2)), 0, (row+1)*(hexWidth/1.17f));
     }
}

由于您重复了此代码,因此我将其变成了一个函数。这样,如果您需要升级它,您所做的就是转到该功能并在那里进行更改。

private GameObject HexCreator()
{
    GameObject newHex = (GameObject)Instantiate(hex);
    newHex.transform.Rotate(new Vector3(0,30,0));

    return newHex;
}
于 2013-11-13T20:58:20.307 回答
0

因此,在又搞砸了几个小时的代码之后,我弄清楚了为什么它没有正确迭代。他是精炼的密码...

void Start () 
{
    //5 mid
     startRowSize = 10;

    for (int i = 0; i < startRowSize; i++)
    {
        GameObject newHex = (GameObject)Instantiate(hex);
        hexWidth = newHex.gameObject.renderer.bounds.size.z;
        newHex.transform.Rotate(new Vector3(0,30,0));
        newHex.transform.position = new Vector3((i * hexWidth),0,0);
    }

    for (int row = 0; row < startRowSize; row++)
    {
        for (int i = 0; i < startRowSize-1-row; i++)
            {
                GameObject newHex = (GameObject)Instantiate(hex);
                newHex.transform.Rotate(new Vector3(0,30,0));

            if (row == 0)
                {
                    newHex.transform.position = new Vector3(((i*hexWidth)+(hexWidth/2)),0,-(hexWidth/1.17f));
                }
            else
                {
                    newHex.transform.position = new Vector3((i*hexWidth)+((row+1)*(hexWidth/2)),0,(row+1)*-(hexWidth/1.17f));
                }
            }
    }

    for (int row = 0; row < startRowSize; row++)
    {
        for (int i = 0; i < startRowSize-1-row; i++)
            {
                GameObject newHex = (GameObject)Instantiate(hex);
                newHex.transform.Rotate(new Vector3(0,30,0));

            if (row == 0)
                {
                    newHex.transform.position = new Vector3(((i*hexWidth)+(hexWidth/2)),0,(hexWidth/1.17f));
                }
            else
                {
                    newHex.transform.position = new Vector3((i*hexWidth)+((row+1)*(hexWidth/2)),0,(row+1)*(hexWidth/1.17f));
                }
            }
    }
}

现在,任何人都可以建议如何清理它吗?我的大脑一片空白……

于 2013-11-13T20:19:03.763 回答