包含此页的版本:
不含此页的版本:
此版本的 Unity 编辑器使用以下 C# 编译器和语言版本:
编辑器将一组默认选项传递给 C# 编译器。若要在项目中传递其他选项,请参阅 Unity 中的条件编译。
Unity 对 Mono 和IL2CPP Unity 开发的脚本后端,在为某些平台构建项目时,可以将其用作 Mono 的替代方案。更多信息
请参阅术语表 脚本后端在Unity 中为脚本提供支持的框架。Unity 支持三种不同的脚本后端,具体取决于目标平台:Mono、.NET 和 IL2CPP。但是,通用 Windows 平台仅支持两个:.NET 和 IL2CPP。更多信息
请参阅术语表和增量模式。有关可用的垃圾回收模式、其含义以及如何在它们之间切换的更多信息,请参阅垃圾回收模式。
如果尝试在项目中使用不受支持的功能,编译会生成错误。
C# 9 初始化和记录支持附带一些注意事项。
System.Runtime.CompilerServices.IsExternalInit是完整记录支持所必需的,因为它仅使用 init setter,但仅在 .NET 5 及更高版本中可用(Unity 不支持)。用户可以通过声明System.Runtime.CompilerServices.IsExternalInit输入他们自己的项目。Unity 支持 C# 9 中引入的非托管函数指针,但不支持可扩展的调用约定。以下示例代码提供了有关如何正确使用非托管函数指针的更多详细信息。
以下示例面向 Windows 平台,需要在玩家设置设置,用于为 Unity 构建的最终游戏设置各种特定于玩家的选项。更多信息
请参阅术语表. 若要启用它,请转到:项目设置>播放器。展开“其他设置”面板,导航到“脚本编译”部分。有关 C# 的unsafe上下文,请参阅 Microsoft 的不安全(C# 参考)文档或 Microsoft 的不安全代码、指针类型和函数指针文档。
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
}
}