Unity官方原文https://learn.unity.com/tutorial/optimizing-unity-ui#5c7f8528edbc2a002053b59f
1.A guide to optimizing Unity UI
Optimizing a user interface driven by Unity UI is an art. Hard-and-fast rules are rare; instead, each situation must be carefully evaluated with the system’s behavior in mind. The core tension when optimizing any Unity UI is the balancing of draw calls with batching costs. While some common-sense techniques can be used to reduce one or the other, complex UIs must make trade-offs.
优化Unity的UI是一门艺术,很少有硬性的规则。而是需要根据系统行为进行仔细地评估。核心问题是如何
平衡DrawCalls和Batch的处理成本。虽然有一些常见的技术手段去减少一种或者另一种的处理成本,但是对于复杂的UI必须进行权衡。
However, as is best practice elsewhere, attempts to optimize Unity UIs should begin with profiling. The primary task before attempting to optimize a Unity UI system is to locate the precise reason for an observed performance problem. There are four common classes of problems encountered by users of Unity UI:
- Excessive GPU fragment shader utilization (i.e. fill-rate overutilization)
- Excessive CPU time spent rebuilding a Canvas batch
- Excessive numbers of rebuilds of Canvas batches (over-dirtying)
- Excessive CPU time spent generating vertices (usually from text)
就像优化其它方面一样,UI的优化也要先从分析开始。首要任务是定位产生明显性能问题的确切原因。大多数用户经常遇到的问题可以归为4类:
- GPU执行了过多的片源着色器(即填充率过度使用,填充率的理解:https://blog.uwa4d.com/archives/fillrate.html)
- CPU花费了大量时间用于合批Canvas(我的理解是单一的Canvas上元素过多)
- 合批Canvas的次数过多(我的理解是Canvas上的元素频繁变化)
- CPU花费了大量时间生成定点(通常是文本)
It is, in principle, possible to create a Unity UI whose performance is constrained by the sheer number of draw calls being sent to the GPU. However, in practice, any project overloading the GPU with draw calls is more likely to be bound by fill-rate overutilization.
原则上,一个UI的性能受到发送给GPU的DrawCalls数量限制。但是实际运用中GPU因DrawCalls导致超负荷原因可能在于填充率的过度使用。
This guide will discuss the fundamental concepts, algorithms and code underlying Unity UI as well as discussing common problems and solutions. It is broken into five chapters:
- The Fundamentals of Unity UI chapter defines terminology specific to Unity UI and discusses the details of many of the fundamental processes performed to render the UI, including the building of batched geometry. It is strongly recommended that readers begin with this chapter.
- The Unity UI profiling tools chapter discusses gathering profiling data with the various tools available to developers.
- The