如何使用脚本更改 Unity UI 中的占位符文本?
How to change placeholder text in Unity UI using script?
对于我的项目,默认值是根据外部输出计算的,可以使用新 Unity UI 中的输入字段更改这些值。如果值未更改,则计算后应出现灰色占位符。我真的不知道如何通过脚本更改占位符文本,甚至在任何地方都找不到解决方案。我试过这个:
gameObject.GetComponent<InputField>().placeholder = uv.value;
脚本附加到给定的输入字段游戏对象。但是,为了在输入字段中获取写入值,我使用了这行代码:
uv.value = gameObject.GetComponent<InputField>().text;
它工作正常。我错过了什么?一些帮助将不胜感激,在这里写是我最后的选择。谢谢转发!
Placeholder 只是一个文本组件。您可以通过以下方式更改其文本:
gameObject.GetComponent<InputField>().placeholder.GetComponent<Text>().text = "Something";
注意GetComponent<InputField>().placeholder
是一个图形组件,不是你要找的机器人:)
您还可以向下转换 placeholder
对象,因为 Text 继承自 Graphic class。
这样的东西也可以。
Graphic graphic = gameObject.GetComponent<InputField>().placeholder;
((Text)graphic).text = "Hello";
这对我有用,但请记住将占位符游戏对象分配给输入字段设置 -> 占位符,否则会出现空错误。
TextMeshProUGUI placeholder = (TextMeshProUGUI)userNameInput.placeholder;
placeholder.text = "你好";
对于我的项目,默认值是根据外部输出计算的,可以使用新 Unity UI 中的输入字段更改这些值。如果值未更改,则计算后应出现灰色占位符。我真的不知道如何通过脚本更改占位符文本,甚至在任何地方都找不到解决方案。我试过这个:
gameObject.GetComponent<InputField>().placeholder = uv.value;
脚本附加到给定的输入字段游戏对象。但是,为了在输入字段中获取写入值,我使用了这行代码:
uv.value = gameObject.GetComponent<InputField>().text;
它工作正常。我错过了什么?一些帮助将不胜感激,在这里写是我最后的选择。谢谢转发!
Placeholder 只是一个文本组件。您可以通过以下方式更改其文本:
gameObject.GetComponent<InputField>().placeholder.GetComponent<Text>().text = "Something";
注意GetComponent<InputField>().placeholder
是一个图形组件,不是你要找的机器人:)
您还可以向下转换 placeholder
对象,因为 Text 继承自 Graphic class。
这样的东西也可以。
Graphic graphic = gameObject.GetComponent<InputField>().placeholder;
((Text)graphic).text = "Hello";
这对我有用,但请记住将占位符游戏对象分配给输入字段设置 -> 占位符,否则会出现空错误。
TextMeshProUGUI placeholder = (TextMeshProUGUI)userNameInput.placeholder;
placeholder.text = "你好";