【问题标题】:Why I don't see any text on the text component when running the game?为什么我在运行游戏时在文本组件上看不到任何文本?
【发布时间】:2019-12-30 05:35:15
【问题描述】:

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

如果使用3D Object > 3D TextUI > TextUI > Text - TextMeshPro

无论我使用什么文本组件,因为没有附加网格渲染器,所以我会遇到异常,所以我首先向文本组件添加一个网格渲染器。

但是我收到异常说没有附加到新创建的文本的 TextMesh 组件,所以我正在更改这一行:

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

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

但它仍然无法正常工作得到异常说 textmesh 为空。

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

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”应该类似于粘贴/附加到每个游戏对象。

我想要做的是将文本“Hello World”粘贴/附加到 3 个新游戏、选项、退出的每个立方体的正面:

【问题讨论】:

    标签: c# unity3d


    【解决方案1】:

    因为它不是Canvas的孩子

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

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


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

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

    所以TextMeshPro 的“正确”替换不是Text,而是TextMesh。 (不过我不会朝那个方向发展 .. 如果您已经使用 TMP 的话.. 它比旧版 UI TextTextMesh 更好。


    您也不应该手动向对象添加MeshRendererTextTextMeshProUGUI 等 UI 元素自带渲染器。也许你应该通过Unity UI Tutorial 一次。

    还可以查看TextMeshPro Guide

    【讨论】:

    • 仍然无法正常工作。我用我的尝试和我的目标更新了我的问题。主要目标是将文本粘贴/附加/放置在 3 个游戏对象 New Game、Options、Quit 的正面。在这种情况下,这 3 个对象是立方体。
    • 现在你正在使用TextMeshProUGUI,这可能正是你不想要的......因为现在你需要一个 WorldSpace Canvas 来制作它们可见.. 我想目前你有一个ScreenSpace Overlay 画布?
    • 对,Canvas 设置为 Screen Space - Overlay
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-06
    • 2012-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多