首页 >Unity3D频道 >【UGUI研究院】 > UGUI研究院之找到具体某个引起了网格重建的UI元素(三十二)
2019
01-18

UGUI研究院之找到具体某个引起了网格重建的UI元素(三十二)

UGUI的标志性函数Canvas.SendWillRenderCanvas,我们只知道网格进行了重建,但是并不知道是哪个Canvas,由于哪个UI元素引起了Canvas网格的重建。比如,修改RectTransform的属性、Text文本内容、更换Sprite、颜色、都会引起网格重建,通过观察UGUI源码,我们发现了可以通过反射的方式,获取到一些有用的信息,在UGUI源码中CanvasUpdateRegistry.cs类中。

通过注册   Canvas.willRenderCanvases += PerformUpdate; 将需要重建的网格保存在

private readonly IndexedSet<ICanvasElement> m_LayoutRebuildQueue = new IndexedSet<ICanvasElement>();
private readonly IndexedSet<ICanvasElement> m_GraphicRebuildQueue = new IndexedSet<ICanvasElement>();

然后就是遍历重建网格,我们要做的就是将这两个数据捞出来,我的代码是这样的。

using System.Collections.Generic;
using System.Reflection;
using UnityEngine;
using UnityEngine.UI;

public class NewBehaviourScript : MonoBehaviour {

    IList<ICanvasElement> m_LayoutRebuildQueue;
    IList<ICanvasElement> m_GraphicRebuildQueue;

    private void Awake()
    {
        System.Type type = typeof(CanvasUpdateRegistry);
        FieldInfo field = type.GetField("m_LayoutRebuildQueue", BindingFlags.NonPublic | BindingFlags.Instance);
        m_LayoutRebuildQueue = (IList<ICanvasElement>)field.GetValue(CanvasUpdateRegistry.instance);
        field = type.GetField("m_GraphicRebuildQueue", BindingFlags.NonPublic | BindingFlags.Instance);
        m_GraphicRebuildQueue = (IList<ICanvasElement>)field.GetValue(CanvasUpdateRegistry.instance);
    }

    private void Update()
    {
        for (int j = 0; j < m_LayoutRebuildQueue.Count; j++)
        {
            var rebuild = m_LayoutRebuildQueue[j];
            if (ObjectValidForUpdate(rebuild))
            {
                Debug.LogFormat("{0}引起{1}网格重建", rebuild.transform.name, rebuild.transform.GetComponent<Graphic>().canvas.name);
            }
        }

        for (int j = 0; j < m_GraphicRebuildQueue.Count; j++)
        {
            var element = m_GraphicRebuildQueue[j];
            if (ObjectValidForUpdate(element))
            {
                Debug.LogFormat("{0}引起{1}网格重建", element.transform.name, element.transform.GetComponent<Graphic>().canvas.name);
            }
        }
    }
    private bool ObjectValidForUpdate(ICanvasElement element)
    {
        var valid = element != null;

        var isUnityObject = element is Object;
        if (isUnityObject)
            valid = (element as Object) != null; //Here we make use of the overloaded UnityEngine.Object == null, that checks if the native object is alive.

        return valid;
    }
}

如下图所示,修改属性以后就知道某个具体的UI元素引起了某个具体的Canvas发生了网格重建。
Snip20190118_1

 

目前来看除了RectTransform的变换都可以通过以上方式获取,通过观察代码发现,修改RectTransform都会回调到C++底层,我们在上层就无法将数据捞出来了。欢迎大家一起讨论。

最后编辑:
作者:雨松MOMO
专注移动互联网,Unity3D游戏开发
捐 赠写博客不易,如果您想请我喝一杯星巴克的话?就进来看吧!

UGUI研究院之找到具体某个引起了网格重建的UI元素(三十二)》有 4 条评论

  1. milk说:

    可以,很强。 大佬,有个问题请教一下,就是为什么网上说网格重建是针对单个Canvas而言呢?看源码是CanvasUpdateRegistry单例来进行注册你打印的2个列表元素的,按道理是可能会涉及到多个Canvas的UI的呀。还有是网格重建其实是有2个过程的叫ReBatch、ReBuild,ReBatch是绘制,然后才ReBuild,都整懵逼了

    • 木骰说:

      网上大多都抄来抄去 一个没说明白 大家都跟着懵逼。
      看源码 针对Canvas的说法应该是 重建和重新合批。 也就是对应 Rebuild 和 ReBatch,网上普遍把这两个混在一起讲 统称重建。
      而Rebuild应该是针对单个UI元素的Mesh重建,而不是整个Canvas,但是单个UI的Rebuild后续又会触发Canvas的Rebatch。 所以一般说的Canvas重建应该是Rebatch,或者硬要说的话 是单个Rebuild + 整体Rebatch 的过程。

  2. DrangKe说:

    事实上,大多来自动画,显示都会有一次。刚好年前跟着大佬 搞过这块

留下一个回复

你的email不会被公开。 必填项已用 * 标注。