Backtrace Blog
For the latest developments from our engineering team, head over to the Engineering Blog at engineering.backtrace.io.
Best Practices to Improve Performance on Mobile Builds in Unity
In a previous blog, we explored performance and out-of-memory issues arising out of WebGL builds. In this blog, we look into some of the best practices to improve performance on mobile builds in Unity.
In a previous blog, we explored performance and out-of-memory issues arising out of WebGL builds. In this blog, we look into some of the best practices to improve performance on mobile builds in Unity.
While many of the best practices outlined in the previous blog for optimizing performance on WebGL builds can be applied for mobile builds in Unity, this blog outlines some additional considerations for mobile builds.
Object Pooling
Frequent creation and destruction of objects such as prefabs and particles can cause GC to run more often and degrade performance. If your game instantiates objects frequently, such as a bullet in an update loop, try to use an Object Pooling system instead.
Regex and LINQ (LinearQuery)
As both Regex and LINQ generate garbage, you should possibly avoid these if you’d need better performance.
Arrays vs Lists
Instead of creating new arrays, you can consider using Lists and other collections and reuse them by clearing the collection instead of creating a new list (or collection) frequently. As an extension to this example, it is also imperative that you use the right data structure to improve performance especially if you iterate frequently over a for loop and/or in an Update loop.
Incremental GC
Under Player Settings in Unity, check mark on ‘Use Incremental GC’ to let Unity distribute the garbage collection process over several frames instead of one long interruption. You can use the Profiler to verify whether your app requires this feature by comparing the GC spikes before and after turning on incremental GC.
Reducing reliance on Update loops
Instead of running a code every frame, evaluate if the code can be moved out of the Update, FixedUpdate or LateUpdate loops to improve performance.
Dynamic Framerate
Instead of a fixed framerate, consider reducing the framerate in runtime when you don’t need a higher frame rate. For example, you may choose to reduce the target frame rate by using Application.targetFrameRate during the times spent in menus or game setup and increase it to a higher fps when the game enters the main gameplay.
Combining costlier operations
Consider optimizing the code by combining costlier operations. For example, if you need to spawn a weapon and parent it to one of the hands of a character and you typically do the following:
You may instead try combining both operations into one:
Likewise, you may combine operations to position and rotate with Instantiate. Additionally, if you are looking to move or rotate an object during runtime, instead of modifying the position and rotation individually, you may combine both operations by using a transform’s SetPositionAndRotation() function.
Lighting
Use of multiple real time lights can affect performance. Consider using lightmaps to bake lights and shadows for static objects. For dynamic objects, consider using light probes to store baked information about your lighting. Additionally, avoid using Reflection probes where possible and instead use cube maps to improve performance. Where possible, replace real-time shadows with fake shadows. There are assets in Unity’s AssetStore that can fake shadows and still perform very well as a result of a lower draw call.
Using Backtrace to report performance issues on mobile
While the above tips and best practices will help you in improving the performance of your next game project, you should definitely use Backtrace in your next project to identify issues that may still go undetected. Backtrace allows you to detect exceptions arising out of unoptimized code, Application Not Responding (ANRs) and Out Of Memory (OOM) on mobile devices to let you identify, troubleshoot and resolve them before they get out of hand.
Backtrace is a Unity VSP (Verified Solution Partner).
Learn more about how we work with Unity and sign up for free today: https://backtrace.io/unity/