我有键盘预制件和6个按键预制件。我把键放在Instantiate上。每个键都有TextMeshPro对象。我需要使用脚本更改这些字符。但是,当我尝试获取TextMeshPro组件时,控制台将返回null。
Debug.Log(GetComponent<TextMeshPro>());
我的代码部分在这里;
private void SetUp()
{
// Global Screen Height
var worldScreenHeight = Camera.main.orthographicSize * 2.0;
// Global Screen Width
var worldScreenWidth = worldScreenHeight / Screen.height * Screen.width;
// Keyboard
var keyboardSize = GetComponent<SpriteRenderer>().sprite.bounds.size.x;
var neededScale = worldScreenWidth / keyboardSize;
var neededYPosition = (worldScreenHeight / 2) - (keyboardSize / 2);
transform.localScale = new Vector3((float)neededScale, (float)neededScale, 0);
transform.position = new Vector3(0, -(float)neededYPosition, 0);
for (int i = 0; i < Characters.Length; i++)
{
double angle = ((i / (Characters.Length / (double)2)) * Math.PI);
GameObject newObject = Instantiate(Keys, new Vector2((float)(keyboardSize / 2 * 0.6 * neededScale * Math.Cos(angle)), (float)(keyboardSize / 2 * 0.6 * neededScale * Math.Sin(angle) - neededYPosition)), Quaternion.identity, transform) as GameObject;
newObject.transform.localScale = new Vector3(0.5f, 0.5f, 0);
// I can't access textmeshpro
Debug.Log(GetComponent<TextMeshPro>());
}
}
如何在TextMeshPro中更改字符串?
将GetComponent<TextMeshPro>()
替换为GetComponent<TextMeshProUGUI>()
。
有两个都来自TMP_Text的TextMeshPro组件。
类型为[[TextMeshPro的第一个组件,旨在替代使用MeshRenderer的旧TextMesh。
类型TextMeshProUGUI]的第二个对象旨在替代UI.Text,并旨在与CanvasRenderer和Canvas系统一起使用,这似乎是您正在使用的系统。