简体   繁体   English

如何计算 Unity3D 中组按钮的自动大小

[英]How calculate autosize for group buttons in Unity3D

I have button's horizontal layout group with TextMeshPro.我有带有 TextMeshPro 的按钮的水平布局组。 How can I calculate auto sizing font for it and set minimum value on all buttons?如何计算自动调整字体大小并在所有按钮上设置最小值? It's need for relative UI.它需要相对的用户界面。

I have now:我现在有了:

在此处输入图像描述

And how I want:以及我想要的方式: 在此处输入图像描述

I tried this code:我试过这段代码:

public class FontSizeController: MonoBehaviour
{
    private void Start()
    {
        SetMinFontForAnswers(transform,
            FindMinFontSizeAnswerOptions(transform));
    }

    private void SetMinFontForAnswers(Transform answerPanel, float minFontSize)
    {
        for (var answerIndex = 0; answerIndex < answerPanel.childCount; answerIndex++)
        {
            var meshProUgui = answerPanel.GetChild(answerIndex).GetChild(1).GetComponent<TextMeshProUGUI>();
            meshProUgui.fontSize = minFontSize;
        }
    }

    private float FindMinFontSizeAnswerOptions(Transform answerOptions)
    {
        var minFontSize = -1f;

        for (var answerIndex = 0; answerIndex < answerOptions.childCount; answerIndex++)
        {
            var component = answerOptions.GetChild(answerIndex).GetChild(1).GetComponent<TextMeshProUGUI>();
            component.enableAutoSizing = true;
            component.ForceMeshUpdate();
            if (IsAnswerOptionActive(answerOptions, answerIndex) && IsMinFontSizeOrNotInitialized(component, minFontSize))
            {
                minFontSize = component.fontSize;
            }

            component.enableAutoSizing = false;
        }

        return minFontSize;
    }

    private bool IsAnswerOptionActive(Transform answerOptions, int answerIndex)
    {
        return answerOptions.GetChild(answerIndex).gameObject.activeSelf;
    }
    private bool IsMinFontSizeOrNotInitialized(TMP_Text textComponent, float minFontSize)
    {
        return textComponent.fontSize < minFontSize || minFontSize == -1f;
    }

}

But it doesn't work on Start and work only in Update method.但它不适用于 Start 并且仅适用于 Update 方法。 But when I used it in Update method I can see when text font size changing.但是当我在 Update 方法中使用它时,我可以看到文本字体大小何时发生变化。 It is quickly but I want do this before panel will be render.它很快,但我想在面板渲染之前这样做。

Question panel is not active by default问题面板默认不激活

在此处输入图像描述

AnswerOptionsPanel:答案选项面板:

在此处输入图像描述

TextMeshPro Text in AnswerOptionsPanel: AnswerOptionsPanel 中的 TextMeshPro 文本:

在此处输入图像描述

You can get your Text assets current size with using this method;您可以使用此方法获取文本资源的当前大小;

Text.cachedTextGenerator.fontSizeUsedForBestFit

You can compare your texts instant size and then change.您可以比较您的文本即时大小,然后更改。 For example;例如;

UnityEngine.UI.Text[] myTexts;

void OptimiseTextSizes()
{

    int minSize = 999;
    foreach (UnityEngine.UI.Text t in myTexts)
    {
        if (t.cachedTextGenerator.fontSizeUsedForBestFit < minSize)
        {
            minSize = t.cachedTextGenerator.fontSizeUsedForBestFit;
        }
    }

    foreach (UnityEngine.UI.Text t in myTexts)
    {
        t.resizeTextMaxSize = minSize;
    }

}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM