【发布时间】:2020-03-03 06:14:09
【问题描述】:
我有带有 TextMeshPro 的按钮的水平布局组。如何计算自动调整字体大小并在所有按钮上设置最小值?它需要相对的 UI。
我现在有:
我试过这段代码:
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;
}
}
但它不适用于 Start 并且仅适用于 Update 方法。但是当我在 Update 方法中使用它时,我可以看到文本字体大小何时发生变化。这很快,但我想在面板渲染之前这样做。
问题面板默认不活动
答案选项面板:
AnswerOptionsPanel 中的TextMeshPro 文本:
【问题讨论】:
-
您的请求令人困惑。您是在问如何删除文本的自动调整大小?
-
不,我想使用自动调整大小来为所有按钮找到最佳字体大小,并在所有按钮上设置最小找到的字体大小值。
标签: c# user-interface unity3d