为什么在运行游戏时在文本组件上看不到任何文本?

时间:2019-08-26 01:31:06

标签: c# unity3d

我想查看“ Hello World”文本。但是什么都没有。

是否使用3D Object > 3D TextUI > TextUI > Text - TextMeshPro

无论我使用的是哪个文本组件,因为都没有附加网格渲染器,所以我遇到了异常,因此我要先将一个网格渲染器添加到“文本”组件中。

但是然后我得到了例外,说新创建的文本没有附加TextMesh组件,因此我要更改此行:

var textmesh = newText.GetComponent<TextMesh>();

收件人

var textmesh = newText.GetComponent<Text>();

但是仍然无法正常工作,例如textmesh为null。

所以我将文本组件更改为UI> Text 在文本中添加了MeshRenderer,现在没有异常/错误:

Text

Text

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AddTextToObject : MonoBehaviour
{
    public UnityEngine.GameObject[] objectsToNumber;
    public UnityEngine.GameObject text;
    public float yPadding;
    public bool rotateNumbers = false;
    public float rotationSpeed = 10f;
    public bool textChild = false;
    public bool textAbove = false;
    public bool textInFront = false;
    public bool textOnFaces = false;

    private List<GameObject> newTexts = new List<GameObject>();
    private MeshRenderer[] renderer;
    private Vector3 newPos;

    private void Start()
    {
        renderer = new MeshRenderer[objectsToNumber.Length];

        for (int i = 0; i < objectsToNumber.Length; i++)
        {
            GameObject newText = Instantiate(text);
            renderer[i] = newText.GetComponent<MeshRenderer>();

            if (textAbove == true)
            {
                newPos = new Vector3
                (
                 objectsToNumber[i].transform.position.x,
                 ((objectsToNumber[i].transform.position.y + renderer[i].bounds.extents.y) + yPadding),
                   objectsToNumber[i].transform.position.z
                 );
            }

            if (textInFront == true)
            {
                newPos = new Vector3
                (
                 ((objectsToNumber[i].transform.position.x + renderer[i].bounds.extents.x) + yPadding),
                 objectsToNumber[i].transform.position.y,
                   objectsToNumber[i].transform.position.z
                 );
            }

            newText.transform.position = newPos;
            newText.transform.parent = transform;
            newText.name = i.ToString();
            newText.tag = "Number";
            newTexts.Add(newText);
            var textmesh = newText.GetComponent<TextMesh>();
            textmesh.transform.localRotation = Quaternion.Euler(0, -90, 0);

            if (textAbove == true)
            {
                textmesh.text = i.ToString();
            }

            if (textInFront == true)
            {
                textmesh.text = "Hello World";
            }
        }
    }

    private void Update()
    {
        if (rotateNumbers == true)
        {
            for (int i = 0; i < newTexts.Count; i++)
            {
                newTexts[i].transform.Rotate(Vector3.up, 10 * rotationSpeed * Time.deltaTime);
            }
        }
    }
}

更新:

尝试使用TextMeshPro:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class AddTextToObject : MonoBehaviour
{
    public UnityEngine.GameObject[] objectsToNumber;
    public UnityEngine.GameObject text;
    public float yPadding;
    public bool rotateNumbers = false;
    public float rotationSpeed = 10f;
    public bool textChild = false;
    public bool textAbove = false;
    public bool textInFront = false;
    public bool textOnFaces = false;

    private List<GameObject> newTexts = new List<GameObject>();
    private MeshRenderer[] renderer;
    private Vector3 newPos;

    private void Start()
    {
        renderer = new MeshRenderer[objectsToNumber.Length];

        for (int i = 0; i < objectsToNumber.Length; i++)
        {
            GameObject newText = Instantiate(text);
            renderer[i] = objectsToNumber[i].GetComponent<MeshRenderer>();

            if (textAbove == true)
            {
                newPos = new Vector3
                (
                 objectsToNumber[i].transform.position.x,
                 ((objectsToNumber[i].transform.position.y + renderer[i].bounds.extents.y) + yPadding),
                   objectsToNumber[i].transform.position.z
                 );
            }

            if (textInFront == true)
            {
                newPos = new Vector3
                (
                 ((objectsToNumber[i].transform.position.x + renderer[i].bounds.extents.x) + yPadding),
                 objectsToNumber[i].transform.position.y,
                   objectsToNumber[i].transform.position.z
                 );
            }

            newText.transform.position = newPos;
            newText.transform.SetParent(transform, false);
            newText.name = i.ToString();
            newText.tag = "Number";
            newTexts.Add(newText);
            var textmesh = newText.GetComponent<TextMeshProUGUI>();
            textmesh.transform.localRotation = Quaternion.Euler(0, -90, 0);

            if (textAbove == true)
            {
                textmesh.text = i.ToString();
            }

            if (textInFront == true)
            {
                textmesh.text = "Hello World";
            }
        }
    }

    private void Update()
    {
        if (rotateNumbers == true)
        {
            for (int i = 0; i < newTexts.Count; i++)
            {
                newTexts[i].transform.Rotate(Vector3.up, 10 * rotationSpeed * Time.deltaTime);
            }
        }
    }
}

但是结果是文本不接近游戏对象。 文字“ Hello World”应像粘贴在每个游戏对象上一样。

Text

Text

我想做的是将文本“ Hello World”粘贴/附加到三个多维数据集“新游戏”,“选项”,“退出”中的每个立方体的正面:

Text

1 个答案:

答案 0 :(得分:1)

因为它不是Canvas的子代

  

画布是所有UI的区域    元素应该在里面

层次结构中不在Canvas下的任何UI元素都不会呈现。


Unity中有两种不同类型的文本组件。旧版TextMeshTextMeshPro没有 Canvas的情况下独立运行,并且像3D对象一样使用和定位。

还有UGUI或UI组件TextTextMeshProUGUICanvas结合使用。

因此,TextMeshPro的“正确”替代将不是Text,而是TextMesh。 (不过,如果您已经有了TMP,我不会朝这个方向发展。..它比旧版UI TextTextMesh更好。


您也不应该手动将MeshRenderer添加到对象!诸如TextTextMeshProUGUI之类的UI元素将带有它们自己的渲染器。也许您应该一次通过Unity UI Tutorial

还可以检出TextMeshPro Guide