Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/322.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统一中的径向菜单问题_C#_Unity3d - Fatal编程技术网

C# 统一中的径向菜单问题

C# 统一中的径向菜单问题,c#,unity3d,C#,Unity3d,我已经在Unity中制作了一个径向菜单预置,可以自动调整按钮之间的间距。我已经在我的单位预制中包含了这个径向菜单,所以玩家可以从每个单位的可用移动选项中进行选择 菜单看起来非常适合直接定向的对象,但尽管菜单是子对象,但它不会旋转以匹配其父对象。按钮确实会旋转到正确的方向,但“径向”菜单本身默认为其原始设置:将按钮放置在一个180弧中,该弧以-90度为起点 public void SetMenu() { float angle = -180 / (childButtons

我已经在Unity中制作了一个径向菜单预置,可以自动调整按钮之间的间距。我已经在我的单位预制中包含了这个径向菜单,所以玩家可以从每个单位的可用移动选项中进行选择

菜单看起来非常适合直接定向的对象,但尽管菜单是子对象,但它不会旋转以匹配其父对象。按钮确实会旋转到正确的方向,但“径向”菜单本身默认为其原始设置:将按钮放置在一个180弧中,该弧以-90度为起点

public void SetMenu()
{
        
    float angle = -180 / (childButtons.Count - 1) * Mathf.Deg2Rad; //180 is the total degree range our buttons will fill. Negative to put the buttons beneath our ship. Count -1 because we want the distance of the gaps between buttons.

    for (int i = 0; i < childButtons.Count; i++) //For each button...
    {

        float xpos = Mathf.Cos(angle * i) * buttonDistance; //Make coordinates along an angle increment depending on the total number of buttons to...
        float zpos = Mathf.Sin(angle * i) * buttonDistance;

        childButtons[i].transform.position = new Vector3(transform.position.x + xpos, transform.position.y, transform.position.z + zpos); //Place the button
    }
}
public void SetMenu()
{
浮动角度=-180/(childButtons.Count-1)*Mathf.Deg2Rad;//180是我们的按钮将填充的总度数范围。负是将按钮放在船下。计数-1,因为我们需要按钮之间的间距。
对于(int i=0;i


任何帮助都将不胜感激

旋转父对象会影响子对象的旋转。 如果要保持其原始旋转,则应将其旋转到与父对象相反的方向

但有更简单、更清晰的方法来解决这个问题

将一个空对象设为ship的子对象,按钮的父对象。 只需从母船反向旋转。 使用此方法时,不需要使用极坐标转换重新定位子按钮


(空对象的局部位置应为(0,0,0))

通过调整局部位置而不是设置全局位置来解决。早期的localPosition调整尝试似乎失败了,因为缩放差异很大,UI元素不使用z轴。buttonDistance需要是原来设置的20倍,才能在localPosition产生相同的结果,现在一切都正确定向

新代码是:

public void SetMenu()
{
    
    float angle = -180 / (childButtons.Count - 1) * Mathf.Deg2Rad; //-180 is the total degree range our buttons will fill. -1 because we want the distance of the gaps between buttons.

    for (int i = 0; i < childButtons.Count; i++) //For each button...
    {
        float xpos = Mathf.Cos(angle * i) * buttonDistance; //Make coordinates along an angle increment depending on the total number of buttons to...
        float ypos = Mathf.Sin(angle * i) * buttonDistance;

        childButtons[i].transform.localPosition = new Vector3(transform.localPosition.x + xpos, transform.localPosition.y + ypos, transform.localPosition.z); //Place the button
    }

}
public void SetMenu()
{
浮动角度=-180/(childButtons.Count-1)*Mathf.Deg2Rad;//-180是按钮将填充的总度数范围。-1,因为我们需要按钮之间的间距。
对于(int i=0;i
听起来好像还有更多的代码没有显示在这里。。总的来说,有一个菜单家长,让孩子们在本地空间安排自己,不是更容易吗?这样,他们甚至不必关心家长的方向。这个菜单是由精灵或UI图像组成的吗?如果是Sprite,您只需使它们都成为平面的子对象,它们将跟随父对象的旋转。否则,只需将图像设置为公共父级,然后旋转父级即可。为了澄清这一点,我使用的层次结构是Unit->Canvas->Canvas(这是上述函数所在的位置)->按钮。纽扣位置是10。我的目标是在减少和增加按钮数量后,能够定期运行上述功能,将它们以180度弧度等距放置在装置下方,而不考虑总数量。他希望通过编程来解决这个问题,而不是在正确的方向上创建大量的UI元素。不知道为什么它会基于世界角度而不是基于父方向放置它们。谢谢你看!