Unity Text Ui在Android设备上看起来很小

时间:2017-08-26 16:48:43

标签: c# unity3d unity5 unityscript unity2d

我是团结的新手,我开发了一款简单的2D游戏。 在记分牌现场,我设法保存分数并将其显示在滚动视图上。当我在团结中运行它它工作正常,但当我在我的Android手机中构建和运行时,scrollview看起来有点大,文本ui(由脚本添加)看起来非常小。 以下是在scrollview中显示内容游戏对象中得分的代码:

void Start () {
        if (PlayerPrefs.HasKey (0 + "HScore")) {

            float y = -30;

            for (int i = 0; i < 10; i++) {
                if (PlayerPrefs.GetInt (i + "HScore") == 0) {
                    break;
                }
                GameObject textobj = new GameObject (i + "HScoreName", typeof(RectTransform));
                GameObject textobj2 = new GameObject (i + "HScore", typeof(RectTransform));

                Text name = textobj.AddComponent<Text> ();
                Text score = textobj2.AddComponent<Text> ();

                GameObject lineObj = new GameObject ("Line", typeof(RectTransform));
                Image l = lineObj.AddComponent<Image> ();
                l.color = Color.white;
                lineObj.transform.localScale = new Vector3 (500, 0.01f, 1);

                name.text = "#" + (i + 1) + "- " + PlayerPrefs.GetString (i + "HScoreName");
                score.text = PlayerPrefs.GetInt (i + "HScore").ToString ();

                name.color = Color.white;
                score.color = Color.white;

                name.alignment = TextAnchor.MiddleLeft;
                score.alignment = TextAnchor.MiddleLeft;

                name.horizontalOverflow = HorizontalWrapMode.Overflow;

                name.font = Resources.GetBuiltinResource<Font> ("Arial.ttf");
                score.font = Resources.GetBuiltinResource<Font> ("Arial.ttf");

                name.fontSize = 15; 
                score.fontSize = 15; 

                score.fontStyle = FontStyle.Bold;

                textobj.transform.position = content.transform.position + new Vector3 (70, y, 0);
                textobj.transform.SetParent (content.transform);

                textobj2.transform.position = content.transform.position + new Vector3 (180, y, 0);
                textobj2.transform.SetParent (content.transform);

                lineObj.transform.position = content.transform.position + new Vector3 (60, y - 25, 0);
                lineObj.transform.SetParent (content.transform);

                y = y - 50;
            }
        }
    }

此脚本中是否缺少任何文本以保持文本适合屏幕?

2 个答案:

答案 0 :(得分:1)

您正在使用平面数字,您需要的是Screen.width和Screen.height的百分比

例如,如果您在分辨率为150x150的手机中运行,如果您希望它在第5位,第5位,那么您可以写这个

transform.position =(5/100)* 150,它会将你的对象从左下角设置为5 PERCENT。

答案 1 :(得分:0)

Canvas上有一个组件 - Canvas Scaler。尝试更改UI缩放模式 - 使用屏幕大小缩放 - 它必须位于画布上。而且你也可以玩数学价值 Try to change UI Scale Mode - Scale with Screen Size - it must be on the Canvas.