简体   繁体   English

Unity 4.6+通过脚本创建文本

[英]Unity 4.6+ Creating Text through a Script

I am trying to create text dynamically so that depending on the number of players, the UI changes. 我正在尝试动态创建文本,以便根据玩家的数量,UI更改。 However as I've coded it at the moment, the objects are created and everything seems to work apart from the text is not visible on the screen or in scene view. 然而,由于我现在编码它,所以创建了对象,并且在屏幕或场景视图中看不到文本之外的所有内容都是不可见的。 The object is there, just not the text. 对象就在那里,而不是文本。 Can anyone see the issue or know how to resolve this? 任何人都可以看到问题或知道如何解决这个问题?

I'm using C# btw. 我正在使用C#btw。

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));
    }

Can you check if the text is shown in the text component within the editor? 您可以检查文本是否显示在编辑器中的文本组件中? As soon as you hit play, "WORK" should appear there. 一旦你上场,“工作”应该出现在那里。 Also, is there any error in the console? 此外,控制台中是否有任何错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM