Unity的动态字库字体超出字库图片尺寸导致字体花屏
来源:互联网 发布:odysseusota4 windows 编辑:程序博客网 时间:2024/06/02 17:07
转载自:http://www.cnblogs.com/bicker/p/3669176.html
在我们游戏的开发过程中,在部分手机上运行游戏的时候,出现了字体丢失的问题,出问题的手机似乎用的都是高通芯片。
使用的unity是4.2.0版本,ngui是3.4.9版本。
在unity的论坛及unity answer中寻找问题的原因及解决办法许久未果,后来在csdn里偶然找到一篇博文,里面讲到的问题出现的原因以及相应的解决办法。根据文中的办法,我试验后证实的确可以解决问题。
博文地址:http://blog.csdn.net/langresser_king/article/details/22095235
在unity editor中可以看到,使用的字体ttf文件下,会生成一个texture文件及material文件。当需要显示文字的时候,会通过RequestCharactersInTexture函数向Font请求更新文字信息,然后使用GetCharacterInfo获取文字信息来渲 染。在调用GetCharacterInfo的时候要保证所有文字都通过RequestCharactersInTexture请求过了。
如果请求的时候,Font内部维护的texture不够用了,就会触发textureRebuildCallback的回调,通知外部使用Font的对象,其内部的texture被更新了,外部应该重新刷新。
原文中提到,是因为texture的大小没有被及时扩大而导致字体丢失。但是在我多次测试之后,猜测问题的原因应该不是这一点。因为在PC上或者其他手机上并不会出现字体丢失的问题,问题并不是出现在unity动态字体的策略上。但是这个问题是可以通过在最开始的时候使用方法将texture尺寸扩大的做法来规避掉。真正的原因实在未能找出。
下面提供规避掉这个问题的做法,供参考:
using UnityEngine;using System.Collections;public class ExpandDynamicFontTexture : MonoBehaviour {public UnityEngine.Font baseFont;private static string chineseText = null;private static int flag = 1;void Start(){if( flag != 1 ) {return;}flag++;ExpandFontTexture();}public void ExpandFontTexture(){if (chineseText == null) {TextAsset text = Resources.Load( "Fonts/configText" ) as TextAsset;chineseText = text.ToString();}baseFont.RequestCharactersInTexture( chineseText, 30, FontStyle.Normal );//baseFont.material.mainTexture.height = 1024;//baseFont.material.mainTexture.width = 1024;Texture texture = baseFont.material.mainTexture;Debug.Log( string.Format( "Texture:{0},{1}", texture.width,texture.height ));}}
这个可以在游戏开始的时候调用一次,通过RequestCharactersInTexture函数,传入一个较大的字符串来使得unity扩大texture的大小。
另外如果游戏里所使用到的文字都是通过配置表来加入的,附送一个函数,来统计游戏中所有用到的文字,并生成上一函数需要的文字文本文件。
using UnityEngine;using UnityEditor;using System.IO;using System.Text;using System.Linq;using System.Collections;using System.Collections.Generic;public class CollectWords : MonoBehaviour {[MenuItem("Custom/Statistic Words")]public static void StatisticWords(){//用于统计字符Dictionary<char, int> wordMap = new Dictionary<char, int>();string[] txtFiles = Directory.GetFiles("Assets/Resources/Localization/", "*.txt");for (int i = 0; i < txtFiles.Length; ++i){Debug.Log("> open file: " + txtFiles[i]);string textContent = File.ReadAllText(txtFiles[i], System.Text.Encoding.UTF8);Debug.Log("> file " + txtFiles[i] + " size: " + textContent.Length);for (int wordIndex = 0; wordIndex < textContent.Length; ++wordIndex){int useCount = 0;wordMap.TryGetValue(textContent[wordIndex], out useCount);wordMap[textContent[wordIndex]] = useCount + 1;}}Dictionary<char, int> sortedWordMap = new Dictionary<char, int>();sortedWordMap = (from entry in wordMap orderby entry.Value descending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);IEnumerator enumerator = sortedWordMap.Keys.GetEnumerator();StringBuilder sBuilder = new StringBuilder();while (enumerator.MoveNext()){sBuilder.Append(string.Format("{0}:\t{1}\n", enumerator.Current, sortedWordMap[(char)enumerator.Current]));}File.WriteAllText("Assets/Resources/Localization/sortedChineseWords.txt", sBuilder.ToString(), Encoding.UTF8);sBuilder = new StringBuilder();enumerator = sortedWordMap.Keys.GetEnumerator();int wordCount = 0;while (enumerator.MoveNext()&&wordCount<800){wordCount++;sBuilder.Append(enumerator.Current);}Debug.Log("> Total word: " + sBuilder.ToString().Length);File.WriteAllText("Assets/Resources/Localization/chineseWords.txt", sBuilder.ToString(), Encoding.UTF8);}}
如果有知道unity这个丢失字体真正原因的,欢迎告知。
ps: 上文中从Resources目录里取得文本文件及font文件,用的是我们自己实现的ResMngr,可自行更换成unity原来的方法
- Unity的动态字库字体超出字库图片尺寸导致字体花屏
- Unity3D使用动态字库字体错位
- 编码,字体与字库
- (libgdx小结)TTF字库的使用(优美字体的绘制)
- 字库
- TTF字体_TTF格式字体_TTF格式字库_TTF格式字体大全_TTF格式字体...
- cocos2dx的CCLabelTTF类使用不同的TTF字库时字体没有变化的问题
- QT中QPF字库的制作与旋转字体的实现
- 关于QT字库的移植和使用总结(能够任意改变字体和颜色)
- C++在Tesseract-OCR中使用自己训练的字库进行字体识别
- 逐浪字库打造最全的书法字体,最全的合集(字体书法欣赏下载)
- SenchaTouch如何用字体icon font图标字库
- Android 全局替换第三方字库(自定义字体)
- 汉仪字库与中国美术学院字体设计工作站正式建立
- Unity 精简字库制作
- Unity Text字体花屏
- 关于ucgui的字库
- DR800SG的字库
- android接入google支付
- Android中选择器selector的使用
- cookie 和session 的区别详解
- Python-OpenCV 处理图像(五):图像中边界和轮廓检测
- HTML开发——响应式布局
- Unity的动态字库字体超出字库图片尺寸导致字体花屏
- casperjs中的step
- 颜色值的各类表示方法简介
- 2016 新的一年新的开始
- SharePreference和DiskLruCache的一些对比
- iOS开发之UINavigationBar(一)
- iOS开发多线程篇—多线程简单介绍
- Java内存模型
- Android图标规范