目的
为提高多语言适配调整效率,解决语言策划文本逐一检查工作量,提前判断各种语言是否超框。
思路
通过对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}");
}
}












网友评论