简体   繁体   English

如何生成多个ui按钮,将其放置在画布的左上角,并在按钮之间留出相等的间距?

[英]How can I generate number of ui buttons position them at the canvas top left corner and make a equal spaces between the buttons?

parent is the Canvas I want to position the buttons at the top left corner. parent是我想将按钮放置在左上角的Canvas。 And by spaces I mean to position each button under the next one with eual space for example : 通过空格,我的意思是将每个按钮放置在下一个具有相等空格的按钮下面,例如:

Button 1 按钮1

Button 2 按钮2

Button 3 按钮3

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GenerateUIButtons : MonoBehaviour
{
    public GameObject buttonPrefab;
    public GameObject parent;
    public int numberOfButtons;
    public float spaceBetweenButtons;

    // Start is called before the first frame update
    void Start()
    {
        float minX = parent.GetComponent<RectTransform>().position.x + parent.GetComponent<RectTransform>().rect.xMin;
        float maxY = parent.GetComponent<RectTransform>().position.y + parent.GetComponent<RectTransform>().rect.yMax;
        float z = parent.GetComponent<RectTransform>().position.z;

        Vector3 topLeft = new Vector3(minX, maxY, z);

        for (int i = 0; i < Rotate.names.Length; i++)
        {
            GameObject newButton = Instantiate(buttonPrefab);
            newButton.name = Rotate.names[i];
            newButton.transform.position = topLeft;
            newButton.transform.SetParent(parent.transform, false);
        }
    }

    // Update is called once per frame
    void Update()
    {

    }
}

I tried this part to calculate the top left position of the canvas also tried using localPosition but in both cases localPosition and position it's creating the buttons outside the canvas area above the canvas: 我尝试了这一部分来计算画布的左上角位置,也尝试使用localPosition进行尝试,但是在两种情况下,localPosition和位置都是在画布上方的画布区域之外创建按钮:

UI按钮

Two approaches, first the simple one, use Vertical/HorizontalLayoutGroup. 有两种方法,第一种是简单的,使用Vertical / Horizo​​ntalLayoutGroup。 Those contains spacing and padding variables to set your items. 这些包含用于设置项目的interval和padding变量。

The second one is to add a parent container and make your button as children of it. 第二个是添加一个父容器并使您的按钮成为其子容器。

If you have a defined amount of items then no need for code, just set the anchoring values. 如果项目的数量已定义,则无需代码,只需设置锚定值即可。

If you want them stacked vertically: 如果要垂直堆叠:

minX = 0 maxX = 1
minY = 2/3 maxY = 1

minX = 0 maxX = 1
minY = 1/3 maxY = 2/3

minX = 0 maxX = 1
minY = 0 maxY = 1/3

then you can use Left/Top/Right/Bottom to set the margin and spacing. 然后您可以使用“左” /“上” /“右” /“下”设置边距和间距。 So if you want to occupy full width and have 5px space between items 因此,如果您要占用全宽并在项目之间留出5px的间距

Left = 0 Top = 0
Right = 0 Bottom = 2.5

Left = 0 Top = 2.5
Right = 0 Bottom = 2.5

Left = 0 Top = 2.5
Right = 0 Bottom = 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用ItemsControl将按钮绑定到画布Windows Phone的顶部/左侧位置 - Using ItemsControl to bind buttons top/left position on canvas Windows Phone 更改按钮大小后如何重新排列按钮之间的空格? - How can I rearrange spaces between buttons after changing their size? 如何在圆圈形成中的班级成员之间留出相等的空间? - How can i make equal spaces between the squad members in the circle formation? 按钮是使用stringbuilder在后面的代码中创建的。 如何使它们调用函数? - Buttons were created using stringbuilder in code behind. How can I make them call functions? 带有UI按钮的UI画布图像 - UI Canvas Image with UI Buttons 如何为列表中的元素加满使其等于10? - How can I top up the number of elements in a list to make it equal to ten? Unity UI Canvas 可选按钮 - Unity UI Canvas selectable buttons 如何将Button保持在TableLayoutPanel下方的相同位置? - How can I keep Buttons at the same position below a TableLayoutPanel? 如何在检查器的编辑器脚本中的添加组件按钮和其他自定义按钮之间隐藏或留出空间? - How can I hide or make space between the add component button and other custom buttons in editor script in the inspector? 如何禁用主菜单中的所有 ui 按钮? - How can I disable all the ui buttons in the main menu?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM