输入一些字母。
删掉部分字母:贴图把删掉的字母回收了
Disable这个GameObject:贴图把所有的字母都回收了
增加很多中文:贴图扩大了,从256x256,变成512x512。
动态图集尺寸变大,引起其他Text组件Dirty
/// <summary>
/// Called by the FontUpdateTracker when the texture associated with a font is modified.
/// </summary>
public void FontTextureChanged()
{
// Only invoke if we are not destroyed.
if (!this)
return;
if (m_DisableFontTextureRebuiltCallback)
return;
cachedTextGenerator.Invalidate();
if (!IsActive())
return;
// this is a bit hacky, but it is currently the
// cleanest solution....
// if we detect the font texture has changed and are in a rebuild loop
// we just regenerate the verts for the new UV's
if (CanvasUpdateRegistry.IsRebuildingGraphics() || CanvasUpdateRegistry.IsRebuildingLayout())
{
UpdateGeometry();
Debug.Log("FontTextureChanged UpdateGeometry : " + gameObject.name);
}
else
{
SetAllDirty();
Debug.Log("FontTextureChanged SetAllDirty : " + gameObject.name);
}
}
总结
UGUI支持动态图集,有类似引用计数的概念。最大支持的贴图尺寸是4096*4096,16M,只要同屏显示的文字不超过这个限制就可以,绝对是够用的。
查看Font的代码
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class FontViewer : EditorWindow
{
[SerializeField] private Font m_Font;
private void ShowFonts()
{
m_Font = (Font)EditorGUILayout.ObjectField(m_Font, typeof(Font), true, GUILayout.Width(300));
if (m_Font != null)
{
Texture texture = m_Font.material.mainTexture;
EditorGUILayout.LabelField(string.Format("width={0} height={1}", texture.width, texture.height));
EditorGUI.BeginDisabledGroup(true);
EditorGUILayout.ObjectField("", texture, typeof(Texture), true, GUILayout.Width(600), GUILayout.Height(600));
EditorGUI.EndDisabledGroup();
}
}
[MenuItem("UITools/FontViewer")]
private static void ShowWindow()
{
var window = GetWindow(typeof(FontViewer)) as FontViewer;
window.titleContent = new GUIContent("FontViewer");
window.Show();
}
private void OnGUI()
{
ShowFonts();
}
}