【发布时间】:2021-04-05 04:14:58
【问题描述】:
我正在尝试在中心六边形周围实例化一些六边形,但其中一些之间出现了奇怪的差距,我无法弄清楚是什么原因造成的。顶部和底部的六边形应该更靠近中间的白色六边形。我已经在这个网站CatLikeCodiing 上阅读了有关如何创建六边形网格的信息,并且数学看起来是正确的。任何想法可能是什么问题?
outerRadius = hexButton.GetComponent<RectTransform>().rect.x;
innerRadius = (Mathf.Sqrt(3) / 2) * outerRadius;
Debug.Log(string.Format("OUTER RADIUS: {0}", outerRadius));
Debug.Log(string.Format("INNER RADIUS: {0}", innerRadius));
startPos = Instantiate(hexButton, Vector3.zero, hexButton.transform.rotation);
startPos.transform.SetParent(GameObject.Find("PanelMain").transform, false);
startPos.name = string.Format("Start Hex");
for (int i = 0; i < level; i++)
{
if (i <= 6)
{
var temp = Instantiate(hexButton, new Vector3(outerRadius * 2, 0, 0), hexButton.transform.rotation);
temp.transform.RotateAround(hexButton.transform.position, Vector3.back, 60 * i);
temp.transform.rotation = hexButton.transform.rotation;
temp.transform.SetParent(GameObject.Find("PanelMain").transform, false);
temp.GetComponent<Button>().image.color = Color.green;
temp.name = string.Format("Hex: {0}", i);
}
}
修复
#region
outerRadius = hexButton.GetComponent<RectTransform>().rect.y * 0.5f;
innerRadius = (Mathf.Sqrt(3) / 2) * outerRadius;
hexWidth = hexButton.GetComponent<RectTransform>().rect.width;
offset = hexWidth * 0.93333333333f;
Debug.Log(string.Format("OUTER RADIUS: {0}", outerRadius));
Debug.Log(string.Format("INNER RADIUS: {0}", innerRadius));
Debug.Log(string.Format("WIDTH: {0}", gap));
startPos = Instantiate(hexButton, Vector3.zero, hexButton.transform.rotation);
startPos.transform.SetParent(GameObject.Find("PanelMain").transform, false);
startPos.name = string.Format("Start Hex");
for (int i = 0; i < level -1 ; i++)
{
if (i <= 6)
{
var temp = Instantiate(hexButton, new Vector3(offset, 0, 0), hexButton.transform.rotation);
temp.transform.RotateAround(hexButton.transform.position, Vector3.back, 60 * i);
temp.transform.rotation = hexButton.transform.rotation;
temp.transform.SetParent(GameObject.Find("PanelMain").transform, false);
temp.GetComponent<Button>().image.color = Color.green;
temp.name = string.Format("Hex: {0}", i);
}
}
#endregion
【问题讨论】:
-
我相信您的问题是使用 .rect.x。您链接的教程指定外半径是从中心到六边形任意角的距离。如果您查看示例中显示的视觉效果,如果您要在此处获取图像的宽度,则不会是这个长度。一个快速而肮脏的解决方法是选中预制件 Preseve Aspect Ratio 的 Image 组件上的框为 true,然后从 outerRadius 中减去值,直到它看起来正确。
-
嗨 user3657449 感谢您指出我更新了代码以使用 rect.y / 0.5f 来获得正确的外半径。