C# 为什么我不';运行游戏时,在文本组件上看不到任何文本?
我想看看“Hello World”文本。但是什么都没有 如果使用C# 为什么我不';运行游戏时,在文本组件上看不到任何文本?,c#,unity3d,C#,Unity3d,我想看看“Hello World”文本。但是什么都没有 如果使用3D对象>3D文本 或UI>Text 或UI>Text-TextMeshPro 无论我使用的是什么文本组件,因为没有网格渲染器连接到它,所以我得到了一个例外,所以我要先向文本组件添加一个网格渲染器 但是我得到了一个例外,说没有TextMesh组件附加到新创建的文本上,所以我更改了这一行: var textmesh = newText.GetComponent<TextMesh>(); var textmesh=newT
3D对象>3D文本
或UI>Text
或UI>Text-TextMeshPro
无论我使用的是什么文本组件,因为没有网格渲染器连接到它,所以我得到了一个例外,所以我要先向文本组件添加一个网格渲染器
但是我得到了一个例外,说没有TextMesh组件附加到新创建的文本上,所以我更改了这一行:
var textmesh = newText.GetComponent<TextMesh>();
var textmesh=newText.GetComponent();
到
var textmesh=newText.GetComponent();
但它仍然不起作用,比如说textmesh为null
因此,我将文本组件更改为UI>text
将MeshRenderer添加到文本中,现在不会出现异常/错误:
及
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
公共类AddTextToObject:MonoBehavior
{
公共UnityEngine.GameObject[]对象编号;
公共UnityEngine.GameObject文本;
公开发行股票;
公共bool rotateEnumbers=false;
公共浮子旋转速度=10f;
public bool textChild=false;
public bool textAbove=false;
public bool textinfort=false;
public bool textOnFaces=false;
私有列表newTexts=新列表();
私有网格渲染器;
私有向量3 newPos;
私有void Start()
{
renderer=新网格渲染器[objectsToNumber.Length];
for(int i=0;i
更新:
正在尝试使用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);
}
}
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用UnityEngine.UI;
使用TMPro;
公共类AddTextToObject:MonoBehavior
{
公共UnityEngine.GameObject[]对象编号;
公共UnityEngine.GameObject文本;
公开发行股票;
公共bool rotateEnumbers=false;
公共浮子旋转速度=10f;
public bool textChild=false;
public bool textAbove=false;
public bool textinfort=false;
public bool textOnFaces=false;
私有列表newTexts=新列表();
私有网格渲染器;
私有向量3 newPos;
私有void Start()
{
renderer=新网格渲染器[objectsToNumber.Length];
for(int i=0;i
但结果是文本与游戏对象不太接近。
文本“Hello World”应该像棍子一样贴在每个g上
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);
}
}
}
}
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);
}
}
}
}