Unity 4.6+通过脚本创建文本
本文关键字:脚本 创建 文本 Unity | 更新日期: 2023-09-27 18:29:50
我正在尝试动态创建文本,以便根据玩家的数量更改UI。然而,正如我目前所编码的那样,对象已经创建,除了文本之外,其他一切似乎都在工作,在屏幕或场景视图中都不可见。对象在那里,只是没有文本。有人能看到这个问题或知道如何解决这个问题吗?
我正在使用C#顺便说一句
public class UserInterface : MonoBehaviour {
public Canvas UI;
public GameObject DamageCounter;
public Color[] colors = new Color[4];
private static List<GameObject> players;
Text uiText;
RectTransform uiPos;
// Use this for initialization
void Start () {
players = PlayerManager.getPlayers ();
float screenHalf = Screen.width / 2;
float screenDiv = Screen.width / players.Count;
Debug.Log ("ScreenDiv = " + screenDiv);
for (int i = 1; i < players.Count + 1; i++)
{
GameObject playerText = new GameObject("Text");
playerText.layer = 5;
uiPos = playerText.AddComponent<RectTransform>();
uiText = playerText.AddComponent<Text>();
playerText.transform.SetParent(UI.transform, false);
uiPos.sizeDelta.Set(Screen.width, Screen.height);
uiPos.anchoredPosition3D = new Vector3(0, 0, 0);
uiPos.anchoredPosition = new Vector2(10, 10);
uiPos.localScale = new Vector3(1.0f, 1.0f, 1.0f);
uiPos.localPosition.Set(0, 0, 0);
uiText.supportRichText = true;
uiText.fontSize = 150;
uiText.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
uiText.alignment = TextAnchor.MiddleCenter;
uiText.horizontalOverflow = HorizontalWrapMode.Overflow;
uiText.verticalOverflow = VerticalWrapMode.Overflow;
uiText.color = colors[i - 1];
uiText.text = "WORK";
Debug.Log ("HERE:" + (i * screenDiv - screenHalf));
}
您能检查编辑器中的文本组件中是否显示了文本吗?一旦你点击play,"WORK"就会出现在那里。此外,控制台中是否有任何错误?