Graphic
它是如何将我们的UI显示在屏幕上的
贴一个简单的例子
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MyGraphic : MonoBehaviour
{
public Texture texture;
// Start is called before the first frame update
void Start()
{
Mesh mesh = new Mesh();
mesh.Clear();
mesh.SetVertices(new List<Vector3>() { new Vector3(0, 0), new Vector3(0, 100), new Vector3(100, 100), new Vector3(100, 0) });
mesh.SetUVs(0, new List<Vector2>() { new Vector2(0, 0), new Vector2(0, 1), new Vector2(1, 1), new Vector2(1, 0) });
mesh.SetTriangles(new List<int>() { 0, 1, 2, 2, 3, 0 }, 0);
mesh.RecalculateBounds();
CanvasRenderer canvasRenderer = gameObject.AddComponent<CanvasRenderer>();
canvasRenderer.SetMesh(mesh);
canvasRenderer.materialCount = 1;
canvasRenderer.SetMaterial(Canvas.GetDefaultCanvasMaterial(), 0);
canvasRenderer.SetTexture(texture);
}
}
效果如下: 我们使用了CanvasRenderer,简单的设置了Mesh,Material,Texture就将图片显示出来了。
CanvasRenderer : A component that will render to the screen after all normal rendering has completed when attached to a Canvas. Designed for GUI application.
Graphic就是通过CanvasRenderer将UI显示在屏幕上的,具体Graphic是在哪里设置的Mesh,Material,Texture可以看后面的代码。
Graphic的代码逻辑
首先是Graphic的OnEnable
protected override void OnEnable()
{
base.OnEnable();
// 找到parent第一个active且enabled的canvas
CacheCanvas();
// GraphicRegistry将Graphic和canvas储存在Dictionary<Canvas, IndexedSet<Graphic>> m_Graphics中
GraphicRegistry.RegisterGraphicForCanvas(canvas, this);
#if UNITY_EDITOR
GraphicRebuildTracker.TrackGraphic(this);
#endif
if (s_WhiteTexture == null)
s_WhiteTexture = Texture2D.whiteTexture;
// 将Layout, Vertices, Materials设置为dirty状态
SetAllDirty();
}
OnEnable简单的做了些初始化工作,然后执行了SetAllDirty,这个函数将Layout, Vertices, Materials设置为dirty状态,这里主要看下对Vertices, Materials的处理,两者均调用了
// CanvasUpdateRegistry负责将graphic添加进入IndexedSet<ICanvasElement> m_GraphicRebuildQueue列表中
CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
在进行这一步的时候,CanvasUpdateRegistry实例化并在构造函数里执行了
// Event that is called just before Canvas rendering happens.
// This allows you to delay processing / updating of canvas based elements until just before they are rendered.
Canvas.willRenderCanvases += PerformUpdate;
PerformUpdate函数将会在每次Canvas渲染前执行。
private void PerformUpdate()
{
......
m_PerformingGraphicUpdate = true;
for (var i = (int)CanvasUpdate.PreRender; i < (int)CanvasUpdate.MaxUpdateValue; i++)
{
for (var k = 0; k < m_GraphicRebuildQueue.Count; k++)
{
try
{
var element = m_GraphicRebuildQueue[k];
if (ObjectValidForUpdate(element))
element.Rebuild((CanvasUpdate)i);
}
catch (Exception e)
{
Debug.LogException(e, m_GraphicRebuildQueue[k].transform);
}
}
}
for (int i = 0; i < m_GraphicRebuildQueue.Count; ++i)
m_GraphicRebuildQueue[i].GraphicUpdateComplete();
m_GraphicRebuildQueue.Clear();
m_PerformingGraphicUpdate = false;
......
}
这个函数遍历了m_GraphicRebuildQueue,并执行其Rebuild方法,GraphicUpdateComplete方法。
// 每当Vertices, Materials为dirty状态,去UpdateGeometry,UpdateMaterial
public virtual void Rebuild(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;
}
}
// UpdateGeometry函数最终会调用DoMeshGeneration
private void DoMeshGeneration()
{
if (rectTransform != null && rectTransform.rect.width >= 0 && rectTransform.rect.height >= 0)
OnPopulateMesh(s_VertexHelper);
else
s_VertexHelper.Clear(); // clear the vertex helper so invalid graphics dont draw.
var components = ListPool<Component>.Get();
GetComponents(typeof(IMeshModifier), components);
for (var i = 0; i < components.Count; i++)
((IMeshModifier)components[i]).ModifyMesh(s_VertexHelper);
ListPool<Component>.Release(components);
s_VertexHelper.FillMesh(workerMesh);
canvasRenderer.SetMesh(workerMesh);
}
protected virtual void UpdateMaterial()
{
if (!IsActive())
return;
canvasRenderer.materialCount = 1;
canvasRenderer.SetMaterial(materialForRendering, 0);
canvasRenderer.SetTexture(mainTexture);
}
可以看到在UpdateMaterial时候Graphic会SetMaterial和SetTexture,在DoMeshGeneration会SetMesh。 通过以上流程,Graphic就将mainTexture显示在屏幕上了。
|