This version of the Unity Editor uses the following C# compiler and language version:
The Editor passes a default set of options to the C# compiler. To pass additional options in your project, refer to Conditional compilation in Unity.
Unity uses the Boehm-Demers-Weiser garbage collector for both the Mono and IL2CPPA Unity-developed scripting back-end which you can use as an alternative to Mono when building projects for some platforms. More info
See in Glossary scripting backendsA framework that powers scripting in Unity. Unity supports three different scripting backends depending on target platform: Mono, .NET and IL2CPP. Universal Windows Platform, however, supports only two: .NET and IL2CPP. More info
See in Glossary and incremental mode by default. For more information on the available garbage collection modes, their meaning, and how to switch between them, refer to Garbage collection modes.
If you try to use unsupported features in your project, compilation generates errors.
C# 9 init and record support comes with a few caveats.
System.Runtime.CompilerServices.IsExternalInit
is required for full record support as it uses init only setters, but is only available in .NET 5 and later (which Unity doesn’t support). Users can work around this issue by declaring the System.Runtime.CompilerServices.IsExternalInit
type in their own projects.Unity supports unmanaged functions pointers as introduced in C# 9, but it doesn’t support extensible calling conventions. The following example code provides more detailed information about how to correctly use unmanaged function pointers.
The following example targets Windows platforms and requires Allow ‘unsafe’ Code to be enabled in the Player SettingsSettings that let you set various player-specific options for the final game built by Unity. More info
See in Glossary. To enable it, go to: Project Settings > Player. Expand the Other Settings panel, navigate to the Script Compilation section. For more information on C#’s unsafe
context, refer to Microsoft’s unsafe (C# Reference) documentation or Microsoft’s Unsafe code, pointer types, and function pointers documentation.
using System;
using System.Runtime.InteropServices;
using UnityEngine;
public class UnmanagedFunctionPointers : MonoBehaviour
{
[DllImport("kernel32.dll")]
static extern IntPtr LoadLibrary(string lpLibFileName);
[DllImport("kernel32.dll")]
static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
// You must enable "Allow 'unsafe' code" in the Player Settings
unsafe void Start()
{
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
// This example is only valid on Windows
// Get a pointer to an unmanaged function
IntPtr kernel32 = LoadLibrary("kernel32.dll");
IntPtr getCurrentThreadId = GetProcAddress(kernel32, "GetCurrentThreadId");
// The unmanaged calling convention
delegate* unmanaged[Stdcall]<UInt32> getCurrentThreadIdUnmanagedStdcall = (delegate* unmanaged[Stdcall]<UInt32>)getCurrentThreadId;
Debug.Log(getCurrentThreadIdUnmanagedStdcall());
#endif
}
}