如何在Unity3D中删除按钮之间的间距? [英] How to remove spacing between buttons in Unity3D?

查看:132
本文介绍了如何在Unity3D中删除按钮之间的间距?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Unity设计的游戏中以编程方式创建按钮.我实例化了按钮的预制件并将它们作为画布上的Panel对象的父对象.我已将面板设置为具有以下属性的水平布局组:

I'm creating buttons programmatically in a game I'm designing in Unity. I instantiate the button prefab and parent them to the Panel object on the canvas. I've set the Panel to have a Horizontal Layout Group with the following properties:

我已将以下组件添加到按钮预制件中,以实现所需的外观:

I've added the following components to the button prefab to achieve the look I require:

完成所有这些之后,我得到的结果是:

After doing all this here's the result I end up with:

因此,我的问题是,如何删除每个按钮之间的间距?这些按钮应该彼此相邻(但不重叠)以看起来像一个句子.我有单独的按钮的原因是,用户需要单击句​​子中正确的2个字母字符(在上面的示例中是字母ar).我已将属性的间距设置为0,并选中/取消选中了各种选项,但是没有运气!

Therefore, my question is, how can I remove the spacing between each of the buttons? The buttons are supposed to be right next to each other (but not overlapping) to look like a sentence. The reason why I've got separate buttons is so that the user needs to click the correct 2 letter characters in the sentence (in the above example is the letters ar). I've set the spacing on the properties to 0 and checked/unchecked the various options but no luck!

我在Panel和Button对象上都没有选中Child Force Expand的width选项,但是句子看起来都塞满了…….我如何确保按钮前后排列整齐(即不重叠)?

I've unchecked the width option for Child Force Expand on both the Panel and Button objects but the sentence looks all crammed.... how could I ensure the buttons sit neatly one after the other (i.e not overlapping)?

与以下评论相关的代码:

Code relating to comment below:

 for (int i = 0; i < letterArray.Length; i++)
        {
            button = (GameObject)Instantiate(buttonPrefab);
            button.transform.SetParent(buttonPanel.transform, false);
            button.GetComponentInChildren<Text>().text = letterArray[i];

            FitButtonWidthToText();
        }
    }

    public void FitButtonWidthToText()
    {
        int widthOfTheText = 0;
        CharacterInfo characterInfo;
        foreach (char c in button.GetComponentInChildren<Text>().text)
        {
            button.GetComponentInChildren<Text>().font.GetCharacterInfo(c, out characterInfo, button.GetComponentInChildren<Text>().fontSize);
            widthOfTheText += characterInfo.advance;
        }
        buttonTransform.sizeDelta = new Vector2(widthOfTheText, buttonTransform.sizeDelta.y);
    }

推荐答案

取消选中 Child Force扩展:宽度

来自文档:

Child Force Expand:是否强制子布局元素扩展以填充额外的可用空间.

Child Force Expand: Whether to force the child layout elements to expand to fill additional available space.

检查宽度(分别为 height )将使元素水平填充可用空间(垂直为 ).

Checking width (resp. height) will have the elements fill the available space horizontally (resp. vertically).

不要布局组的子项上放置 Content Size Fitter .更多信息:使UI元素适合其内容的大小.

Do not put a Content Size Fitter on children of a Layout Group. More info: Making UI elements fit the size of their content.

基于我们的讨论:因此,以上链接中描述的步骤仅适用于Unity的最新版本.如果您坚持使用更成熟的版本,则可以保留 ContentSizeFitter 以获得所需的结果,但要注意的是,如果更改子元素的大小,则布局不会自动更新,如您所愿.为了解决这个问题,您可以强制进行部分重新布置,如

Based on our discussion: So the steps described in the above link only work on the most recent versions of Unity. If you are stuck with a more mature version, you may keep the ContentSizeFitter to achieve the desired result, with the gotcha that if you change the size of a child element, the layout won’t update automatically as you would expect. To remedy this, you can force a partial re-layout like explained by Stephan-B in this thread:

以下函数 LayoutRebuilder.ForceRebuildLayoutImmediate 应该有用.您可以对层次结构中的根(顶级父对象)对象调用此函数,而不必强制重建可能会对性能产生重大影响的所有Canvas,而该函数包含影响所讨论对象的布局组件.

The following function LayoutRebuilder.ForceRebuildLayoutImmediate should prove useful. Instead of forcing a rebuild of all the Canvases which can have a significant performance impact, you can call this function on the root (top parent) object in the hierarchy the contains the layout component affecting the object in question.

这篇关于如何在Unity3D中删除按钮之间的间距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆