As we push the boundaries of what’s possible in game development, performance optimization becomes increasingly crucial. One of Unity’s powerful features for optimization is code stripping, a process that reduces the final build size and improves runtime performance by removing unused code. Let’s explore how code stripping works in Unity and navigate through its different levels to understand its benefits and potential pitfalls.
What is Code Stripping?
Code stripping, also known as Managed Code Stripping or IL2CPP stripping, is Unity’s method of analyzing your game’s code to identify and remove parts of the .NET libraries and your own code that are never called. This process is crucial for keeping your build as lightweight as possible, ensuring faster load times and smoother gameplay, especially on mobile platforms where resources are limited.
How Does Code Stripping Work?
Unity uses static analysis to determine which code paths are used and which are not. During the build process, the IL2CPP (Intermediate Language To C++) converter translates the Intermediate Language code to C++ code, which is then compiled to machine code for the target platform. It’s at the IL2CPP stage where code stripping options come into play, determining the aggressiveness of the stripping.
Levels of Code Stripping in Unity
Unity offers several levels of code stripping, each providing a different balance between build size reduction and runtime safety:
Minimal: This is the least aggressive level, removing only the most obvious unused code. It’s the safest option, minimizing the risk of inadvertently breaking functionality but offering the least reduction in build size.
Low (Standard): The default setting for most Unity projects, offering a balanced approach. It strips more unused code than the minimal setting without a significant risk of removing necessary code.
Medium: This level is more aggressive, potentially resulting in smaller build sizes but increasing the risk of inadvertently stripping code that might be used through reflection or other indirect means.
High: The most aggressive level of code stripping. It significantly reduces build size but at a higher risk of runtime errors due to missing code. Recommended for advanced users who are familiar with their project’s codebase and can test thoroughly.
Potential Problems and Best Practices
While code stripping is a powerful tool for optimization, it can cause issues if the static analysis fails to recognize code that’s dynamically accessed (e.g., through reflection or callbacks). This can lead to runtime errors where the game tries to call stripped code.
To mitigate these risks:
Test thoroughly at each level of code stripping to ensure functionality remains intact.
Use link.xml files to explicitly tell Unity not to strip certain classes, methods, or assemblies you know are needed but might not be recognized by the static analysis.
Incrementally increase the stripping level, starting from minimal and testing at each stage to find the optimal balance for your project.
Where It Shines
Code stripping is particularly beneficial for mobile games, where download size can significantly impact user acquisition. It’s also valuable for any platform where memory and storage are concerns, helping to deliver a smoother, more responsive gaming experience.
Code stripping in Unity is a double-edged sword: wielded with care, it can significantly enhance your game’s performance and reduce load times; used recklessly, it can introduce hard-to-diagnose runtime errors. Understanding and leveraging the different levels of code stripping allows you to optimize your game effectively while navigating potential pitfalls.