IL2CPP 运行时代码检查
Configure IL2CPP 的 C++ 生成成以Enable或禁用运行时时安全Features(例如 null 引用和边界检查)。
阅读时间3 分钟最后更新于 10 天前
您可以使用 C# 属性及其 参数来控制 IL2CPP 编译器在其生成的 C++ 代码中包含的安全检查。
[Il2CppSetOption]Option[Il2CppSetOption][Il2CppSetOption]- 在安装 Unity Version 的目录中,导航到 ON Windows 的 目录或 ON macOS 的
Data\il2cpp目录。Contents/Frameworks/il2cpp - 查找音频源文件。
Il2CppSetOptionAttribute.cs - 将音频源文件复制到 Project 的 Folder。
Assets
属性支持的选项如下:
属性 | 描述 | Default |
|---|---|---|
| ** null 检查** | 建议保持此选项已启用。When Disabled,IL2CPP 生成没有 null 检查的 C++,不会抛出 Managed | 已启用 |
| 数组边界检查 | 建议保持此选项已启用。When Disabled,IL2CPP 生成没有 Array 边界检查的 C++,不会抛出 Managed | 已启用 |
| 除以零检查 | 除非需要运行除以零检查,否则保持Disabled此选项。已启用时,IL2CPP 生成包含整数除以零检查的 C++,并根据需要抛出 Managed 这些检查会影响运行时的 ON 性能。 | Disabled |
以下示例显示了如何使用 属性:
[Il2CppSetOption][Il2CppSetOption(Option.NullChecks, false)]public static string MethodWithNullChecksDisabled(){ var tmp = new object(); return tmp.ToString();}
您可以将应用于程序集、类型、方法和 Properties。Unity 使用最本本地作用域的属性。
[Il2CppSetOption][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(); } }}