我正在尝试创建一个带有动态(其文本内容可能会有所不同)前缀的输入字段(TextMesh Pro)。这张壮观的图片应该可以解释我们的目标。
https://i.stack.imgur.com/97agV.jpg
因此,我设置了一个 TextMeshPro 文本用作前缀,并通过脚本尝试相应地“移动”TextArea。事实上,TextArea 是一个 RectTransform,而我正在 ScreenSpace 渲染模式下操作。
我正在尝试这样:
private TextMeshProGUI prefix;
private RecTransform textArea;
public void ChangePrefixTo(string newPrefix)
{
float oldWidth = prefix.preferredWidth;
prefix.text = newPrefix;
float newWidth = prefix.preferredWidth;
Vector2 newPos = new Vector2();
newPos.x = textArea.position.x + (newWidth - oldWidth);
newPos.y = textArea.position.y;
textArea.position = newPos;
}
Run Code Online (Sandbox Code Playgroud)
,但是 textArea 被射入星星。如何根据 TextMeshPro 文本的大小映射 RectTransform 位置?
感谢您的帮助,鲸鱼万岁