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

栏目: 后端 · 发布时间: 5年前

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

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发生了网格重建。

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

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

雨松MOMO提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!

最后编辑:

作者:雨松MOMO

专注移动互联网,Unity3D游戏开发

站内专栏 QQ交谈 腾讯微博 新浪微博

捐 赠 如果您愿意花20块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

ANSI Common Lisp

ANSI Common Lisp

Paul Graham / Prentice Hall / 1995-11-12 / USD 116.40

For use as a core text supplement in any course covering common LISP such as Artificial Intelligence or Concepts of Programming Languages. Teaching students new and more powerful ways of thinking abo......一起来看看 《ANSI Common Lisp》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

随机密码生成器
随机密码生成器

多种字符组合密码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具