通常UGUI界面操作卡大概率都是Canvas.SendWillRenderCanvases()
方法耗时,需要检查界面是否存在多余或者无用的重建情况。由于界面很多我们无法定位到到底是哪个界面下的哪个元素引起了网格重建。
通过观察CanvasUpdateRegistry.cs
源代码,我们发现需要网格重建的元素都被缓存在这两个对象中。
// CanvasUpdateRegistry.cs(部分代码)
public class CanvasUpdateRegistry
{
//...略
//保存待重建布局元素(如:RectTransform变化)
private readonly IndexedSet<ICanvasElement> m_LayoutRebuildQueue = new IndexedSet<ICanvasElement>();
//保存待重建渲染元素(如:Image变化)
private readonly IndexedSet<ICanvasElement> m_GraphicRebuildQueue = new IndexedSet<ICanvasElement>();
}
我们来看看待重建布局元素和待重建渲染元素是如何被缓存起来的。如果某个Graphic发生布局位置或者渲染变化会分别加入这两个不同的渲染队列,等待下一次UI的重建。
// Graphic cs(部分代码
public abstract class Graphic: UIBehaviour,ICanvasElement
{
//...略
protected override void OnBeforeTransformParentChanged()
{
GraphicRegistry.UnregisterGraphicForCanvas(canvas, this);
//布局发生变化
LayoutRebuilder.MarkLayoutForRebuild(rectTransform);
//LayoutRebuilder.MarkLayoutForRebuild方法内部实现
//private static void MarkLayoutRootForRebuild(RectTransform controller)
//{
// if (controller == null)
// return;
// var rebuilder = s_Rebuilders.Get();
// rebuilder.Initialize(controller);
// 局部发生变化,会通过TryRegisterCanvasElementForLayoutRebuild()将自己加入待布局重建队列
// if (!CanvasUpdateRegistry.TryRegisterCanvasElementForLayoutRebuild(rebuilder))
// s_Rebuilders.Release(rebuilder);
//}
}
public virtual void SetMaterialDirty()
{
if (!IsActive())
return;
m_MaterialDirty = true;
//渲染发生变化,会通过RegisterCanvasElementForGraphicRebuild()将自己加入待渲染队列
CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
if (m_OnDirtyMaterialCallback != null)
m_OnDirtyMaterialCallback();
}
}
所以我们只需要在外面将这两个对象捞出来遍历一下就能知道到底是哪个界面下的哪个元素引起了网格重建。
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}引起网格重建",rebuild.transform.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;
//Here we make use of the overloaded UnityEngine.Object == null,that checks if the native object is alive.
if (isUnityObject)
valid = (element as Object) != null;
return valid;
}
}
如图3-2所示,当Canvas下某个元素引起了网格重建,我们可以知道具体是哪个UI元素。