UGUI的动态字体会动态生成材质,开始是256*256,然后根据字体的使用情况慢慢扩大。直到4096*4096。当文字太多不够放的时候,会触发UGUI内部重建字体贴图命令,接着就可能造成文字花屏了。可以监听重建的事件,然后刷新一下当前场景中的所有字体即可。
using UnityEngine; using UnityEngine.UI; public class FontRebuild : MonoBehaviour { private Font m_NeedRebuildFont = null; void Start () { Font.textureRebuilt += delegate (Font font) { m_NeedRebuildFont = font; Debug.LogError(font.name); }; } void Update () { if (m_NeedRebuildFont) { Text[] texts = GameObject.FindObjectsOfType<Text>(); if (texts != null) { foreach (Text text in texts) { text.FontTextureChanged(); } } m_NeedRebuildFont = null; } } }