기술 자료

Unity Engine


User Manual

Script Reference

Unity Engine


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

Supported built-in types

Burst supports the following built-in types:
  • bool
  • byte
    /
    sbyte
  • double
  • float
  • int
    /
    uint
  • long
    /
    ulong
  • short
    /
    ushort

Unsupported built-in types

Burst doesn't support the following built-in types:
  • char
  • decimal
  • string
    because this is a managed type

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
BC1361
.
For 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
참고
Structs with an explicit layout might generate non-optimal native code.

Supported struct layout

Burst supports the following struct layouts:
  • LayoutKind.Sequential
  • LayoutKind.Explicit
  • StructLayoutAttribute.Pack
  • StructLayoutAttribute.Size
Burst supports
System.IntPtr
and
System.UIntPtr
natively as intrinsic structs that directly represent pointers.

Generic 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.
참고
There are restrictions if you use generic jobs.

Vector types

Burst can translate vector types from
Unity.Mathematics
to native SIMD vector types with the following first class support for optimizations:
  • bool2
    /
    bool3
    /
    bool4
  • uint2
    /
    uint3
    /
    uint4
  • int2
    /
    int3
    /
    int4
  • float2
    /
    float3
    /
    float4
팁
For performance reasons, use the 4 wide types (
bool4
,
uint4
,
float4
,
int4
) over the other types.

Enum types

Supported enum types

Burst supports all enums including enums that have a specific storage type, for example,
public enum MyEnum : short
.

Unsupported enums

Burst doesn't support
Enum
methods, for example
Enum.HasFlag
.

Pointer types

Burst supports any pointer types to any Burst supported types

Span types

Burst supports
Span<T>
and
ReadOnlySpan<T>
types in the Unity Editors that support them.
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
Span
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.

Tuple types

Burst supports value tuples
ValueTuple<T1,T2>
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:
[BurstCompile]private struct MyTuple{ public int item1; public float item2;}

Additional resources