You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Unity3D中GUI.Label在Android端不显示问题求助

Android端GUI.Label文本不显示问题分析与修复

咱们来一步步排查你遇到的问题——Unity模拟器正常显示但Android端看不到Welcome!文本,结合你的代码来看,主要有这几个核心问题和对应的解决办法:

1. 字体加载逻辑的顺序错误

你在代码里先创建了TextStyle,之后才加载字体并设置GUI.skin.font,但TextStyle是基于创建时的皮肤状态生成的,所以后续的字体设置不会同步到TextStyle上。如果Android端字体加载失败(或者没生效),文本就可能用了不可见的默认字体。

修复方式:

  • 先加载字体,再创建GUIStyle;
  • 或者直接给TextStyle.font赋值,跳过修改全局皮肤的步骤。

同时要确保字体资源的路径正确:必须放在Assets/Resources/font/目录下,文件名是stingray(带对应后缀比如.ttf),并且在字体的导入设置中,Android平台是启用状态(Inspector面板里的Platform Settings要勾选Android)。

2. 固定Rect导致的布局适配问题

你用GUI.Label(new Rect(50,50,300,70), "Welcome!", TextStyle)用了固定的像素值,在不同分辨率的Android设备上,这个区域可能超出屏幕可视范围,或者被其他元素遮挡。

修复方式:

  • 改用相对屏幕比例计算Rect,比如:
    float labelX = Screen.width * 0.1f; // 屏幕宽度的10%位置
    float labelY = Screen.height * 0.2f; // 屏幕高度的20%位置
    float labelWidth = Screen.width * 0.6f;
    float labelHeight = Screen.height * 0.1f;
    GUI.Label(new Rect(labelX, labelY, labelWidth, labelHeight), "Welcome!", TextStyle);
    
  • 或者换成GUILayout.Label,配合GUILayout的自动布局系统,更适配不同屏幕:
    GUILayout.Label("Welcome!", TextStyle);
    

3. OnGUI内重复创建GUIStyle的性能与渲染问题

你在OnGUI()方法里每次都new GUIStyle(GUI.skin.label),这不仅会导致性能浪费,还可能在Android平台触发渲染异常。

修复方式:
把TextStyle改成类的成员变量,在Awake()或Start()中初始化:

public class MainMenu : MonoBehaviour {
    public GUISkin Skin;
    public Texture btnTexture;
    private GUIStyle TextStyle; // 成员变量

    public void Awake() {
        // 提前初始化GUIStyle
        TextStyle = new GUIStyle(GUI.skin.label);
        TextStyle.fontSize = 50;
        TextStyle.normal.textColor = Color.yellow;
        TextStyle.hover.textColor = Color.yellow;
        
        // 加载字体
        Font myFont = (Font)Resources.Load("font/stingray", typeof(Font));
        if (myFont != null) {
            TextStyle.font = myFont;
        } else {
            Debug.LogWarning("Failed to load font! Using default font.");
        }
    }

    public void OnGUI() {
        if (this.Skin != null) {
            GUI.skin = this.Skin;
        }
        GUI.skin.button.fontSize = 30;

        Rect content = new Rect(30, 100, Screen.width-60, Screen.height-200);
        GUILayout.BeginArea(content);
        
        GUILayout.BeginHorizontal();
        // 使用提前初始化的TextStyle
        GUI.Label(new Rect(Screen.width*0.1f, Screen.height*0.2f, Screen.width*0.6f, Screen.height*0.1f), "Welcome!", TextStyle);
        // 或者用GUILayout.Label
        // GUILayout.Label("Welcome!", TextStyle);
        GUILayout.EndHorizontal();

        GUILayout.BeginHorizontal();
        GUILayout.FlexibleSpace();
        if (GUILayout.Button("Join", GUILayout.Width(300), GUILayout.Height(80))) {
            SceneManager.LoadSceneAsync("SingleMenu");
        }
        GUILayout.FlexibleSpace();
        GUILayout.EndHorizontal();
        
        GUILayout.EndArea();
    }

    void Start () { }
    void Update () { }
}

4. 额外的检查项

  • 查看Android LogCat日志,确认字体是否加载成功:添加Debug.Log("Font loaded: " + (myFont != null));,如果显示false,说明资源路径或导入设置有问题;
  • 在Unity Player Settings中,把Android的UI Scale Mode设置为Scale With Screen Size,确保UI适配不同屏幕比例;
  • 检查文本颜色:虽然你设置了黄色,但如果Android端的背景色也是黄色,文本会被遮挡,可以临时改成红色测试。

内容的提问来源于stack exchange,提问作者Virasack Viravong

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:53:22