UGUI 字体2

我们项目从某个时候开始ui突然开始出现字体花屏现象(unity 开发版本:5.3.6p6),而且很难必现却又时有发生,确实查找和解决起来并不太容易。

  关于这个问题,uwa官方给出了解释,http://blog.uwa4d.com/archives/techsharing_35.html, http://blog.uwa4d.com/search/%E5%AD%97%E4%BD%93/。可用的方案就是一开始把字体扩容到足够大,但对于常用汉字就有3000多个,再加上不同的大小和样式,不同的字体,内存占用光想一想就很可观,所以将此当做最保底的方案。Unity 官方应该是解决过这个问题,但是没有彻底。但是我更加高度怀疑的是:FontTexture 本身的生成和对应的 UV 信息并没有问题,而是字体使用的 UV 有问题(没有更新),uwa 提供的方案可能是针对的早期问题,也许现在问题是新引起的。

  后来又看到雨松MOMO的博客也有提到并给出了解决方案:http://www.xuanyusong.com/archives/4259,这个方法很好,在 FontTexture 重建时在其后的 LateUpdate 中刷新所有的 Text,这个方案也是我希望的方式,不会产生过多的浪费字体纹理。不过 GameObject.FindObjectsOfType<Text>() 这个调用我觉得对于 Text 过多的场景恐怕效率堪忧,翻阅了下 UI 的源码,发现其中有个类 UnityEngine.UI.FontUpdateTracker 就是专门用来更新 Text 相关字体信息的,但是接口都不是 public,但是可以反射调用就好,所以可以用此对 雨松MONO 的方案优化一下:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class DynamicFontTextureRebuildTracker : MonoBehaviour
{
    private class FontUpdateNode
    {
        private bool m_FontTextureRebuilt = false;
        private Font m_FontRebuilt = null;

        public FontUpdateNode(Font font)
        {
            m_FontRebuilt = font;
            Validate();
        }

        public void Validate()
        {
            if (null == m_FontRebuilt)
            {
                m_FontTextureRebuilt = false;

                Debug.LogWarning("You need a actual font to validate!");
                return;
            }

            m_FontTextureRebuilt = true;
        }

        public void Invalidate()
        {
            m_FontTextureRebuilt = false;
        }

        public bool NeedUpdate
        {
            get { return m_FontTextureRebuilt && (null != m_FontRebuilt); }
        }

        public Font font
        {
            get { return m_FontRebuilt; }
        }
    }

    private System.Reflection.MethodInfo m_RebuildForFont = null;
    private List<FontUpdateNode> m_FontUpdateList = new List<FontUpdateNode>();

    private static DynamicFontTextureRebuildTracker m_Instance = null;

    void Awake()
    {
        if (null != m_Instance)
        {
            Debug.LogError("There is only one DynamicFontTextureRebuildTracker instance allowed!");
            Destroy(gameObject);
            return;
        }

        m_Instance = this;
    }

    // Use this for initialization
    void Start() 
    {
        Font.textureRebuilt += OnFontTextureRebuilt;

        System.Type fontUpdateTrackerType = typeof(UnityEngine.UI.FontUpdateTracker);
        m_RebuildForFont = fontUpdateTrackerType.GetMethod("RebuildForFont", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
        Debug.Log("Get RebuildForFont method is: " + m_RebuildForFont);
    }
    
    // Update is called once per frame
    void LateUpdate() 
    {
        if (null == m_RebuildForFont)
        {
            return;
        }

        for (int i = 0; i < m_FontUpdateList.Count; i++)
        {
            FontUpdateNode node = m_FontUpdateList[i];
            if (node.NeedUpdate)
            {
                Font font = node.font;
                m_RebuildForFont.Invoke(null, new object[] { font });

                // Log rebuild.
                Texture fontTexture = font.material.mainTexture;
                Debug.Log(string.Format("Texture of dynamic font \"{0}\" is enlarged to {1}x{2}.", font.name, fontTexture.width, fontTexture.height));

                node.Invalidate();
            }
        }
    }

    void OnDestroy()
    {
        Font.textureRebuilt -= OnFontTextureRebuilt;
    }

    private void OnFontTextureRebuilt(Font font)
    {
        bool findThisFont = false;
        for (int i = 0; i < m_FontUpdateList.Count; i++)
        {
            FontUpdateNode node = m_FontUpdateList[i];
            if (node.font == font)
            {
                node.Validate();
                findThisFont = true;
                break;
            }
        }

        if (!findThisFont)
        {
            m_FontUpdateList.Add(new FontUpdateNode(font));
        }
    }

    //void OnGUI()
    //{
    //    if (GUI.Button(new Rect(30.0f, 50.0f, 200.0f, 50.0f), "Force Update Text"))
    //    {
    //        for (int i = 0; i < m_FontUpdateList.Count; i++)
    //        {
    //            Font font = m_FontUpdateList[i].font;
    //            m_RebuildForFont.Invoke(null, new object[] { font });
    //            Debug.Log(string.Format("Force rebuild text for font \"{0}\".", font.name));
    //        }

    //        Debug.Log("Force rebuild all text ok!");
    //    }
    //}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值