Get started with Burst
Read time 2 minutesLast updated 13 days ago
You can use Burst to compile jobs or static methods in non-job C# types. To start using the Burst compiler in your code, decorate a job or static method with the attribute.
[BurstCompile]For more information on where and when to apply the attribute, refer to Marking code for Burst compilation.
[BurstCompile]Compiling jobs with Burst
For jobs, you only need to apply the attribute to the job declaration and Burst compiles everything inside the job automatically. The following example demonstrates this:
[BurstCompile]using Unity.Burst;using Unity.Collections;using Unity.Jobs;using UnityEngine;public class MyBurst2Behavior : MonoBehaviour{ void Start() { var input = new NativeArray<float>(10, Allocator.Persistent); var output = new NativeArray<float>(1, Allocator.Persistent); for (int i = 0; i < input.Length; i++) input[i] = 1.0f * i; var job = new MyJob { Input = input, Output = output }; job.Schedule().Complete(); Debug.Log("The result of the sum is: " + output[0]); input.Dispose(); output.Dispose(); } // Using BurstCompile to compile a Job with Burst [BurstCompile] private struct MyJob : IJob { [ReadOnly] public NativeArray<float> Input; [WriteOnly] public NativeArray<float> Output; public void Execute() { float result = 0.0f; for (int i = 0; i < Input.Length; i++) { result += Input[i]; } Output[0] = result; } }}
Compiling static methods with Burst
For static methods, you must apply the attribute to both the individual methods you want Burst to compile and to the declaration of the parent type. The following example demonstrates this:
[BurstCompile]using Unity.Burst;using Unity.Collections;using Unity.Jobs;using UnityEngine;[BurstCompile]public static class MyBurstUtilityClass{ [BurstCompile] public static void BurstCompiled_MultiplyAdd(in float4 mula, in float4 mulb, in float4 add, out float4 result) { result = mula * mulb + add; }}
For more information on how you can call this Burst-compiled utility class and its member method from your C# code, refer to Calling Burst-compiled code.
Limitations
Burst supports most C# expressions and statements, with a few exceptions. For more information, refer to C# language support.
Compilation
Burst compiles your code just-in-time (JIT) while in Play mode in the Editor, and ahead-of-time (AOT) when your application runs in a Player. For more information on compilation, refer to Burst compilation
Command line options
You can pass the following options to the Unity Editor on the command line to control Burst:
- disables Burst.
--burst-disable-compilation - force Burst to compile synchronously.
--burst-force-sync-compilation
For more information, refer to Burst compilation.