c# - Unity 4.6+ 通过脚本创建文本

标签 c# user-interface unityscript unity3d

我正在尝试动态创建文本,以便根据玩家数量改变 UI。然而,正如我目前编写的那样,对象已创建,并且除了文本在屏幕或场景 View 中不可见之外,一切似乎都正常工作。对象在那里,而不是文本。谁能看到这个问题或知道如何解决这个问题?

顺便说一句,我正在使用 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));
    }

最佳答案

你能检查文本是否显示在编辑器的文本组件中吗?一旦你点击播放,“WORK”就会出现在那里。 另外,控制台有没有报错?

关于c# - Unity 4.6+ 通过脚本创建文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34734207/

相关文章:

c# - Web GUI 设计问题

animation - 通过脚本更改动画 Controller

c# - mysql语法错误?

jquery - jquery 基本对话框小部件中的自定义样式

java - 可显示的 Toast 数量有限制吗?

android - Unity Android 触摸无法按预期工作

animation - Unity 2D Animator,平台 2d 角色的参数和过渡困惑

c# - 进程开始时间

c# - C# 中泛型参数的空值或默认比较

C# OAuth OWIN : return custom response when invalid credentials provided to Token endpoint?