Game Development
Burst compiler
Learn high-performance code with the Burst compiler.
What Is Burst Compiler?
Burst Compiler is a compiler developed by Unity that converts certain C# code into high-performance machine code. It helps improve execution speed, especially in systems with intensive mathematical operations. Burst can take advantage of processor features such as SIMD in supported code.
using Unity.Burst;
[BurstCompile]
public struct CalculationJob
{
// Calculation operations
}
To use Burst, the relevant package must be present in the project and supported code structures must be used.
The BurstCompile Attribute
You can add the BurstCompile attribute to a supported method or job structure that you want Burst to compile. This attribute allows the Burst compiler to compile the relevant code. Burst can be useful especially for mathematical operations and computation-intensive tasks.
using Unity.Burst;
public static class MathOperations
{
[BurstCompile]
public static float DoubleValue(float value)
{
return value * 2f;
}
}
Burst does not support every C# feature, so the compatibility of the code being used is important.
Exercise Create a Burst-Enabled Calculation Class
Create a static class named BurstMath. Define a static method named Multiply and add the BurstCompile attribute above the method. The method must accept two float parameters and return their product. Print the result to the console.
Show example solution
using Unity.Burst;
using UnityEngine;
public static class BurstMath
{
[BurstCompile]
public static float Multiply(float a, float b)
{
return a * b;
}
}
public class BurstMathTest : MonoBehaviour
{
void Start()
{
Debug.Log("Result: " + BurstMath.Multiply(3f, 4f));
}
}
Test yourself
-
What is the main purpose of Burst Compiler?
- To create scene objects automatically
- To compile C# code into high-performance machine code
- To edit visual materials
- To compress audio files
Show answer
To compile C# code into high-performance machine code — Burst Compiler aims to improve performance by converting supported C# code into optimized machine code.
-
Add the Burst Attribute
using Unity.Burst;
____
public struct CalculationJob
{
public float Value;
}
Show answer
using Unity.Burst;
[BurstCompile]
public struct CalculationJob
{
public float Value;
}
-
Burst Performance Concepts
- Burst Compiler
- Enables code optimization
- BurstCompile
- Is a compilation attribute
- SIMD
- Multiple data in one instruction
- Machine Code
- Code executed by the processor
Next lesson: Summary - Unity DOTS/ECS