C#/.NET type support
읽는 시간 3분최근 업데이트: 5일 전
Burst works on a subset of .NET that doesn't let you use any managed objects or reference types in your code (classes in C#).
The following sections give more details about the constructs that Burst supports, and any limitations they have.
- Built-in types
- Array types
- Struct types
- Generic types
- Vector types
- Enum types
- Pointer types
- Span types
- Tuple types
Built-in types
Supported built-in types
Burst supports the following built-in types:
bool- /
bytesbyte doublefloat- /
intuint - /
longulong - /
shortushort
Unsupported built-in types
Burst doesn't support the following built-in types:
chardecimal- because this is a managed type
string
Array types
Supported array types
Burst supports read-only managed arrays loaded from static read-only fields:
[BurstCompile]public struct MyJob : IJob { private static readonly int[] _preComputeTable = new int[] { 1, 2, 3, 4 }; public int Index { get; set; } public void Execute() { int x = _preComputeTable[0]; int z = _preComputeTable[Index]; }}
However, accessing a static read-only managed array has the following restrictions:
- You can only use the read-only static managed array directly and can't pass it around, for example as a method argument.
- C# code that doesn't use jobs shouldn't modify the read-only static array's elements. This is because the Burst compiler makes a read-only copy of the data at compilation time.
- Multi-dimensional arrays aren't supported.
If you've used an unsupported static constructor, Burst produces the error .
BC1361For more information on how Burst initializes arrays, see Static readonly fields and static constructors.
Unsupported array types
Burst doesn't support managed arrays. Instead, use a native container such as .
NativeArray<T>Struct types
Supported structs
Burst supports the following structs:
- Regular structs with any field with supported types
- Structs with fixed array fields
Supported struct layout
Burst supports the following struct layouts:
LayoutKind.SequentialLayoutKind.ExplicitStructLayoutAttribute.PackStructLayoutAttribute.Size
Burst supports and natively as intrinsic structs that directly represent pointers.
System.IntPtrSystem.UIntPtrGeneric types
Burst supports generic types used with structs. It supports full instantiation of generic calls for generic types that have interface constraints, for example when a struct with a generic parameter needs to implement an interface.
Vector types
Burst can translate vector types from to native SIMD vector types with the following first class support for optimizations:
Unity.Mathematics- /
bool2/bool3bool4 - /
uint2/uint3uint4 - /
int2/int3int4 - /
float2/float3float4
Enum types
Supported enum types
Burst supports all enums including enums that have a specific storage type, for example, .
public enum MyEnum : shortUnsupported enums
Burst doesn't support methods, for example .
EnumEnum.HasFlagPointer types
Burst supports any pointer types to any Burst supported types
Span types
Burst supports and types in the Unity Editors that support them.
Span<T>ReadOnlySpan<T>You can only use span types in Burst jobs or function-pointers, but not across the interface to them. This is because in C#'s implementation of the span types it supports taking spans into managed data types (like a managed array). For example, the following code is invalid:
[BurstCompile]public static void SomeFunctionPointer(Span<int> span) {}
This is because is used across the managed and Burst boundary. In Burst, span types respect any safety check setting, and only perform performance-intensive checks when safety checks are enabled.
SpanTuple types
Burst supports value tuples in Burst-compiled jobs or static methods, but not across the interface to them. This is because value tuples are of struct layout LayoutKind.Auto. Burst does not support LayoutKind.Auto (to see a list of struct layouts Burst supports see the section Struct types). However, one can use a regular struct to emulate a tuple like so:
ValueTuple<T1,T2>[BurstCompile]private struct MyTuple{ public int item1; public float item2;}