When building your playable experience in Unity, and C#, it is important to keep in mind that the end result will be running in HTML5 on a web browser. As such, optimising the performance as much as possible will result in higher-performing user experiences, with better device support and ultimately, ad performance.
We've assembled a number of examples and tips which will help you identify and optimise your C# code for better runtime performance on the Luna engine.
arrays instead of
lists where possible.
An example is using
LINQ in your game to loop through lists, as this can result in performance issues if this process is repetitive. Generally avoid using LINQ loops during the gameplay to iterate through lists.
Although this operation is usually fine on start, it can be expensive during the gameplay.
List<>.FindAll() methods are really expensive, we have seen these used and can cause performance lags, especially when comparing enums as this adds additional overhead.
Where ever possible try to cache references, reducing these calls eliminates the need to:
• Search the list.
• Compare the elements multiple times.
Thus improving performance.
On top of caching using a regular
for loop to iterate an array is much more efficient method than using LINQ to loop through lists. So if you have to iterate through, use
arrays and use regular
for loops where possible.
.x is a property which must be accessed.
One of the most common use cases for allocations is passing structs in/out of methods.
For example, a heavy Triangle structure being passed into a method: