Building a project using IL2CPP

要在项目中使用 IL2CPP, 打开 Build Settings. 选择你要发布的平台, 点击 Player Settings 打开 Player settings属性面板

在使用IL2CPP脚本后端时,可以控制il2cpp.exe生成c++代码的方式。特别地,也可以使用下列中的c#属性用来启用或禁用runtime check。

OptionDescriptionDefault
Null checks如果这个打开(enable),则通过IL2CPP生成的代码会进行null检查,并抛出托管空引用异常 managed NullReferenceException exceptions. 如果此选项被禁用,null检查不会发送到生成的c++代码中。 对于某些项目,禁用此选项可以提高运行时性能。
但是,对生成代码中的空值的任何访问都不会被检查,并可能导致不正确的行为 通常,在空值被解除引用后,游戏很快就会崩溃, 请小心禁用此选项
Enabled
Array bounds checks如果启用此选项,由IL2CPP生成的c++代码将检查数组边界是否越界,并在必要时抛出托管的IndexOutOfRangeException异常,如果禁用此选项生成的c++代码中则不会进行数组边界检查。
同上
Enabled
Divide by zero checks除以0检查,同上,一般在写代码的时候,就会注意这一点Disable

.在c#代码中,如果要开始或者禁用以上option,要使用IL2CppSetOptions属性。要使用这个属性,要把Il2CppSetOptionsAttribute.cs脚本拷贝到你的asset文件夹下,在editor->data(Windows上的Data\il2cpp, OS X上的Contents/Frameworks/il2cpp)中

比如:下面没有开启nullcheck检查

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

可以将Il2CppSetOptions属性应用于类型、方法和属性,

[Il2CppSetOption(Option.NullChecks, false)]
public class TypeWithNullChecksDisabled
{
    public static string AnyMethod()
    {
        // Null checks will be disabled in this method.
        var tmp = new object();
        return tmp.ToString();
    }

    [Il2CppSetOption(Option.NullChecks, true)]
    public static string MethodWithNullChecksEnabled()
    {
        // Null checks will be enabled in this method.
        var tmp = new object();
        return tmp.ToString();
    }
}
public class SomeType
{
    [Il2CppSetOption(Option.NullChecks, false)]
    public string PropertyWithNullChecksDisabled
    {
        get
        {
            // Null checks will be disabled here.
            var tmp = new object();
            return tmp.ToString();
        }
        set
        {
            // Null checks will be disabled here.
            value.ToString();
        }
    }

    public string PropertyWithNullChecksDisabledOnGetterOnly
    {
        [Il2CppSetOption(Option.NullChecks, false)]
        get
        {
            // Null checks will be disabled here.
            var tmp = new object();
            return tmp.ToString();
        }
        set
        {
            // Null checks will be enabled here.
            value.ToString();
        }
    }
}

 

How IL2CPP works

在使用IL2CPP发布时, Unity自动执行以下步骤:

  1. 编译unity的脚本称常规的.net dll,也就是托管程序集

  2. 应用托管字节码剥离。这一步大大减少发布游戏的大小Applies managed bytecode stripping. This step significantly reduces the size of a built game.

  3. 将所有托管程序集转换为标准c++代码

  4. 使用本机平台编译器编译生成的c++代码和IL2CPP的运行时部分。Compiles the generated C++ code and the runtime part of IL2CPP with a native platform compiler.

  5. 根据您的目标平台,将代码链接到可执行文件或DLL中。

上面这张图很到位

 

Optimizing IL2CPP build times

当使用IL2CPP构建项目时,项目构建时间可能要长得多。然而,有几种方法可以显著减少构建时间:

Use incremental building

当使用 incremental building时,C++ 编译器仅编译相对于上一次发布改变的文件,没有改变的不编译,注意,发布的文件夹要和上一次发布的文件夹是一致的,也就是发布到同一个文件夹下compiler only recompiles files that have changed since the last build. To use incremental building, build your project to a previous build location (without deleting the target directory).

Exclude project and target build folders from anti-malware software scans

关闭杀毒软件,比如360或者Windows defender,这个会减少50-66%的时间 (Testing by Unity Technologies found that build times decreased by 50 – 66% after disabling Windows Defender on a fresh Windows 10 installation.)

Store your project and target build folder on a Solid State Drive (SSD)

固态硬盘(ssd)的读/写速度比传统硬盘驱动器(HDD)更快。将IL代码转换为c++并编译它涉及到大量的读/写操作。更快的存储设备加速了这一过程。

 

Managed stack traces with IL2CPP

当托管代码中发生异常时,  stack trace 会告诉你发生错误的地方. 然而它并不总是出现,这时候,就需要配置的il2cpp的错误堆栈, 下面分别是c#自带的和c++配置的

Debug builds

When you use the debug build configuration, IL2CPP
reports a reliable managed stack trace, and includes each managed method in the call stack. The stack trace does not include line numbers from the original C# source code. ​

Release builds

When you use a release build configuration, IL2CPP might produce a call stack that’s missing one or more managed methods. This is because the C++ compiler has inlined the missing methods. Method inlining is usually good for performance at run time, but it can make call stacks more difficult to understand. IL2CPP always provides at least one managed method on the call stack. This is the method where the exception occurred. It also includes other methods if they are not inlined. ​

Source code line numbers

IL2CPP call stacks do not include source code line number information in the debug or release configurations.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TO_ZRG

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值