废话不多说 说一下解决思路
第一:字体库包含多音字 但是因为文档只支持一种字体。所以就是将要变换的多音字转化为空格(不显示),将多音字放置到空格位置上(修改字体)
第二:一句中可能有多种字体 如 的 de di 长 chang zhang de 是第二种字体 chang 是第三种字体才有,所以多音字要可以多次定义字体,即相同字体的要可以公用一个字体
第三:这样会出现很多子Text 更改文档内容的时候要清空所有text
第一个方法 将文档中这个字替换掉 这里不能用空格 要用\u3000 代替 否者空格会被换行符代替
/// <summary> /// 替换除了这个字的所有别的文字为空格 /// </summary> /// <param name="text"></param> /// <param name="charPoint"></param> /// <returns></returns> private string ChangeExceptThisCharAddEmptyText(string text, char charPoint) { string[] strings = text.Split(charPoint); string ft_text = null; for (int i = 0; i < strings.Length; i++) { if (i != 0) { ft_text += charPoint; } for (int a = 0; a < strings[i].Length; a++) { ft_text = ft_text + "\u3000"; } } return ft_text; }
第二个方法 替换这个字为空格
/// <summary> /// 替换这个字为空格 /// </summary> /// <param name="text"></param> /// <param name="charPoint"></param> /// <returns></returns> private string ChangeThisCharAddEmptyText(string text, char charPoint) { string[] strings = text.Split(charPoint); string ft_text = null; for (int i = 0; i < strings.Length; i++) { //Debug.Log(strings[i]); ft_text = ft_text + strings[i] + "\u3000"; } return ft_text; }
第三个方法 将所有多音字注入文档
/// <summary> /// 迭代修改 将多音字 字节组全部修改一遍 /// </summary> /// <param name="text"></param> /// <param name="fontIndex"></param> /// <param name="charList"></param> /// <param name="textContent"></param> private void ChangeTextPinYinCharListEmptyApi(Text text, int fontIndex, char[] charList, string textContent) { string ft_text = textContent; for (int i = 0; i < charList.Length; i++) { ft_text = ChangeThisCharAddEmptyText(ft_text, charList[i]); } text.text = ft_text; }
第四个方法 清空
/// <summary> /// 清理索引值为1以后的子物体 /// </summary> /// <param name="text"></param> public void ClearPinYinText(Text text) { for (int i =0; i < text.gameObject.transform.childCount; i++) { Destroy(text.transform.GetChild(i).gameObject); } text.text = null; }
第五个方法 修改多音字总方法
private void ChangeTextPinYinExceptThisCharEmptyApi(Text text, int fontIndex, char charPoint, string textContent) {
//获取清空的字符串 string ft_text = ChangeExceptThisCharAddEmptyText(textContent, charPoint);
//创建新的text Text newText = Instantiate(text, text.transform.localPosition, text.transform.localRotation) as Text; for (int i = 0; i < newText.transform.childCount; i++) { Destroy(newText.transform.GetChild(i).gameObject); }
//重置childtext参数 newText.transform.SetParent(text.transform); newText.transform.localPosition = Vector3.zero; newText.transform.localScale = Vector3.one; newText.font = fontList[fontIndex]; newText.text = ft_text; }
最后就可以设置自己要更换的字体及文字了
测试例子
public void SetTextTextContentApi(Text text, string textContent,bool isClearAll) { if (isClearAll) { ClearAllPinYinText(text);} else {ClearPinYinText(text); }; ChangeTextPinYinCharListEmptyApi(text, 0, new char[4] { '长', '的', '呢','了' }, textContent); ChangeTextPinYinExceptThisCharEmptyApi(text, 1, '长', textContent); ChangeTextPinYinExceptThisCharEmptyApi(text, 2, '的', textContent); ChangeTextPinYinExceptThisCharEmptyApi(text, 1, '呢', textContent); ChangeTextPinYinExceptThisCharEmptyApi(text, 1, '了', textContent); }