当前位置:网站首页>Unity3d: ugui source code, rebuild optimization
Unity3d: ugui source code, rebuild optimization
2022-07-23 12:45:00 【Sixi Liyu】
Image How to draw
Unity The objects rendered in are made of mesh (Mesh) Composed of , The drawing unit of mesh is primitive ( spot 、 Line 、 Trigonometry )
The drawing information is stored in Vertexhelper Class , Except for vertices , It also includes normals 、UV、 Color 、 Tangent line .
Rebuild Concept
Canvas Be responsible for the of child nodes UI Mesh merge of elements , And generate corresponding rendering instructions and send them to Unity The process of graphics pipeline . therefore Canvas It's rendering UI The components of , When UI If there is a change, it should be implemented once Batch, It is the culprit that has a greater impact on performance . Be careful Canvas Of Batch Only its child nodes will be affected , But it won't affect his son Canvas.
Rebuild The flow of the program
- Image,Text They are all inherited. Graphic,Graphic Yes ICanvasElement Interface , Realization rebuild function
public interface ICanvasElement
{
/// <summary>
/// Rebuild the element for the given stage.
/// </summary>
/// <param name="executing">The current CanvasUpdate stage being rebuild.</param>
/// // according to CanvasUpdate Rebuild elements at different stages of
void Rebuild(CanvasUpdate executing);
- CanvasUpdateRegistry monitor Canvas Of willRenderCanvases event , This event will be called every frame before rendering
public class CanvasUpdateRegistry
{
// Layout rebuild queue , When UI When the layout of the element needs to be updated, it is added to the queue
private readonly IndexedSet<ICanvasElement> m_LayoutRebuildQueue = new IndexedSet<ICanvasElement>();
// Graph reconstruction queue , When UI When the image of the element needs to be updated, it is added to the queue
private readonly IndexedSet<ICanvasElement> m_GraphicRebuildQueue = new IndexedSet<ICanvasElement>();
protected CanvasUpdateRegistry()
{
// Monitored Canvas Of willRenderCanvases event , This event will be called every frame before rendering
Canvas.willRenderCanvases += PerformUpdate;
}
- PerformUpdate Collect layout and rebuild queue , Graph reconstruction queue call ICanvasElement.Rebuild Complete the reconstruction
When to join the reconstruction
By setting “ Dirty data ” Realized , Including layout (Layout)、 texture of material (Material) And vertex (Vertices) In the third part of , Set the layout to dirty , The layout will be rebuilt , Set the vertex or material to dirty , Then carry out graphic reconstruction . The layout will be added to the reconstruction itself m_LayoutRebuildQueue in , Graphic reconstruction will add itself to m_GraphicRebuildQueue in , Waiting to be called .
SetLayoutDirty: Join the layout reconstruction queue
SetVerticesDirty,SetMaterialDirty: texture of material , Vertex transformations are added to the graph reconstruction queue
Layout reconstruction : Position or size ;
Image reconstruction : Vertex change , Material change ( size , Rotation and text changes 、 Picture modification )
Optimize
The main target , hold Profile in Canvas.SendWillRenderCanv Parameter reduction . By limiting the number of vertices , Vertex change, etc .
text Property change (“123”–>“1234”), Trigger SetLayoutDirty: Doing countdown related , According to each 1s change , Don't change in real time
Change the text , Picture color , Trigger SetVerticesDirty( Vertex change ), So the best way to change the picture color is to change the shader color
layout Components cause reconstruction problems
text Stroke , Shadow performance problems
One character produces 4 vertices ,
If you add Shadow It is equivalent to putting Text Copied it again to produce 8 individual ,
Outline Will be Text Copy 4 Over and over again 20 vertices .
With corresponding shader Replacetext The gradient
Image Format selection
Image: The number of vertices depends on Image Type The choice of .
①Simple 4 vertices ;
②Sliced Check FillCenter The number of vertices is 36 individual , Uncheck yes 32 individual ;
③Tiled Depending on Rectranform Set the size of the and the size of the original image , Spread out N Zhang Tu is 4*N individual ;
④Filled More choices , But at least 4 individual .
So for Image The preferred Simple Mode followed by Sliced Mode and uncheck FillCenterDynamic and static separation :Canvas.SendWillRenderCanvases() And Canvas.BuildBatch() The calculation is based on Canvas For the root node , Different Canvas It won't affect the other Canvas. however , A large number of dynamic and static separation will affect Canvas Approved by , So it can be targeted to fight UI, The main interface is separated
Check the factors affecting reconstruction in the source code
Trigger SetLayoutDirty
Graphic:
- protected override void OnRectTransformDimensionsChange(): When UI Of RectTransform Callback when changing , As long as inherit UIBehavior You can get the callback
Image:
- protected override void OnCanvasHierarchyChanged(): The state of the parent canvas changes
Text:
- text Property change : Doing countdown related , According to each 1s change , Don't change in real time
- public bool supportRichText: When setting whether to turn on rich text , Switching rules ( As long as the status is different from last time ,SetLayoutDirty once , Instead of turning it on in real time Dirty)
- public bool resizeTextForBestFit: Set whether to allow text to be automatically resized , Switching rules
- public int resizeTextMinSize: Minimum text size allowed
- public int resizeTextMaxSize: Set maximum text size
- public TextAnchor alignment: The text is relative to its RectTransform The positioning of .
- public int fontSize: Text size
- public HorizontalWrapMode horizontalOverflow: Horizontal overflow mode
- public VerticalWrapMode verticalOverflow: Vertical overflow mode
- public float lineSpacing: Row spacing , Specifies a factor for the line height of the font . The value is 1 Standard line spacing will be generated
- public FontStyle fontStyle: Font style
Trigger SetVerticesDirty: Vertex change
Graphic:
- public virtual Color color: Color , So the best way to change the picture color is to change the shader color
- protected override void OnRectTransformDimensionsChange(): When UI Of RectTransform Callback when changing , As long as inherit UIBehavior You can get the callback
Image:
- public Type type:Simple,Sliced etc.
- public bool preserveAspect: Whether to maintain the aspect ratio , Switching rules
- public bool fillCenter
- public FillMethod fillMethod: Fill mode
- public float fillAmount
- public bool fillClockwise
- public int fillOrigin
- public bool useSpriteMesh: Cut the transparent part of the picture
- protected override void OnCanvasHierarchyChanged(): The parent canvas changes
RawImage:
- public Texture texture
- public Rect uvRect
Shadow:
- public Color effectColor
- public Vector2 effectDistance
- public bool useGraphicAlpha
Text:
- public virtual string text
- public bool supportRichText
- public bool resizeTextForBestFit
- public int resizeTextMinSize
- public int resizeTextMaxSize
- public TextAnchor alignment
- public bool alignByGeometry: Perform horizontal alignment using the glyph geometry of the section , Instead of glyph indicators .
This can lead to better fitting, left and right alignment , But it may lead to incorrect positioning when trying to cover multiple Fonts ( Such as professional outline font ) On - public int fontSize
- public HorizontalWrapMode horizontalOverflow
- public VerticalWrapMode verticalOverflow
- public float lineSpacing
- public FontStyle fontStyle
Trigger SetMaterialDirty: Material change
Graphic:
- public virtual Material material
Mask:
- public bool showMaskGraphic:
- protected override void OnEnable()
- protected override void OnDisable()
- protected override void OnValidate(): The editor uses
MaskableGraphic:
- public bool maskable
- protected override void OnTransformParentChanged()
- protected override void OnCanvasHierarchyChanged()
- public virtual void RecalculateMasking(): Recalculate the mask for this element and all child elements .
Trigger SetAllDirty, Total change
Image Indirectly inherited from Graphic, When it's Sprite When something changes , Would call SetAllDirty function
SetAllDirty Change the timing
Graphic:
- protected override void OnTransformParentChanged() The parent object changes
- protected override void OnEnable()
- protected override void Reset(): Assignment default , Only useful under the editor , Can be ignored
- protected override void OnDidApplyAnimationProperties(): Animation properties change
- protected override void OnValidate(): Script loading or Inspector When any value in is modified, it will call , Only useful under the editor , Can be ignored
Image:
- static void RebuildImage(SpriteAtlas spriteAtlas) Atlas changes
- sprite Property change
- overrideSprite Temporarily modify the picture
- public override void SetNativeSize() Set size
Text:
- public void FontTextureChanged(): The font texture has been modified :TTF Dynamic Fonts ,Text Every time you assign a value Unity Will generate maps , And save each word UV Information , Then when displaying the font, according to UV The information is taken from the generated map and finally rendered on the screen .
- font Property changes
边栏推荐
- flask项目celery使用redis sentinel中遇到的坑
- Explain the release of TCP connection in detail
- 并发编程1-2
- SCI审稿过程中的几种状态
- Hcip--- BGP related configuration (Federal chapter)
- 浅析UDP协议和TCP协议
- GameFramework:打包资源,打随app发布包,打包生成文件夹说明,上传资源至服务器,下载资源,GameFreamworkList.dat 与GameFrameworkVersion.dat
- 关于搭建Hybrid App所需要的基础技术一文
- 如何解决if语句太多
- 线程池总结
猜你喜欢
随机推荐
《Kubernetes in Action》第二章笔记
Vs attribute configuration related knowledge
Unity在URP管线下使用TriLib插件加载模型材质不正确的问题
Analyze redis cluster
Explain TCP segmentation and IP fragmentation in detail
C语言:基于顺序表的学生管理系统,超级详细,全部都有注释,看完不懂来扇我。
Common sort exchange sort
直白理解一文搞定http协议缓存
剑指offer 05 两个栈实现队列
[bootloader architecture and brushing process based on UDS service]
如何解决if语句太多
C语言:详细讲解基于tcp和udp的两种本地通信方式
牛客面试必考真题【算法篇】高频Top200 题目汇总
表格个人简历
C# 自定义双向链表
LVS负载均衡调度原理及配置方法
[AUTOSAR DCM 1. module introduction (DSL, DSD, DSP)]
htpasswd作用
Analysis of Internet Protocol (II)
Unity Shader丢失问题









