UI文本未呈现,而是按层次结构显示-缺少什么? [英] UI text is not rendered but appearing in hierarchy — what is missing?
问题描述
试图显示动态创建到画布上但未呈现的文本. 创建画布后,将一个空的gameObject附加为子对象,并将以下脚本附加到该子对象上.运行后,对象出现在层次结构中但未呈现.也许我遗漏了一些东西:
trying to show text dynamically created to the canvas but its not rendering. After creating canvas an empty gameObject attached as child and the following script is attached to that. After running the object appears in hierarchy but not rendered.maybe I'm missing something:
GameObject o;
void Start() {
GameObject test = new GameObject("myTextGO");
test.transform.SetParent(this.transform);
// test.transform.SetParent(o.transform);
Text myText = test.AddComponent<Text>();
myText.text = "Hello there!";
}
还尝试了其他几种组合,但未显示文本.需要知道解决方案.
Also tried few other combination but not text is not rendering. Need to know solution.
推荐答案
动态且手动创建UI文本时需要做的三件事:
Three things required to do when creating UI text dynamically and manually:
- 字体
- 适当的养育子女
- RectTransform(现在,由UI组件自动由Unity附加 附加到GameObject)
- Font
- Proper Parenting
- RectTransform (Now, automatically attached by Unity when UI component is attached to a GameObject)
我假设您使用的是最新的Unity版本,因此您只需要执行前两个操作即可:
I assume that you are using the latest Unity version so you just need to do the first two:
供文本组件使用的字体,如此答案所述.另外,使用transform.SetParent
时,必须为其提供false
.这将导致UI组件保持其本地方向.
Supply font for the Text component to use as mentioned this answer. Also, when using transform.SetParent
, you have to supply false
to it. This causes the UI component to keep its local orientation.
void CreateText(GameObject canvas, string text)
{
//Create Canvas Text and make it child of the Canvas
GameObject txtObj = new GameObject("myTextGO");
txtObj.transform.SetParent(canvas.transform, false);
//Attach Text,RectTransform, an put Font to it
Text txt = txtObj.AddComponent<Text>();
txt.text = text;
Font arialFont = Resources.GetBuiltinResource<Font>("Arial.ttf");
txt.font = arialFont;
txt.lineSpacing = 1;
txt.color = new Color(50 / 255, 50 / 255, 50 / 255, 255 / 255);
}
用法:
public GameObject canvas;
void Start()
{
CreateText(canvas, "Hello there!");
}
这就是手动创建文本组件的方法,但是不再使用.让Unity自动执行此操作.应该使用 DefaultControls
创建UI对象.这样可以减少创建代码失败或遇到类似问题的机会.使用 DefaultControls.Resources
和DefaultControls.CreateText
.
That's how to create a text component manually but that should no longer be used. Let Unity do this automatically. The DefaultControls
should be used to create UI Objects. It reduces the chances that the creation code will fail or that you'll run into similar issues in your question. Create the Text
resources with DefaultControls.Resources
then the Text
itself with DefaultControls.CreateText
.
void CreateText(GameObject canvas, string text)
{
DefaultControls.Resources uiResources = new DefaultControls.Resources();
GameObject uiText = DefaultControls.CreateText(uiResources);
uiText.transform.SetParent(canvas.transform, false);
uiText.GetComponent<Text>().text = text;
//Change the Text position?
//uiText.GetComponent<RectTransform>().sizeDelta = new Vector2(100f, 100f);
}
这篇关于UI文本未呈现,而是按层次结构显示-缺少什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!