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

Unity安卓构建异常:场景加载失败与TMP文本丢失

环境
  • Unity 6000.4.9f1
问题描述

游戏在Unity编辑器中运行正常,但安卓构建后出现两类异常:

问题1:场景加载失败

场景流程:

加载场景(启动)
    ↓
主菜单
    ↓
加载界面
    ↓
游戏场景

编辑器表现:

  • 所有场景加载正常
  • 异步加载功能正常

安卓构建版本表现:

  • 场景切换失败
  • 加载界面显示后无法进入目标场景
  • 编辑器无报错信息
  • 加载场景中的文本不显示

问题2:TextMeshPro(TMP)文本丢失

编辑器中TMP文本显示正常,构建后:

  • TMP文本全部消失
  • 部分UI元素存在,但TMP文本未渲染
  • 重新导入TMP资源无法解决问题
场景加载脚本
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
    public static int sceneToLoadIndex;

    // =====================================================
    // 按名称加载
    // =====================================================

    public static void Load(string sceneName)
    {
        Scene targetScene =
            SceneManager.GetSceneByName(
                sceneName
            );

        // 手动查找构建索引
        for (int i = 0;
             i < SceneManager.sceneCountInBuildSettings;
             i++)
        {
            string path =
                SceneUtility.GetScenePathByBuildIndex(i);

            string name =
                System.IO.Path.GetFileNameWithoutExtension(path);

            if (name == sceneName)
            {
                sceneToLoadIndex = i;
                break;
            }
        }

        SceneManager.LoadScene("LoadingScene");
    }

    // =====================================================
    // 按索引加载
    // =====================================================

    public static void Load(int sceneIndex)
    {
        sceneToLoadIndex = sceneIndex;
        SceneManager.LoadScene("LoadingScene");
    }
}
加载界面脚本
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using TMPro;
using System.Collections;

public class LoadingScreen : MonoBehaviour
{
    public Slider progressBar;
    public TMP_Text progressText;

    private void Start()
    {
        StartCoroutine(LoadAsync());
    }

    IEnumerator LoadAsync()
    {
        AsyncOperation operation =
            SceneManager.LoadSceneAsync(
                SceneLoader.sceneToLoadIndex
            );

        operation.allowSceneActivation = false;

        while (!operation.isDone)
        {
            float progress =
                Mathf.Clamp01(operation.progress / 0.9f);

            progressBar.value = progress;

            if (progressText != null)
            {
                progressText.text =
                    (progress * 100f).ToString("0") + "%";
            }

            if (operation.progress >= 0.9f)
            {
                operation.allowSceneActivation = true;
            }

            yield return null;
        }
    }
}
已尝试的排查手段

场景加载相关

  • 确认所有场景已添加至:File → Build Settings → Scenes In Build
  • 确认场景名称完全匹配
  • 测试按名称和索引两种加载方式
  • 调整加载场景在Build Settings中的顺序(仅第一个场景可运行,切换场景失败)
  • 确认编辑器中场景流程正常
  • 清除编译错误后重新构建

TMP相关

  • 导入TMP Essentials
  • 重新导入所有TMP资源
  • 检查TMP组件引用有效字体
  • 确认编辑器中TMP文本显示正常
  • 重新导入TMP资源后重新构建
  • 删除Library文件夹后重复上述步骤
额外信息

构建过程中出现以下警告:

Shader warning in 'ConvGeneric':
"TRANSPOSE_OUTPUT variants not supported with NON_UNIFORM_CONVGROUP_PER_OC"

该警告来自:

Packages/com.unity.ai.inference/

此外存在缺失的TMP资源,每次重新导入后仍未恢复,编辑器中游戏运行正常。

调试建议与解决方案

针对场景加载失败问题

  1. 修复静态变量传递问题:SceneLoader.sceneToLoadIndex作为静态变量,场景切换时可能因Unity的场景卸载逻辑重置为空值。改用PlayerPrefs传递目标索引:
    修改SceneLoader.Load方法:

    PlayerPrefs.SetInt("TargetSceneIndex", sceneToLoadIndex);
    SceneManager.LoadScene("LoadingScene");
    

    在LoadingScreen.LoadAsync中读取:

    int targetIndex = PlayerPrefs.GetInt("TargetSceneIndex");
    AsyncOperation operation = SceneManager.LoadSceneAsync(targetIndex);
    
  2. 优化异步加载逻辑:部分安卓设备可能因资源加载延迟,导致进度未完全就绪就触发场景激活。添加帧等待后再激活:

    if (operation.progress >= 0.9f)
    {
        yield return new WaitForEndOfFrame();
        operation.allowSceneActivation = true;
    }
    
  3. 获取设备运行日志:通过adb logcat -s Unity命令查看安卓设备上的实时日志,定位场景加载失败的具体报错(编辑器无报错不代表设备无报错)。

针对TMP文本丢失问题

  1. 重建TMP资源:

    • 打开Window → TextMeshPro → TMP Settings,点击Regenerate Font Atlas重新生成字体图集
    • 删除Library/com.unity.textmeshpro/文件夹,重启项目让Unity重新生成TMP相关资源
  2. 检查字体打包配置:

    • 选中TMP使用的字体文件,在Inspector面板勾选Include in Build
    • 确认字体的Character Set包含所有需要显示的字符(如中文、特殊符号),避免构建时被裁剪
  3. 标记TMP资源为必包项:在TMP字体资源的Asset Labels中添加Always Included,确保构建时被打包进APK

关于构建警告

该警告来自AI推理包,与当前场景加载、TMP问题无关,可忽略;也可尝试更新com.unity.ai.inference包到最新版本消除警告。

内容的提问来源于stack exchange,提问作者Md Labib Alam

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 20:35:07