voidResetAlphaHitThresholdIfNeeded() { if (!SpriteSupportsAlphaHitTest() && m_AlphaHitTestMinimumThreshold > 0) { Debug.LogWarning("Sprite was changed for one not readable or with Crunch Compression. Resetting the AlphaHitThreshold to 0.", this); m_AlphaHitTestMinimumThreshold = 0; } }
publicvirtualvoidSetAllDirty() { // Optimization: Graphic layout doesn't need recalculation if // the underlying Sprite is the same size with the same texture. // (e.g. Sprite sheet texture animation)
///<summary> /// Mark the vertices as dirty and needing rebuilt. ///</summary> ///<remarks> /// Send a OnDirtyVertsCallback notification if any elements are registered. See RegisterDirtyVerticesCallback ///</remarks> publicvirtualvoidSetVerticesDirty() { if (!IsActive()) return;
privatevoidCleanInvalidItems() { // So MB's override the == operator for null equality, which checks // if they are destroyed. This is fine if you are looking at a concrete // mb, but in this case we are looking at a list of ICanvasElement // this won't forward the == operator to the MB, but just check if the // interface is null. IsDestroyed will return if the backend is destroyed.
var layoutRebuildQueueCount = m_LayoutRebuildQueue.Count; for (int i = layoutRebuildQueueCount - 1; i >= 0; --i) { var item = m_LayoutRebuildQueue[i]; if (item == null) { m_LayoutRebuildQueue.RemoveAt(i); continue; }
m_LayoutRebuildQueue.Sort(s_SortLayoutFunction); for (int i = 0; i <= (int)CanvasUpdate.PostLayout; i++) { for (int j = 0; j < m_LayoutRebuildQueue.Count; j++) { rebuild.Rebuild((CanvasUpdate)i); } } for (int i = 0; i < m_LayoutRebuildQueue.Count; ++i) m_LayoutRebuildQueue[i].LayoutComplete();
publicvirtualvoidRebuild(CanvasUpdate update) { if (canvasRenderer == null || canvasRenderer.cull) return;
switch (update) { case CanvasUpdate.PreRender: if (m_VertsDirty) { UpdateGeometry(); m_VertsDirty = false; } if (m_MaterialDirty) { UpdateMaterial(); m_MaterialDirty = false; } break; } }
换句话说,对于组件Image,不会进行Layout重建而只有Graphics重建。
3. UI元素裁剪
这一部分的关键代码就一句:
1
ClipperRegistry.instance.Cull();
裁剪和Mask等有关,我们之后单开一篇文章进行叙述。
4. 对Graphic重建队列元素进行重建
如下,代码去除了错误处理、参数验证和性能采样的部分:
1 2 3 4 5 6 7 8 9 10
for (var i = (int)CanvasUpdate.PreRender; i < (int)CanvasUpdate.MaxUpdateValue; i++) { for (var k = 0; k < m_GraphicRebuildQueue.Count; k++) { element.Rebuild((CanvasUpdate)i); } }
for (int i = 0; i < m_GraphicRebuildQueue.Count; ++i) m_GraphicRebuildQueue[i].GraphicUpdateComplete();