Unity UI 优化最佳实践2 -Unity UI 基础

 

理解组成Unity UI系统的不同部分很重要。 有几个基本的类和组件共同组成系统。 本章首先定义了本系列文章中使用的许多术语,然后讨论了Unity UI中几个关键系统的低级(low-level)行为。low-level

术语

Canvas是一个本地代码(native-code)的Unity组件,Unity的渲染系统使用该组件来提供分层的几何图形,这些图形将被绘制在游戏世界空间(world-space)中或世界空间(world-space)前面。

Canvases负责将其组成几何图形组合成批,生成相应的渲染命令并将其发送到Unity的图形系统。所有这些都是在本机C ++代码中完成的,称为rebatch 或batch build。当Canvas被标记为包含需要rebatching的几何体时,Canvas被认为是脏的。

几何图形由Canvas Renderer组件提供给Canvases。

Sub-canvas只是一个嵌套在另一个Canvas组件内的Canvas组件。Sub-canvas将他们的子canvas与父canvas分开;一个脏(dirty)的子canvas不会强迫父canvas重建其几何图形,反之亦然。但有些情况并非如此,例如对父Canvas进行更改导致调整子Canvas的大小时。

Graphic是由Unity UI C#库提供的基类。它是所有Unity UI C#类的基类,可为Canvas系统提供可绘制的几何图形。大多数内置的Unity UI图形都是通过MaskableGraphic子类实现的,这使得它们可以通过IMaskable接口进行屏蔽。 Drawable的主要子类是Image和Text,它们提供了同名的组件。

Layout 组件控制RectTransforms的大小和位置,通常用于创建需要相对大小或相对定位其内容的复杂布局。布局组件仅依赖于RectTransforms,并且仅影响其关联的RectTransforms的属性。它们不依赖于Graphic类,可以独立于Unity UI的Graphic组件使用。

Graphic和Layout组件都依赖于CanvasUpdateRegistry类,该类未暴露在Unity编辑器界面。该类跟踪必须更新的一组Layout组件和Graphic组件,并在其关联的Canvas调用willRenderCanvases事件时根据需要触发更新。

Layout和Graphic组件的更新称为重建(rebuild)。本文档稍后会详细讨论重建过程。

渲染细节

在Unity UI中编写用户界面时,请记住,Canvas绘制的所有几何图形都将绘制在透明队列(Transparent queue)中。 也就是说,Unity UI生成的几何图形将始终通过alpha混合重新绘制。 从性能角度要记住的重要一点是,多边形中被光栅化的每个像素都会被采样,即使它完全被其他不透明的多边形覆盖。 在移动设备上,这种高水平的过度绘制(overdraw)[1]可能会迅速超过GPU的填充率。

批量构建(Batch building)过程 -(Canvases)

批量构建过程是Canvas合并表示其UI元素的网格(mesh)并生成相应的渲染命令以发送到Unity的图形管线的过程。这个过程的结果被缓存并重复使用,直到Canvas被标记为脏的,每当其中任意一个组成网格发生变化时就会发生这种情况。

Canvas使用的网格取自附加到Canvas但不包含在任何Sub-canvas中的Canvas Renderer组件集合。

计算批次需要按照深度对网格进行排序并检查它们是否有重叠,共享材料等。这个操作是多线程的,所以在不同的CPU架构中,尤其是在移动SoC(通常具有很少CPU核心)和现代台式机CPU(通常具有4个或更多核心)之间,其性能通常会有很大差异。

重建(rebuild)过程 -(Graphics)

重建rebuild过程是Unity UI C#图形组件的布局和网格被重新计算的地方。这是在CanvasUpdateRegistry类中执行的。请记住,这是一个C#类,其源可以在Unity的Bitbucket上找到。

在CanvasUpdateRegistry中,相关方法是PerformUpdate。只要Canvas组件调用WillRenderCanvases事件,就会调用此方法。该事件每帧调用一次。

PerformUpdate运行三个步骤:

  • 请求脏的Layout组件通过ICanvasElement.Rebuild方法重建其布局。
  • 任何注册了Clipping 组件(例如Masks)都被要求剔除任何被裁剪组件。这是通过ClippingRegistry.Cull完成的。
  • 脏的Graphic组件被要求重建其图形元素。
    对于Layout和Graphic重建,该过程分为多个部分。Layout重建以三部分(PreLayout,Layout和PostLayout)运行,而Graphic重建以两部分运行(PreRender和LatePreRender)。

布局(Layout)重建

为了重新计算一个或多个Layout组件中包含的组件的适当位置(以及可能的大小),有必要以适当的分层次序来应用布局。靠近GameObject层次结构根部的Layout可能会改变可能嵌套在其中的任何Layout的位置和大小,因此必须首先进行计算。

为此,Unity UI根据层次结构中深度对脏的Layout组件的列表进行排序。层次结构中较高的项目(即使用较少的父Transforms)将移动到列表的前面。

然后排好序的Layout组件列表请求重建其布局;这是由布局组件控制的UI元素的位置和大小实际上被改变的地方。有关各个元素位置如何受布局影响的更多详细信息,请参阅Unity手册的UI Auto Layout部分。

Graphic重建

当重新构建Graphic组件时,Unity UI将控制传递给ICanvasElement接口的Rebuild方法。 Graphic实现了这一点,并在重建过程的PreRender阶段执行两个不同的重建步骤。

  • 如果顶点数据已被标记为脏(例如,当组件的RectTransform已改变尺寸时),则重建网格。
  • 如果材质数据已被标记为脏(例如,当组件的材质或纹理已更改),则attach的Canvas Renderer的材质将被更新。
    Graphic重建不按照任何特定顺序处理Graphic组件列表,也不需要任何排序操作。

译注:
[1]overdraw 过度绘制,相关概念:
总填充数峰值:项目运行过程中,单帧总填充像素数量的最大值;
填充倍数峰值:项目运行过程中,单帧的最大填充倍数。
单帧填充倍数 = 该帧总填充数 / 该帧渲染相机的分辨率。

注:翻译自unity官网 https://unity3d.com/cn/learn/tutorials/topics/best-practices/fundamentals-unity-ui?playlist=30089

Pingbacks are open.

Trackback URL

Comments

39 Comments

  • how to get help in windows 10
    29 May 2019, 02:59 | Reply
    My family members all the time say that I am killing my time here at web, except I know I am getting knowledge all the time by reading such good content.
  • how to get help in windows 10
    29 May 2019, 02:59 | Reply
    My family members all the time say that I am killing my time here at web, except I know I am getting knowledge all the time by reading such good content.
  • gamefly free trial
    29 May 2019, 12:06 | Reply
    I am in fact delighted to glance at this weblog posts which contains plenty of useful data, thanks for providing such data.
  • gamefly free trial
    29 May 2019, 12:06 | Reply
    I am in fact delighted to glance at this weblog posts which contains plenty of useful data, thanks for providing such data.
  • gamefly free trial
    30 May 2019, 11:35 | Reply
    Awesome blog you have here but I was curious about if you knew of any discussion boards that cover the same topics talked about in this article? I'd really love to be a part of group where I can get feed-back from other knowledgeable individuals that share the same interest. If you have any recommendations, please let me know. Cheers!
  • gamefly free trial
    30 May 2019, 11:36 | Reply
    Awesome blog you have here but I was curious about if you knew of any discussion boards that cover the same topics talked about in this article? I'd really love to be a part of group where I can get feed-back from other knowledgeable individuals that share the same interest. If you have any recommendations, please let me know. Cheers!
  • how to get help in windows 10
    30 May 2019, 11:44 | Reply
    This info is priceless. When can I find out more?
  • gamefly free trial
    30 May 2019, 14:39 | Reply
    It is the best time to make some plans for the future and it is time to be happy. I have read this submit and if I may just I desire to suggest you few interesting issues or advice. Maybe you can write next articles referring to this article. I wish to read even more issues about it!
  • gamefly free trial
    30 May 2019, 14:40 | Reply
    It is the best time to make some plans for the future and it is time to be happy. I have read this submit and if I may just I desire to suggest you few interesting issues or advice. Maybe you can write next articles referring to this article. I wish to read even more issues about it!
  • gamefly free trial
    31 May 2019, 18:34 | Reply
    Hi! I know this is kinda off topic but I'd figured I'd ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa? My site discusses a lot of the same subjects as yours and I think we could greatly benefit from each other. If you are interested feel free to send me an email. I look forward to hearing from you! Superb blog by the way!
  • gamefly free trial
    31 May 2019, 18:34 | Reply
    Hi! I know this is kinda off topic but I'd figured I'd ask. Would you be interested in exchanging links or maybe guest writing a blog post or vice-versa? My site discusses a lot of the same subjects as yours and I think we could greatly benefit from each other. If you are interested feel free to send me an email. I look forward to hearing from you! Superb blog by the way!
  • gamefly free trial
    31 May 2019, 20:22 | Reply
    Admiring the persistence you put into your website and in depth information you provide. It's great to come across a blog every once in a while that isn't the same out of date rehashed information. Excellent read! I've bookmarked your site and I'm adding your RSS feeds to my Google account.
  • gamefly free trial
    31 May 2019, 20:22 | Reply
    Admiring the persistence you put into your website and in depth information you provide. It's great to come across a blog every once in a while that isn't the same out of date rehashed information. Excellent read! I've bookmarked your site and I'm adding your RSS feeds to my Google account.
  • gamefly free trial
    02 Jun 2019, 05:58 | Reply
    I'm really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you modify it yourself? Anyway keep up the nice quality writing, it's rare to see a great blog like this one today.
  • gamefly free trial
    02 Jun 2019, 05:58 | Reply
    I'm really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you modify it yourself? Anyway keep up the nice quality writing, it's rare to see a great blog like this one today.
  • gamefly free trial
    02 Jun 2019, 17:44 | Reply
    Hey there! Do you know if they make any plugins to assist with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Appreciate it!
  • gamefly free trial
    02 Jun 2019, 17:44 | Reply
    Hey there! Do you know if they make any plugins to assist with Search Engine Optimization? I'm trying to get my blog to rank for some targeted keywords but I'm not seeing very good gains. If you know of any please share. Appreciate it!
  • gamefly free trial
    02 Jun 2019, 20:29 | Reply
    Wow! After all I got a webpage from where I be able to genuinely obtain useful data regarding my study and knowledge.
  • gamefly free trial
    02 Jun 2019, 20:29 | Reply
    Wow! After all I got a webpage from where I be able to genuinely obtain useful data regarding my study and knowledge.
  • gamefly free trial
    03 Jun 2019, 17:31 | Reply
    Ridiculous story there. What happened after? Take care!
  • gamefly free trial
    03 Jun 2019, 17:31 | Reply
    Ridiculous story there. What happened after? Take care!
  • gamefly free trial
    04 Jun 2019, 14:13 | Reply
    Everyone loves it when people get together and share thoughts. Great website, stick with it!
  • gamefly free trial
    04 Jun 2019, 17:11 | Reply
    Every weekend i used to pay a quick visit this website, for the reason that i want enjoyment, since this this website conations actually good funny material too.
  • gamefly free trial
    04 Jun 2019, 17:11 | Reply
    Every weekend i used to pay a quick visit this website, for the reason that i want enjoyment, since this this website conations actually good funny material too.
  • gamefly free trial
    05 Jun 2019, 02:45 | Reply
    Hi there mates, its fantastic article concerning cultureand completely explained, keep it up all the time.
  • gamefly free trial
    05 Jun 2019, 02:45 | Reply
    Hi there mates, its fantastic article concerning cultureand completely explained, keep it up all the time.
  • 구미출장마사지
    05 Jun 2019, 08:50 | Reply
    Have you ever thought about adding a little bit more than just your articles? I mean, what you say is fundamental and everything. Nevertheless think of if you added some great visuals or video clips to give your posts more, "pop"! Your content is excellent but with images and videos, this site could certainly be one of the best in its niche. Amazing blog!
  • gamefly free trial
    06 Jun 2019, 01:00 | Reply
    This post is worth everyone's attention. Where can I find out more?
  • gamefly free trial
    06 Jun 2019, 01:00 | Reply
    This post is worth everyone's attention. Where can I find out more?
  • gamefly free trial
    06 Jun 2019, 12:09 | Reply
    Great post however , I was wanting to know if you could write a litte more on this subject? I'd be very grateful if you could elaborate a little bit more. Appreciate it!
  • gamefly free trial
    06 Jun 2019, 12:09 | Reply
    Great post however , I was wanting to know if you could write a litte more on this subject? I'd be very grateful if you could elaborate a little bit more. Appreciate it!
  • gamefly free trial
    06 Jun 2019, 19:32 | Reply
    Amazing things here. I'm very satisfied to look your article. Thanks a lot and I'm having a look ahead to touch you. Will you please drop me a mail?
  • gamefly free trial
    06 Jun 2019, 19:32 | Reply
    Amazing things here. I'm very satisfied to look your article. Thanks a lot and I'm having a look ahead to touch you. Will you please drop me a mail?
  • gamefly free trial
    07 Jun 2019, 02:22 | Reply
    Hello! This is kind of off topic but I need some guidance from an established blog. Is it very difficult to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start. Do you have any tips or suggestions? Thank you
  • gamefly free trial
    07 Jun 2019, 02:22 | Reply
    Hello! This is kind of off topic but I need some guidance from an established blog. Is it very difficult to set up your own blog? I'm not very techincal but I can figure things out pretty fast. I'm thinking about making my own but I'm not sure where to start. Do you have any tips or suggestions? Thank you
  • gamefly free trial
    08 Jun 2019, 05:22 | Reply
    Hi, i think that i saw you visited my site thus i came to “return the favor”.I'm attempting to find things to enhance my site!I suppose its ok to use a few of your ideas!!
  • gamefly free trial
    08 Jun 2019, 05:22 | Reply
    Hi, i think that i saw you visited my site thus i came to “return the favor”.I'm attempting to find things to enhance my site!I suppose its ok to use a few of your ideas!!
  • prediksi cambodia
    13 Jun 2019, 08:28 | Reply
    Yesterday, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a 40 foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!
  • prediksi cambodia
    13 Jun 2019, 08:28 | Reply
    Yesterday, while I was at work, my cousin stole my apple ipad and tested to see if it can survive a 40 foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is entirely off topic but I had to share it with someone!
Post your comment

cancel reply