纹理串流系统可以控制 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 设置为 LZ4 或 LZ4HC。Unity 需要使用其中一种压缩方法进行异步纹理加载,这是纹理串流系统所必需的操作。
Unity 在满足纹理__内存预算 (Memory Budget)__ 的同时以最高分辨率级别加载 Mipmap。要进行更具体的控制或微调纹理串流系统的自动化结果,请使用 C# API 为每个纹理指定 Mipmap 级别。如需了解更多详细信息,请参阅纹理串流 API。
对于纹理串流,您需要将每个纹理分配给一个 Unity 渲染器,以允许系统计算所需的 Mipmap 级别,除非脚本要求手动设置 Mip 级别(请参阅有关 Texture2D.requestedMipmapLevel 的文档)。如果未将纹理分配给渲染器(并且未设置手动请求的 Mip),则系统将无法计算要使用的 Mip。这种情况下将导致 Unity 加载 Mip 更少的纹理,这样的贴图在靠近摄像机时会显得模糊。
以下系统不使用标准渲染器,因此您应该对关联的纹理禁用纹理串流:
当 Unity 直接使用 API(例如 Graphics.DrawMeshNow)直接渲染串流纹理时,系统没有用于计算 Mip 级别的渲染器边界信息,因此您需要显式设置纹理 Mip 级别(或在此纹理上禁用纹理串流)。有关更多详细信息,请参阅有关 Texture2D.requestedMipmapLevel 的文档。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.