当前位置:网站首页>Unity ugui source code graphic
Unity ugui source code graphic
2022-07-05 04:57:00 【OldMan_ sjc】
Graphic
How does it bring us UI Displayed on the screen
Post a simple example
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);
}
}
The effect is as follows :
We used CanvasRenderer, Simple setup Mesh,Material,Texture The picture is displayed .
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 It is through CanvasRenderer take UI Displayed on the screen , Specifically Graphic Where is it set up Mesh,Material,Texture You can see the following code .
Graphic Code logic
First of all Graphic Of OnEnable
protected override void OnEnable()
{
base.OnEnable();
// find parent first active And enabled Of canvas
CacheCanvas();
// GraphicRegistry take Graphic and canvas Stored in the Dictionary<Canvas, IndexedSet<Graphic>> m_Graphics in
GraphicRegistry.RegisterGraphicForCanvas(canvas, this);
#if UNITY_EDITOR
GraphicRebuildTracker.TrackGraphic(this);
#endif
if (s_WhiteTexture == null)
s_WhiteTexture = Texture2D.whiteTexture;
// take Layout, Vertices, Materials Set to dirty state
SetAllDirty();
}
OnEnable Simply do some initialization , And then we did SetAllDirty, This function will Layout, Vertices, Materials Set to dirty state , Here we mainly look at the right Vertices, Materials To deal with , Both call
// CanvasUpdateRegistry Responsible for graphic Add entry IndexedSet<ICanvasElement> m_GraphicRebuildQueue In the list
CanvasUpdateRegistry.RegisterCanvasElementForGraphicRebuild(this);
In this step ,CanvasUpdateRegistry Instantiate and execute in the constructor
// 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 The function will be used every time Canvas Before rendering .
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;
......
}
This function traverses m_GraphicRebuildQueue, And execute Rebuild Method ,GraphicUpdateComplete Method .
// whenever Vertices, Materials by dirty state , Go to 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 The function will eventually call 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);
}
You can see in the UpdateMaterial When Graphic Meeting SetMaterial and SetTexture, stay DoMeshGeneration Meeting SetMesh.
Through the above process ,Graphic will mainTexture It's on the screen .
边栏推荐
- 3dsmax snaps to frozen objects
- 质量体系建设之路的分分合合
- [groovy] closure (closure call | closure default parameter it | code example)
- 【acwing】240. food chain
- On-off and on-off of quality system construction
- Download the details and sequence of the original data access from the ENA database in EBI
- Cocos progress bar progresstimer
- 【Leetcode】1352. Product of the last K numbers
- [groovy] closure (Introduction to closure class closure | closure parametertypes and maximumnumberofparameters member usage)
- Manually implement heap sorting -838 Heap sort
猜你喜欢
【Leetcode】1352. 最后 K 个数的乘积
AutoCAD - command repetition, undo and redo
Use assimp library to read MTL file data
AutoCAD - Document Management
Unity parallax infinite scrolling background
AutoCAD -- dimension break
AutoCAD - feature matching
PostgreSQL 超越 MySQL,“世界上最好的编程语言”薪水偏低
【acwing】240. food chain
Special information | real estate and office buildings - 22.1.9
随机推荐
Redis has four methods for checking big keys, which are necessary for optimization
669. 修剪二叉搜索树 ●●
The first topic of ape Anthropology
Wan broadband access technology V EPON Technology
[Business Research Report] top ten trends of science and technology and it in 2022 - with download link
AutoCAD - workspace settings
775 Div.1 B. integral array mathematics
Flutter 小技巧之 ListView 和 PageView 的各种花式嵌套
AutoCAD - scaling
XSS injection
AutoCAD - set layer
AutoCAD - feature matching
stm32Cubemx(8):RTC和RTC唤醒中断
中国金刚烷行业研究与投资预测报告(2022版)
775 Div.1 C. Tyler and strings combinatorial mathematics
[groovy] closure (closure as function parameter | code example)
2022/7/2做题总结
Looking at Chinese science and technology from the Winter Olympics: what is the mystery of the high-speed camera that the whole people thank?
Group counting notes (1) - check code, original complement multiplication and division calculation, floating point calculation
UE4/UE5 虚幻引擎,材质篇(三),不同距离的材质优化