スタートガイド
Burst は、主に Unity の Job System と連携することを目的に設計されています。Burst コンパイラーをコードで使用するには、ジョブ構造体 を [BurstCompile]
属性で修飾します。[BurstCompile]
属性を Burst でコンパイルしたい型と静的メソッドに追加します。
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();
}
// Burst でジョブをコンパイルするために BurstCompile を使用します
[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;
}
}
}
制限事項
Burst では、わずかな例外を除き、C# の式およびステートメントのほとんどがサポートされています。詳細については、C# 言語のサポート を参照してください。
コンパイル
Burst は、エディターでの再生モード中はコードの 実行時 (JIT) コンパイルを行い、プレイヤーでアプリケーションを実行する場合は 事前 (AOT) コンパイルを行います。コンパイルの詳細については、Burst コンパイル を参照してください。
コマンドラインオプション
コマンドラインで Unity エディターに以下のオプションを渡すことにより、Burst を制御できます。
--burst-disable-compilation
: Burst を無効にします。--burst-force-sync-compilation
: Burst に同期コンパイルを強制します。詳細については、Burst コンパイル を参照してください。