UGUI Font图集

输入一些字母。

删掉部分字母:贴图把删掉的字母回收了

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();
	    }
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值