内容简介: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发生了网格重建。
目前来看除了RectTransform的变换都可以通过以上方式获取,通过观察代码发现,修改RectTransform都会回调到C++底层,我们在上层就无法将数据捞出来了。欢迎大家一起讨论。
- 本文固定链接: https://www.xuanyusong.com/archives/4573
- 转载请注明:雨松MOMO 于雨松MOMO程序研究院 发表
雨松MOMO提醒您:亲,如果您觉得本文不错,快快将这篇文章分享出去吧 。另外请点击网站顶部彩色广告或者捐赠支持本站发展,谢谢!
捐 赠 如果您愿意花20块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
深入浅出Web设计(中文版)
(美)瓦特罗尔、(美)西罗托 / O'Reilly Taiwan公司 / 东南大学出版社 / 2010-11 / 99.00元
你将从《深入浅出Web设计(中文版)》学到什么?你曾经希望看看书就能学到真正的网站设计吗?曾经想过该如何同时达成让网站看起来美观,又能有效率地沟通信息,还要通过可访问性与可用性的策略吗?《深入浅出Web设计》正是精通上述主题的秘笈。我们将学到如何设计一个绝佳、用户友好的网站,上谈客户需求,下说手绘分镜表,乃至完成在线所需的HTML与css主文件……而且会有一个真正可以运作的网站!一起来看看 《深入浅出Web设计(中文版)》 这本书的介绍吧!
CSS 压缩/解压工具
在线压缩/解压 CSS 代码
URL 编码/解码
URL 编码/解码