Mipmap 串流系统可以控制 Unity 加载到内存中的 Mipmap 级别。该系统可减少 Unity 对于纹理所需的内存总量,因为它只加载 Unity 在场景中渲染当前摄像机位置时所需的 Mipmap,而不是默认加载所有 Mipmap。该系统通过消耗少量 CPU 资源来节省潜在的大量 GPU 内存。
您还可以使用 Memory Budget(内存预算) 为项目中使用的所有纹理设置一个总内存限制。Mipmap 串流系统会自动降低 Mipmap 级别来确保不超出此预算范围。
您可以使用 Mipmap 串流系统 API 为特定纹理请求特定的 Mipmap 级别。Unity 提供了 C# 代码示例,其中会重现 Mipmap 选择方法的引擎逻辑;您可以使用此引擎逻辑来覆盖您自己项目中的引擎逻辑。有关更多详细信息,请参阅 Mipmap 串流 API。
在 Unity 的 Viking Village 演示项目中,Mipmap 串流可以节省 25–30% 的纹理内存,具体数值取决于摄像机位置。
要启用 Mipmap 串流,请选择 Unity 的 Quality Settings (Edit > Project Settings > Quality),然后启用 Texture Streaming 复选框。此时将显示 Mipmap 串流系统特有的设置。如需了解每项设置的详细信息,请参阅 Quality Settings 的文档。
完成此操作后,请在各个纹理上设置 Mipmap 串流,从而让 Mipmap 串流系统将每个纹理的 Mipmap 从磁盘流式传输到内存中。为此,请选择要应用 Mipmap 串流的纹理,导航到 Inspector 窗口,并查看纹理导入设置。打开 Advanced 设置,并启用 Streaming Mip Maps 复选框。
如果是进行 Android 开发,还需要打开 Build Settings 并将 Compression Method 设置为 LZ4 或 LZ4HC。Unity 需要使用其中一种压缩方法进行异步纹理加载,这是 Mipmap 串流系统所必需的操作。
Unity 在满足纹理 Memory Budget(内存预算) 的同时以最高分辨率级别加载 Mipmap。要进行更具体的控制或微调 Mipmap 串流系统的自动化结果,请使用 C# API 为每个纹理指定 Mipmap 级别。有关更多详细信息,请参阅 Mipmap 串流 API。
您可以指示 Mipmap 串流使用以下方法之一计算所需的 Mipmap 级别:
如果您不指示 Mipmap 串流使用这些方法之一计算 Mipmap 级别,Unity 将无法计算要使用的 Mip 级别。这会导致 Unity 使用看起来模糊的低质量 Mip 加载纹理。
The following systems don’t use standard Renderers. This means you must manually set the requested mips for these systems or Unity loads the textures they use at low resolution:
当渲染器处于活动状态时,渲染器使用的网格需要有效的 UV 分布指标来计算所需的 Mipmap 级别。作为网格导入过程的一部分,Unity 会自动计算分布指标。您也可以在脚本中使用 Mesh.GetUVDistributionMetric 进行计算。
If you create a mesh from code, Unity does not calculate distribution metrics automatically. This causes Unity to load the wrong mipmaps. For example, it may load low-resolution textures when the camera is close to a mesh. To manually trigger the UV distribution metric calculation use Mesh.RecalculateUVDistributionMetrics.
When Unity renders a streamed Texture directly with an API (such as Graphics.DrawMeshNow the system has no renderer bounds or other information to calculate the mip level. This means you need to set the Texture mip level manually or disable Mipmap Streaming on this Texture. See Texture2D.requestedMipmapLevel for more details on how to manually set which mip level Unity loads.
When Unity calculates the miplevel for a texture it looks for the scale and translation of that texture in a _ST
value with the same name as the texture flagged in your shader. For example, if you reference your textures are in the shader using _MainTex
, Unity will look for _MainTex_ST
.
Unity 具有内置的 Mipmap 串流调试视图模式。要访问此模式,请单击 Scene 视图控制下拉选单,然后选择 Texture Streaming。根据游戏对象在 Mipmap 串流系统中的状态,此视图模式将其显示为以下颜色:
您还可以使用调试 API 编写您自己的自定义调试工具。
Important : When the main texture is set using the MainTexture attribute, it is not visible in the game view when you use the texture streaming debugging view mode or a custom debug tool.
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.