【问题标题】:Unity 4.6+ Creating Text through a ScriptUnity 4.6+ 通过脚本创建文本
【发布时间】:2016-01-12 01:36:24
【问题描述】:

我正在尝试动态创建文本,以便根据玩家的数量,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));
    }

【问题讨论】:

    标签: c# user-interface unity3d unityscript unity5


    【解决方案1】:

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

    【讨论】:

    • 是的,文本在检查器中正确显示,控制台中没有错误。
    • 很奇怪。您是否设置了 alpha 为 1f 的“colors”数组中的颜色?
    • 天哪,非常感谢。我知道这很简单,因为我看不到任何问题。非常感谢你,我已经把头撞在砖墙上好几个小时了(现在看起来很尴尬)。
    • 这种事情大家都会遇到!哈哈祝你的游戏好运!
    猜你喜欢
    • 2022-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2022-10-21
    • 2011-06-20
    • 2015-10-13
    相关资源
    最近更新 更多