Version: 2019.2
优化着色器加载时间
串流控制器 (Streaming Controller)

纹理串流 (Texture Streaming)

纹理串流系统可以控制 Unity 加载到内存中的 Mipmap 级别。该系统可减少 Unity 对于纹理所需的内存总量,因为它只加载 Unity 在场景中渲染当前摄像机位置时所需的 Mipmap,而不是默认加载所有 Mipmap。该系统通过消耗少量 CPU 资源来节省潜在的大量 GPU 内存。

借助于纹理系统,还可以使用__内存预算 (Memory Budget)__ 为项目中使用的所有纹理设置一个总内存限制。纹理串流系统会自动降低 Mipmap 级别来确保不超出此预算范围。

可以使用纹理系统 API 为特定纹理请求特定的 Mipmap 级别。Unity 提供了 C# 代码示例,其中会重现 Mipmap 选择方法的引擎逻辑;您可以使用此引擎逻辑来覆盖您自己项目中的引擎逻辑。如需了解更多详细信息,请参阅纹理串流 API

在 Unity 的 Viking Village 演示项目中,纹理串流可以节省 25–30% 的纹理内存,具体数值取决于摄像机位置。

纹理串流设置

要启用纹理串流,请选择 Unity 的 Quality Settings (Edit > Project Settings > Quality),然后启用 Texture Streaming 复选框。此时将显示纹理串流系统特有的设置。如需了解每项设置的详细信息,请参阅 Quality Settings 的文档。

完成此操作后,请在各个纹理上设置纹理串流,从而让纹理串流系统将每个纹理的 Mipmap 从磁盘串流到内存中。为此,请选择要应用纹理串流的纹理,导航到 Inspector 窗口,并查看纹理导入设置。打开 Advanced 设置,并启用 Streaming Mip Maps 复选框。

如果是进行 Android 开发,还需要打开 Build Settings 并将 Compression Method 设置为 LZ4LZ4HC。Unity 需要使用其中一种压缩方法进行异步纹理加载,这是纹理串流系统所必需的操作。

Unity loads mipmaps at the highest resolution level possible while observing the Texture Memory Budget. For more specific control, or to fine-tune the Texture Streaming system’s automatic results, use the C# API to specify mipmap levels for each Texture. For more details, see Texture Streaming API.

Texture Streaming limitations

For Texture Streaming, you need to assign each Texture to a Unity Renderer to allow the system to calculate the required mip map level, unless a script requests manual mip levels (see documentation on Texture2D.requestedMipmapLevel). If you do not assign a Texture to a Renderer (and do not set a manually requested mip), the system cannot calculate what mip to use. This results in Unity loading the Texture with reduced mips, which look blurry when close to the Camera.

The following systems don’t use standard Renderers, so you should disable Texture Streaming on Textures associated with them:

  • Decal projector Textures.
  • Reflection probe Textures. These are related to different Material properties at lower mips, so it doesn’t make sense to stream them.
  • Textures in Unity’s Terrain system. Unity does not support Texture Streaming on Terrain Textures, because the system usually blends these Textures over the whole Terrain, so they should stay on the GPU.
  • Systems which have complex Texture blending in the Shader.

When Unity renders a streamed Texture directly with an API (such as Graphics.DrawMeshNow) the system has no renderer bounds information to calculate the mip level, so you need to set the Texture mip level explicitly (or disable Texture streaming on this Texture). See documentation on Texture2D.requestedMipmapLevel for more details.

优化着色器加载时间
串流控制器 (Streaming Controller)