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

Unity+TextMeshPro构建报错:无法初始化宽为负/零的非默认纹理

Unity TextMeshPro构建报错修复建议

问题背景

  • 使用环境:Unity 2023.1.17f1 + TextMeshPro 3.0.6
  • 异常现象:编辑器Play模式运行正常,使用Ctrl+B构建并运行时触发报错
  • 已尝试无效操作:重新导入TextMeshPro资源、清除TMP字体缓存、检查包更新、验证TMP字体尺寸有效性

报错信息

Cannot initialize non-default texture with negative or zero width.
UnityEngine.Texture2D:Resize (int,int,UnityEngine.TextureFormat,bool)
TMPro.TMP_FontAsset:ClearAtlasTextures (bool) (at ./Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMP_FontAsset.cs:2846)
TMPro.TMP_FontAsset:ClearFontAssetDataInternal () (at ./Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Runtime/TMP_FontAsset.cs:2726)
TMPro.TMP_PreBuildProcessor:OnPreprocessBuild (UnityEditor.Build.Reporting.BuildReport) (at ./Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Editor/TMP_PreBuildProcessor.cs:26)
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun ()

Error building Player: Cannot initialize non-default texture with negative or zero width.

Build completed with a result of 'Failed' in 0 seconds (68 ms)
2 errors
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun ()

修复方案

1. 检查自定义TMP字体资产的图集参数

  • 打开所有自定义TMP Font Asset,查看Atlas Settings面板中的Width/Height数值,确保为正整数(推荐使用2的幂数,如256、512、1024)
  • 若发现数值异常,重置为默认值后点击Inspector中的Generate Font Atlas按钮重新生成图集

2. 排查损坏的TMP字体资产

  • 在Project窗口中过滤TMP Font Asset类型,逐个检查:
    • 查看字体预览是否正常,若预览空白或显示异常,标记为可疑资产
    • 将可疑资产替换为官方默认TMP字体(如Liberation Sans)后重新尝试构建
  • 若项目中存在动态生成TMP字体的代码,检查是否有错误设置纹理尺寸的逻辑

3. 临时禁用TMP预构建处理器

  • 找到路径Library/PackageCache/com.unity.textmeshpro@3.0.6/Scripts/Editor/TMP_PreBuildProcessor.cs
  • 注释掉文件顶部的[InitializeOnLoad]和[PreBuildCallback]特性,或临时清空文件内容(操作前请备份原文件)
  • 构建完成后建议恢复原文件,避免引发其他潜在问题

4. 清理Unity缓存与构建残留

  • 关闭Unity编辑器,删除项目根目录下的Library、Temp、Obj文件夹
  • 重新打开Unity,等待资源重新导入完成后再次尝试构建

5. 调整TextMeshPro版本

  • 通过Package Manager尝试升级TextMeshPro到3.0.7及以上版本,或降级到3.0.5版本,排查版本兼容性问题

内容的提问来源于stack exchange,提问作者Teoman shipahi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.02 23:42:35