How to Optimize Game for Mobile in Unity: Best Practices
To optimize a game for mobile in
Unity, reduce draw calls by using static batching and dynamic batching, lower texture sizes, and limit real-time lights. Also, use Profiler to find bottlenecks and adjust quality settings for smooth performance on mobile devices.Syntax
Here are key Unity features and settings to optimize mobile games:
- Static Batching: Combine static objects to reduce draw calls.
- Dynamic Batching: Combine small moving objects automatically.
- Quality Settings: Adjust texture resolution, shadows, and effects.
- Profiler: Tool to measure CPU, GPU, and memory usage.
- Lightmapping: Bake lighting to textures to reduce real-time calculations.
csharp
using UnityEngine; public class MobileOptimization : MonoBehaviour { void Start() { // Enable static batching StaticBatchingUtility.Combine(gameObject); // Adjust quality settings for mobile QualitySettings.shadowDistance = 15f; // Lower shadow distance QualitySettings.masterTextureLimit = 1; // Reduce texture quality // Disable real-time shadows foreach (var light in FindObjectsOfType<Light>()) { light.shadows = LightShadows.None; } } }
Example
This example script shows how to reduce shadows and texture quality at game start to improve mobile performance.
csharp
using UnityEngine; public class OptimizeForMobile : MonoBehaviour { void Start() { // Lower shadow distance QualitySettings.shadowDistance = 10f; // Reduce texture quality (0 = full res, 1 = half res, etc.) QualitySettings.masterTextureLimit = 1; // Disable all real-time shadows foreach (Light light in FindObjectsOfType<Light>()) { light.shadows = LightShadows.None; } Debug.Log("Mobile optimization applied: shadows off, textures reduced."); } }
Output
Mobile optimization applied: shadows off, textures reduced.
Common Pitfalls
Common mistakes when optimizing for mobile include:
- Using high-resolution textures that consume too much memory.
- Leaving many real-time lights and shadows enabled, causing slow frame rates.
- Ignoring draw calls by not batching static objects.
- Not profiling the game to find actual performance bottlenecks.
Always test on real devices and use Unity Profiler to guide optimizations.
csharp
/* Wrong approach: High texture quality and many shadows */ QualitySettings.masterTextureLimit = 0; // Full resolution textures foreach (Light light in FindObjectsOfType<Light>()) { light.shadows = LightShadows.Hard; } /* Right approach: Lower texture quality and disable shadows */ QualitySettings.masterTextureLimit = 2; // Lower resolution textures foreach (Light light in FindObjectsOfType<Light>()) { light.shadows = LightShadows.None; }
Quick Reference
- Use Static Batching for static objects to reduce draw calls.
- Lower texture resolution in Quality Settings.
- Disable or limit real-time shadows.
- Use lightmapping to bake lighting.
- Profile regularly with Unity Profiler.
- Optimize scripts to avoid heavy computations every frame.
Key Takeaways
Reduce draw calls by using static and dynamic batching.
Lower texture sizes and disable real-time shadows to save memory and processing power.
Use Unity Profiler to identify and fix performance bottlenecks.
Bake lighting with lightmaps instead of using real-time lights.
Test optimizations on actual mobile devices for best results.