2
我正在尝试动态创建文本,以便根据玩家的数量改变UI。然而,正如我在此刻编写的那样,创建了对象,并且除了文本以外,一切似乎都在屏幕上或场景视图中不可见。对象在那里,而不是文本。任何人都可以看到这个问题或知道如何解决这个问题?Unity 4.6+通过脚本创建文本
我使用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));
}
是的,文本在检查器中正确显示,控制台中没有错误。 – gn12345
奇怪。您是否使用1f的alpha设置了“颜色”数组的颜色? –
Omg,非常感谢。我知道这很简单,因为我看不到任何问题。非常感谢你,我几个小时一直在砖墙上撞我的头(现在看起来令人尴尬)。 – gn12345