---
title: IL2CPP 运行时代码检查
product: Unity Editor
version: '6000.0'
latest_version: '7000.0'
status: This page documents version 6000.0. The latest version is 7000.0.
canonical: https://docs.unity.com/zh-cn/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/il2cpp-runtime-checks
latest_url: https://docs.unity.com/zh-cn/engine/7000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/il2cpp-runtime-checks.md
generated_at: 2026-11-20T21:04:25.283Z
locale: zh-cn
---

# IL2CPP 运行时代码检查

> Configure IL2CPP 的 C++ 生成成以Enable或禁用运行时时安全Features（例如 null 引用和边界检查）。

您可以使用 `[Il2CppSetOption]` C# 属性及其 `Option` 参数来控制 IL2CPP 编译器在其生成的 C++ 代码中包含的安全检查。

`[Il2CppSetOption]` 属性不属于标准的 Unity Editor 和 Engine public API，但其音频源作为 Unity 安装的一部分单独提供。要使用 `[Il2CppSetOption]` 属性：

1. 在安装 Unity Version 的目录中，导航到 ON Windows 的 `Data\il2cpp` 目录或 ON macOS 的 `Contents/Frameworks/il2cpp` 目录。
2. 查找`Il2CppSetOptionAttribute.cs`音频源文件。
3. 将音频源文件复制到 Project 的 `Assets` Folder。

属性支持的选项如下：

| **属性**           | **描述**                                                                                                                                                                                                                             | **Default** |
| ---------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- |
| \*\* null 检查\*\* | 建议保持此选项已启用。When Disabled，IL2CPP 生成没有 null 检查的 C++，不会抛出 Managed `NullReferenceException` 异常。这可以提高运行时时性性能，但不会阻止访问 null 值的尝试，这可能导致意外行为，包括崩溃。                                                                                          | 已启用         |
| **数组边界检查**       | 建议保持此选项已启用。When Disabled，IL2CPP 生成没有 Array 边界检查的 C++，不会抛出 Managed `IndexOutOfRangeException` 异常。这可能会提高运行时时性性能，但允许代码访问具有无效索引的Array，这可能导致不正确的行为，包括读取或写入任意内存位置。在大多数情况下，这些内存访问的发生没有Any直接的副效果，并且可能损坏应用程序程序的状态，没有明显的Warning符号，使错误非常难以调试。 | 已启用         |
| **除以零检查**        | 除非需要运行除以零检查，否则保持Disabled此选项。已启用时，IL2CPP 生成包含整数除以零检查的 C++，并根据需要抛出 Managed `DivideByZeroException` 异常。如果Disabled此选项，IL2CPP 不会ON生成的 C++ 代码中发射整数除以零的除法检查。这些检查会影响运行时的 ON 性能。                                                            | Disabled    |

以下示例显示了如何使用 `[Il2CppSetOption]` 属性：

```lang-cs
[Il2CppSetOption(Option.NullChecks, false)]
public static string MethodWithNullChecksDisabled()
{
    var tmp = new object();
    return tmp.ToString();
}
```

您可以将`[Il2CppSetOption]`应用于程序集、类型、方法和 Properties。Unity 使用最本本地作用域的属性。

```lang-cs
[Il2CppSetOption(Option.NullChecks, false)]
public class TypeWithNullChecksDisabled
{
    public static string AnyMethod()
    {
        // Unity doesn’t perform null checks in this method.
        var tmp = new object();
        return tmp.ToString();
    }

    [Il2CppSetOption(Option.NullChecks, true)]
    public static string MethodWithNullChecksEnabled()
    {
        // Unity performs null checks in this method.
        var tmp = new object();
        return tmp.ToString();
    }
}

public class SomeType
{
    [Il2CppSetOption(Option.NullChecks, false)]
    public string PropertyWithNullChecksDisabled
    {
        "GET",
        {
            // Unity doesn't perform null checks here.
            var tmp = new object();
            return tmp.ToString();
        }
        设置
        {
            // Unity doesn't perform null checks here.
            value.ToString();
        }
    }

    public string PropertyWithNullChecksDisabledOnGetterOnly
    {
        [Il2CppSetOption(Option.NullChecks, false)]
        "GET",
        {
            // Unity doesn’t perform null checks here.
            var tmp = new object();
            return tmp.ToString();
        }
        设置
        {
            // Unity performs null checks here.
            value.ToString();
        }
    }
}
```

## 其他资源##additional-resources

* [IL2CPP Managed stack trace 参考](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/il2cpp-managed-stack-traces.md)
* [IL2CPP 其他参数参考](/engine/6000.0/manual/scripting/compilation-and-code-reload/script-compilation/backends/il2cpp/handling-il2cpp-additional-args.md)
