使用 IL2CPP 脚本后端时,可以控制 il2cpp.exe 如何生成 C++ 代码。具体来说,可使用 C# 属性启用或禁用下面列出的运行时检查。
选项 | 描述 | 默认值 |
---|---|---|
Null checks | 如果启用此选项,则 IL2CPP 生成的 C++ 代码将包含 null 检查,并根据需要抛出托管的 NullReferenceException 异常。如果禁用此选项,则不会将 null 检查放入生成的 C++ 代码中。对于某些项目,禁用此选项可能会提高运行时性能。 但是,在生成的代码中对 null 值的任何访问都不会受到保护,并可能会导致不正确的行为。_通常情况下_,游戏将在取消引用 null 值后很快崩溃,但我们无法保证这一点。请谨慎禁用此选项。 |
Enabled |
Array bounds checks | 如果启用此选项,则 IL2CPP 生成的 C++ 代码将包含数组边界检查,并根据需要抛出托管的 IndexOutOfRangeException 异常。如果禁用此选项,则不会将数组边界检查放入生成的 C++ 代码中。 对于某些项目,禁用此选项可能会提高运行时性能。但是,在生成的代码中对具有无效索引的数组的任何访问都不会受到保护,并可能导致不正确的行为,包括读取或写入任意内存位置。在大多数情况下,这些内存访问在发生时不会表现出任何直接副作用,可能会悄无声息破坏游戏状态。禁用此选项要极度谨慎。 |
Enabled |
Divide by zero checks | 如果启用此选项,则 IL2CPP 生成的 C++ 代码将包含整数除法的除以零检查,并根据需要抛出托管的 DivideByZeroException 异常。如果禁用此选项,则不会将整数除法的除以零检查放入生成的 C++ 代码中。对于大多数项目,应禁用此选项。仅在需要进行除以零检查时才启用,因为这些检查具有运行时成本。 | Disable |
The runtime checks can be enabled or disabled in C# code using the Il2CppSetOptions
attribute. To use this attribute, find the Il2CppSetOptionAttribute.cs source file in the IL2CPP directory in the Unity Editor installation on your computer. (Data\il2cpp on Windows, Contents/il2cpp on OS X). Copy this source file into the Assets folder in your project.
现在按照以下示例中所示使用该属性。
[Il2CppSetOption(Option.NullChecks, false)]
public static string MethodWithNullChecksDisabled()
{
var tmp = new object();
return tmp.ToString();
}
可将 Il2CppSetOptions
属性应用于类型、方法和属性。Unity 在最局部的作用域内使用该属性。
[Il2CppSetOption(Option.NullChecks, false)]
public class TypeWithNullChecksDisabled
{
public static string AnyMethod()
{
// 在此方法中将禁用 null 检查。
var tmp = new object();
return tmp.ToString();
}
[Il2CppSetOption(Option.NullChecks, true)]
public static string MethodWithNullChecksEnabled()
{
// 此方法中将启用 null 检查。
var tmp = new object();
return tmp.ToString();
}
}
public class SomeType
{
[Il2CppSetOption(Option.NullChecks, false)]
public string PropertyWithNullChecksDisabled
{
get
{
// 此处将禁用 null 检查。
var tmp = new object();
return tmp.ToString();
}
set
{
// 此处将禁用 null 检查。
value.ToString();
}
}
public string PropertyWithNullChecksDisabledOnGetterOnly
{
[Il2CppSetOption(Option.NullChecks, false)]
get
{
//此处将禁用 null 检查。
var tmp = new object();
return tmp.ToString();
}
set
{
//此处将启用 null 检查。
value.ToString();
}
}
}
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.