2D文本需要Canvas和EventSystem,最好使用Editor来添加;
动态显示一个文本,采用3D Text的方式:
GameObject text = new GameObject(); text.transform.position = Camera.main.transform.position + Camera.main.transform.forward * 5.0f; //text.transform.position = Vector3.zero; text.AddComponent<TextMesh>(); var style = text.GetComponent<TextMesh>(); style.text = "3D Text"; style.fontSize = 50; //显示大小与fontSize、characterSize、距离有关 style.characterSize = 0.05f; style.anchor = TextAnchor.MiddleCenter;
TextMesh结构(属性)如下: