正如标题所示,我正在尝试使用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));
}
}
}
代码有效,但行是“向后”生成的,这意味着外部行包含更多的hexes,而内部行包含最小的行。
这显然是我想要实现的相反效果。几个小时我一直在搞乱这段代码,我无法弄清楚原因。
有什么想法吗?
答案 0 :(得分: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));
}
}
}
}
现在,有人可以建议如何清理它吗?我的大脑失败了......
答案 1 :(得分:0)
只是一个提示,Awake发生在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;
}