Since its creation, mobile gaming has been booming, and developers are constantly seeking to improve it. The two first must be suited adequately to run on any kind of device in which they are going to be installed, and this will thus depend on the size of the game and how much it uses its memory.
This is the article to teach you how to develop your Unity mobile games. In this, using a few tips and tricks, you may get your game to run much faster; these will make the games more enjoyable for players. You will also be able to play well on different devices.
Key Takeaways
- Learn about optimization in mobile game development for better performance and user experiences.
- Learn the techniques of optimization with best practices for reducing the size of the game at build-time, which leads to faster downloads and higher installations.
- Understand ways of optimizing memory to enhance responsiveness and prevent crashes on low-end devices as well.
- Overcome the limitations and constraints in a device with optimal optimization methods.
- Use Unity’s tools and features to make optimization smoother and optimize results.
Why Optimization is Critical in Mobile Game Development
Optimization happens to be an important factor in the fast-paced world of mobile game development. Since the devices change with varied capabilities, it is important to make sure that your game works well on many devices which is desirable to have a great experience for all players.
Improving both Performance and User Experience
Optimize for performance: Such is an important thing. Users can simply do various things because this is really so smooth running even on those with poor hardware.
Apart from that, one would simply enjoy playing more of this game. Moreover, one can play this game more than usual as it is now easier to play on a single device.
Overcoming Device Limitations and Constraints
Mobile devices come with their special set of issues for game developers. Good optimization helps you run past those problems. Your game will run perfectly on most devices, making for a fantastic experience.
| Key Optimization Considerations | Impact on Mobile Game Development |
|---|---|
| Build Size Reduction | Enables faster downloads and smoother installation processes, improving user engagement and retention. |
| Memory Usage Optimization | Ensures seamless performance and responsiveness, even on devices with limited hardware resources. |
| Asset Management and Optimization | Reduces the overall footprint of the game, allowing for efficient resource utilization and enhanced user experience. |
| Rendering and Graphics Optimization | Delivers visually stunning experiences while maintaining high frame rates and smooth gameplay, regardless of device capabilities. |
Optimizing Mobile Games in Unity
As mobile game developers, we know how crucial it is to optimize Unity projects. One key area is reducing the game’s build size. This affects download times and performance on different mobile devices. Here, we’ll look at ways to achieve this goal.
1. Optimizing Assets
Assets like textures, audio, and animations typically encompass the bulk of your game’s size. Optimizing such assets reduces the build size significantly without impacting the quality significantly.
1.1 Compress Textures
Textures generally constitute a significant chunk of a game build. Use the texture compression formats ETC2, ASTC, or PVRTC to bring down their size dependent on different mobiles.
- ETC2: Implemented by nearly all modern Android devices, so the balance between good quality with a compact size is achieved in any case.
- ASTC: Might have significantly better compression but potentially not in every device type.
- PVRTC: Good choice for iOS but can introduce artifacts when not used well.
You can alter texture compression settings if you select the texture in Unity and inside the inspector select one of these formats from Compression.
1.2 Texture Mipmaps
Mipmaps are pre-calculated, optimized textures that reduce rendering load. They do, however, increase build size. If you don’t need mipmaps, for example because your elements are UI or small objects, turn off mipmaps to save space.
- De-select Generate Mip Maps in the import settings of the texture to switch it off.
1.3 Reducing Texture Resolution
High-resolution textures are often overkill for a mobile game. Reducing the resolution dramatically reduces the size without noticeably affecting the visual quality.
- Reduce Max Size in the Inspector window for textures to lower values, such as from 2048 to 1024.
1.4 Texture Atlasing
One combines several small textures into one to minimize the number of draw calls and memory usage. Of particular use for sprites is Unity’s Sprite Packer; these will group similar sprites together on one texture.
1.5 Audio Compression and Format
Audio files are enormous. Use compressed formats like Ogg Vorbis, not WAV or MP3.
- Set Load Type to Streaming or Compressed In Memory for audio tracks that run longer than about 30 seconds. That includes background music and the like.
- Compress short SFX, so they take up less space, and set them to Decompress on Load so they are accessed more quickly.
2. Removing Unused Assets
All assets referenced in the final build are included, even those you probably don’t use. Finding and eliminating unused assets can cut your build significantly in size.
2.1 Build Report Tools
Unity’s Build Report is another good indicator of what may be causing your build size to inflate. To turn this on, go to File > Build Settings, and then when the game is built, the resulting Editor.log will detail the size of your assets.
- Unnecessary assets should be removed or, where possible, compress the assets that utilize most of the space.
2.2 Accessible Assets
Unity’s Addressable Asset System lets you load them on the fly and not include them in the initial build; this way, only the assets required for the features that the user wants are downloaded.
3. Code Optimisation
Code that runs well will generally result in better memory management and performance, especially on mobile.
3.1 Object Pooling
Object pooling is a technique that reuses existing objects rather than constantly creating and destroying, which saves memory allocation and garbage collection overhead. This is especially useful for frequently instantiated objects like bullets or enemies in a mobile game.
public class ObjectPool : MonoBehaviour
{
public GameObject prefab;
private Queue<GameObject> pool = new Queue<GameObject>();
public GameObject GetFromPool()
{
if (pool.Count > 0)
{
GameObject obj = pool.Dequeue();
obj.SetActive(true);
return obj;
}
return Instantiate(prefab);
}
public void ReturnToPool(GameObject obj)
{
obj.SetActive(false);
pool.Enqueue(obj);
}
}
3.2 Minimize Garbage Collection
Too much garbage collection can cause bad performance issues. Avoid unnecessary memory allocations, reuse objects over and over. Never use a new keyword too much or even in Update() and similar frequently called functions.
3.3 IL2CPP and .NET Stripping
Using IL2CPP instead of Mono when building for mobile may produce smaller and more optimized code. Enabling Managed Code Stripping will also remove unused code and reduce the final build size even further.
- Open Project Settings > Player > Other Settings and make Scripting Backend to IL2CPP.
- Now set Managed Stripping Level to Low or Medium for a moderate stripping.
4. Memory Usage Optimization
For mobile, memory usage efficiency is highly relevant since normally RAM is a bottleneck. Here are a few optimisation strategies that can be employed to control the memory footprint.
4.1 Minimizing Memory Use by Textures and Audio
Once you load a texture or audio file into the memory, it occupies much more memory space than when stored compressed within your build. So use the compressed formats wherever possible and load the big assets only when needed and not waste memory.
4.2 Unloading Unused Assets
Unity provides Resources.UnloadUnusedAssets(), which unloads all the assets that are not in use, in order to free up some memory. This can be very useful when moving from one scene to another.
void LoadNewScene()
{
SceneManager.LoadScene("NextScene");
StartCoroutine(UnloadUnusedAssets());
}
IEnumerator UnloadUnusedAssets()
{
yield return Resources.UnloadUnusedAssets();
}
4.3 Asset Bundles
Asset Bundles or Addressable Assets are used in the code to load and unload assets dynamically, so that only assets needed will be fetched into memory, reducing the overall memory consumption in total.
5 Reduce Build Size: More Techniques
There are other techniques that don’t depend on assets and code alone to shrink the Unity build size.
5.1 Remove Unused Features of the Engine
There are lots of features and libraries in Unity that you probably will never require for your game. Unused parts of your project decrease its build size.
- In Player Settings, you have a Strip Engine Code checkbox to remove unused engine functionality.
5.2 Shader Variants Reduction
Shaders can easily be the biggest contributors to build size when there are lots of variants. Shader Variant Collections let you just compile the shader variants you are really using in your game.
- Use Graphics Settings to precompile shaders that are needed and remove any unused shader variants.
5.3 Compression and Build Settings
Use compression methods for reducing the final build size. Unity provides the following compression options according to your target platform:
- LZ4HC : Best compression option to reduce size but may affect loading time.
- LZ4 : Renders the ideal balance between size and speed in loading.
- Go to Player Settings > Publishing Settings and select Compress Method as LZ4HC.
Conclusion
Optimizing a mobile game in Unity involves achieving holistic balance between build size, memory usage, and performance. With asset compression, proper memory management and stripping of unused components, your game will be delivered as a lightweight, smooth-performing application running beautifully on most mobile devices. By implementing these strategies, your game will most assuredly remain optimal for the user while still controlling file size.