美文网首页
Text文本超框检查

Text文本超框检查

作者: 忆中异 | 来源:发表于2023-03-07 15:16 被阅读0次

目的

为提高多语言适配调整效率,解决语言策划文本逐一检查工作量,提前判断各种语言是否超框。

思路

通过对Hierarchy中Text的遍历,对比显示文本和实际文本的长度判断是否超框,BestFit也适用。

使用

1.在Hierarchy(运行状态下亦可)选择需要检查的对象,UI工具/检查超框文本。

image.png

2.在Assets选择需要检查的对象或文件夹,UI工具/检查超框文本。

image.png

如果出现“实际显示次数:-1”字样,请再执行一次。

输出结果暂时会显示在控制台中。

代码

部分相关代码如下

/// <summary>
        /// 遍历对象中的Text
        /// </summary>
        /// <param name="obj"></param>
        private static void CheckOutText(GameObject obj)
        {
            Text[] txts = obj.GetComponentsInChildren<Text>();
            for(int i = 0; i < txts.Length; i++)
            {
                Text textComp = txts[i];
                string namePath = textComp.transform.name;
                GetObjPath(textComp.gameObject, obj, ref namePath);
                if (string.IsNullOrEmpty(textComp.text))
                {
                    Debug.LogError($"{namePath} 文本为空");
                    continue;
                }
                int visibleLen = textComp.cachedTextGenerator.characterCountVisible;
                int len = textComp.text.Length;
                TextGenerationSettings settings = textComp.GetGenerationSettings(textComp.rectTransform.rect.size);
                bool withError = textComp.cachedTextGenerator.PopulateWithErrors(textComp.text, settings, textComp.gameObject);
                //Debug.LogError($"withError:{withError},setfont:{settings.fontSize}");
                if (visibleLen < len)
                    Debug.LogError($"{namePath} 超框,实际显示字数:{visibleLen},原有字数:{len},文本:{textComp.text}");

                
            }
        }

相关文章

网友评论

      本文标题:Text文本超框检查

      本文链接:https://www.haomeiwen.com/subject/adhbldtx.html