From 28ad02dc80ed433cd4755b4a6da5941a47ede1c7 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Sat, 7 Mar 2026 22:10:14 +0800 Subject: [PATCH] =?UTF-8?q?docs(best-practices):=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E7=AB=AF=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E7=A4=BA=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了图形 API 和多线程渲染的配置说明 - 增加了运行时可调整的性能设置选项 - 优化了纹理质量和阴影设置参数 - 添加了目标帧率和 VSync 配置 - 实现了基于设备性能的质量等级自动调整 - 改进了垃圾回收优化策略和相关注释说明 - 添加了设备性能检测的辅助方法 --- .../best-practices/mobile-optimization.md | 76 ++++++++++++++++--- 1 file changed, 64 insertions(+), 12 deletions(-) diff --git a/docs/zh-CN/best-practices/mobile-optimization.md b/docs/zh-CN/best-practices/mobile-optimization.md index c2795cf..de515d8 100644 --- a/docs/zh-CN/best-practices/mobile-optimization.md +++ b/docs/zh-CN/best-practices/mobile-optimization.md @@ -783,14 +783,25 @@ public class iOSOptimizer { public static void ApplyOptimizations() { - // 使用 Metal 渲染 - SystemInfo.graphicsDeviceType = GraphicsDeviceType.Metal; + // 注意:图形 API 和多线程渲染需要在 Player Settings 中配置 + // 在 Unity Editor 中:Edit > Project Settings > Player > Other Settings + // - Graphics APIs: 选择 Metal + // - Multithreaded Rendering: 启用 - // 优化纹理格式 + // 运行时可以调整的设置: + + // 优化纹理质量(0 = 最高质量) QualitySettings.masterTextureLimit = 0; - // 启用多线程渲染 - PlayerSettings.MTRendering = true; + // 设置目标帧率 + Application.targetFrameRate = 60; + + // 启用 VSync(0 = 关闭,1 = 每帧同步,2 = 每两帧同步) + QualitySettings.vSyncCount = 1; + + // 优化阴影质量 + QualitySettings.shadowDistance = 50f; + QualitySettings.shadowResolution = ShadowResolution.Medium; } } ``` @@ -802,17 +813,58 @@ public class AndroidOptimizer { public static void ApplyOptimizations() { - // 使用 Vulkan 或 OpenGL ES 3 - SystemInfo.graphicsDeviceType = GraphicsDeviceType.Vulkan; + // 注意:图形 API 和多线程渲染需要在 Player Settings 中配置 + // 在 Unity Editor 中:Edit > Project Settings > Player > Other Settings + // - Graphics APIs: 选择 Vulkan 或 OpenGL ES 3.0 + // - Multithreaded Rendering: 启用 - // 优化纹理格式 + // 运行时可以调整的设置: + + // 优化纹理质量 QualitySettings.masterTextureLimit = 0; - // 启用多线程渲染 - PlayerSettings.MTRendering = true; + // 设置目标帧率 + Application.targetFrameRate = 60; - // 优化 GC - GarbageCollector.GCMode = GarbageCollector.Mode.Disabled; + // 根据设备性能调整质量等级 + var devicePerformance = GetDevicePerformance(); + QualitySettings.SetQualityLevel(devicePerformance switch + { + DevicePerformance.Low => 0, + DevicePerformance.Medium => 1, + DevicePerformance.High => 2, + _ => 1 + }); + + // GC 优化建议: + // 警告:完全禁用 GC (GarbageCollector.Mode.Disabled) 在内存受限设备上有风险 + // 仅在以下情况考虑使用: + // 1. 高端设备(4GB+ RAM) + // 2. 配合自定义内存管理策略 + // 3. 经过充分测试 + + // 推荐的 GC 优化方式: + // 1. 使用增量 GC 减少卡顿 + GarbageCollector.GCMode = GarbageCollector.Mode.Enabled; + + // 2. 调整 GC 增量时间片(微秒) + GarbageCollector.incrementalTimeSliceNanoseconds = 2000000; // 2ms + + // 3. 定期在合适的时机手动触发 GC(如加载界面) + // GC.Collect(); + } + + private static DevicePerformance GetDevicePerformance() + { + var memory = SystemInfo.systemMemorySize; + var processorCount = SystemInfo.processorCount; + + if (memory < 2048 || processorCount < 4) + return DevicePerformance.Low; + else if (memory < 4096 || processorCount < 6) + return DevicePerformance.Medium; + else + return DevicePerformance.High; } } ```