内容简介: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块钱请我喝一杯咖啡的话,请用手机扫描二维码即可通过支付宝直接向我捐款哦。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Microsoft Windows程序设计
佩措尔德 / 章立民 / 华中科技 / 2004-1 / 118.00元
Charles Petzold是全球最权威且知名的Windows程序设计专家,他将其最畅销Programming Microsoft Windows with C#一书加以改写,使之能完全适用于Visual Basic.NET的开发人员。这位畅销书的作家示范了如何使用Visual Basic.NET将Windows Forms的功能发挥到极致(Windows Forms是新一代的Windows程序......一起来看看 《Microsoft Windows程序设计》 这本书的介绍吧!