为什么80%的码农都做不了架构师?>>>   

编者注

由于之前引入的log4net,在IL2Cpp下无法正常运行,导致没有日志库使用。还需要解决protobuf或thrift是否支持il2cpp进行测试。

Unity IL2Cpp构建

Unity安装

首先,如果选择的是最小化Unity安装,则没有安装IL2Cpp。请在安装时勾选。

Unity下载器

进行Choose Components菜单下,如果需要编译Windows版本的IL2Cpp,请安装如下内容勾选

[x] Unity 2018
[x] Window Build Support (IL2Cpp)

安装大约需要3GB,请提前清理好硬盘。

第一次尝试编译

编译配置

新建项目后,点击File->Build Settings,打开构建窗口,选择好对应平台。这里选择Windows。
点击Player Settings设置构建选项为IL2Cpp。

点击后,能够看到Inspector界面发生变化。找到Other Settings,子配置Configuration。由于Unity2018支持.Net Standard 2.0标准,则请按照如下内容进行

Scripting Runtime Version : .Net 4.x Equivalent
Scripting Backend         : IL2Cpp
Api Compatibility Level*  : .Net Standard 2.0

构建报错

当直接进行构建,将会发生错误,主要错误内容截取如下:

Failed running C:\Program Files\Unity\Editor\Data\il2cpp/build/il2cpp.exe --convert-to-cpp --emit-null-checks --enable-array-bounds-check --dotnetprofile="unityaot" --compile-cpp --libil2cpp-static --platform="WindowsDesktop" --architecture="x64" --configuration="Release" --outputpath="C:\Workspace\Unity\SnowTest\Temp/StagingArea/Data\Native\GameAssembly.dll" --cachedirectory="C:\Workspace\Unity\SnowTest\Assets\..\Library/il2cpp_cache" --map-file-parser="C:\Program Files\Unity\Editor\Data\Tools\MapFileParser\MapFileParser.exe" --assembly="C:\Workspace\Unity\SnowTest\Temp\StagingArea\Data\Managed\UnityEngine.dll" --generatedcppdir="C:\Workspace\Unity\SnowTest\Temp\StagingArea\Data\il2cppOutput" stdout:
il2cpp.exe didn't catch exception: System.InvalidOperationException: C++ code builder is unable to build C++ code. In order to build C++ code for Windows Desktop, you must have one of these installed:Visual Studio 2010 with C++ compilers and Windows 7 SDK (it cannot build C++ code because it is not installed)Visual Studio 2010 installation is found by looking at "SOFTWARE\Microsoft\VisualStudio\10.0_Config\InstallDir" in the registryWindows 7 SDK is found by looking at "SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v7.0A\InstallationFolder" in the registryVisual Studio 2013 with C++ compilers and Windows 8.1 SDK (it cannot build C++ code because it is not installed)Visual Studio 2013 installation is found by looking at "SOFTWARE\Microsoft\VisualStudio\12.0_Config\InstallDir" in the registryWindows 8.1 SDK is found by looking at "SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v8.1\InstallationFolder" in the registryVisual Studio 2015 with C++ compilers and Windows 10 SDK (it cannot build C++ code because it is not installed)Visual Studio 2015 installation is found by looking at "SOFTWARE\Microsoft\VisualStudio\14.0_Config\InstallDir" in the registryWindows 10 SDK is found by looking at "SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0\InstallationFolder" in the registryVisual Studio 2017 with C++ compilers and Windows 10 SDK (it cannot build C++ code because it is not installed)Visual Studio 2017 installation is found using Microsoft.VisualStudio.Setup.Configuration COM APIsWindows 10 SDK is found by looking at "SOFTWARE\Wow6432Node\Microsoft\Microsoft SDKs\Windows\v10.0\InstallationFolder" in the registry

通过该内容,能够查看到需要安装Visual Studio 2017 和 Windows 10 SDK

安装Visual Studio Community 2017

首先,登陆Visual Studio官网进行下载。无奈Visual Studio Community 2017需要 工作负载->通用Windows平台开发,安装需要15.4GB(我恨微软)。 - 暂停安装磁盘只剩17GB

尝试只使用Windows 10 SDK - 失败

尝试安装WIndows 10 SDK是否能够解决il2cpp的问题。经过下载3G内容,重启Unity,依旧是失败。

尝试只安装Cpp环境 - 成功

尝试Visual Studio Community 2017,工作负载->使用C++的桌面开发
勾选后,点击安装。IL2Cpp编译成功并运行。

IL2Cpp 适用范围

根据附录IL2CPP & Mono当中内容

IL2CPPCode generation is heavily improved compared to Mono.Debugging Script code in C++ from top to bottom is possible.You can enable Engine code stripping to reduce code size.Build times are longer than with Mono.Only supports Ahead of Time (AOT) compilation.MonoFaster build times than IL2CPP.Supports more managed libraries due to Just In Time compilation (JIT).Supports runtime code execution.Must ship managed assemblies (.dll files that mono- or .net- produces).

IL2Cpp仅仅支持Ahead of Time(AOT)编译。我需要确定AOT编译的库和方法的范围是哪些

Scripting restrictions 程序限制(翻译)

Unity力求提供通用化的脚本API和跨平台体验。但是,一些平台具有固有限制。为了帮助您理解平台约束和编写出跨平台代码,如下表格将会描述适用于每个平台和脚本的限制。

.Net 4.x equivalent scripting runtime

平台 AOT 无线程 .Net核心库子集
Android (IL2CPP)
Android (Mono)
iOS (IL2CPP)
PlayStation 4 (IL2CPP)
PlayStation Vita (IL2CPP)
Standalone (IL2CPP)
Standalone (Mono)
Switch (IL2CPP)
Universal Windows Platform (IL2CPP)
Universal Windows Platform (.NET)
WebGL (IL2CPP)
WiiU (Mono)
XBox One (IL2CPP)

.NET 3.5 equivalent scripting runtime

略 - 由于个人判断,未来支持都是.Net Standard 2.0则可以忽略.Net 3.5

Ahead-of-time compile AOT编译

有些平台不允许运行时生成代码。因此,任何在目标设备上的JIT编译都将失败。索性能够替代的方案,我们需要提前编译所有AOT代码。通常,可以只使用AOT保证兼容性,但是在一些特殊案例中,平台的AOT需求,需要额外考虑。

System.Reflection.Emit

任何AOT平台都无法实现System.Reflection.Emit命名空间下的所有函数。请注意其他部分System.Reflection是能够接受AOT,只要编译器能够在运行时中存在的代码进行反射。

Serialization

AOT平台无法预计的问题是由序列化过程使用了反射导致的。如果类型或方法只使用反射来序列化或反序列化其中一部分,AOT编译器无法检查哪些代码类型需要生成。

Generic virtual methods

泛型方法需要编译器做一些额外工作,将开发人员编写的代码在实际执行中扩展。举例:需要生成不同代码List<int>和List<double>。针对虚方法,是需要在运行时阶段确定哪些方法执行,编译器只能简单的认为所有代码需要完全彻底的进行编译。
假设有如下代码,这些代码在JIT平台正常工作(在控制台中打印Zero):

using UnityEngine;
using System;public class AOTProblemExample : MonoBehaviour, IReceiver {public enum AnyEnum {Zero,One,}void Start() {// Subtle trigger: The type of manager *must* be// IManager, not Manager, to trigger the AOT problem.IManager manager = new Manager();manager.SendMessage(this, AnyEnum.Zero);}public void OnMessage<T>(T value) {Debug.LogFormat("Message value: {0}", value);}
}public class Manager : IManager {public void SendMessage<T>(IReceiver target, T value) {target.OnMessage(value);}
}public interface IReceiver {void OnMessage<T>(T value);
}public interface IManager {void SendMessage<T>(IReceiver target, T value);
}

当这些代码在AOT平台中执行会发生如下错误:

ExecutionEngineException: Attempting to call method 'AOTProblemExample::OnMessage<AOTProblemExample+AnyEnum>' for which no ahead of time (AOT) code was generated.at Manager.SendMessage[T] (IReceiver target, .T value) [0x00000] in <filename unknown>:0 at AOTProblemExample.Start () [0x00000] in <filename unknown>:0

同样Mono脚本端会提供近似的异常:

  ExecutionEngineException: Attempting to JIT compile method 'Manager:SendMessage<AOTProblemExample/AnyEnum> (IReceiver,AOTProblemExample/AnyEnum)' while running with --aot-only.at AOTProblemExample.Start () [0x00000] in <filename unknown>:0

AOT编译器不知道需要为AnyEnum生成泛型方法OnMessage,因此编译器继续编译跳过该方法。当桉树执行到这里时,运行时找不到代码来执行,因此会抛出异常。
为了让AOT了解到这个问题,需要强制编译器去生成代码。我们想AOTProblemExample类添加一个方法:

public void UsedOnlyForAOTCodeGeneration() {// IL2CPP needs only this line.OnMessage(AnyEnum.Zero);// Mono also needs this line. Note that we are// calling directly on the Manager, not the IManager interface.new Manager().SendMessage(null, AnyEnum.Zero);// Include an exception so we can be sure to know if this method is ever called.throw new InvalidOperationException("This method is used for AOT code generation only. Do not call it at runtime.");
}

编译器遇到显示调用的枚举时,将会为了枚举生成代码进行执行。UsedOnlyForAOTCodeGeneration方法永远也不需要调用。仅仅只为了编译器而存在的。

No Thread 无线程

某些平台不支持线程的使用(当前碰到的只有WebGL),因此,任何使用System.Threading的命名空间将会在运行过程发生错误。一些.Net类库隐式依赖thread,举例System.Timers.Timer类,在这些平台无法使用。

附录

Unity将来时:IL2CPP是什么?
How IL2CPP works
IL2CPP & Mono

转载于:https://my.oschina.net/hava/blog/1865498

Unity IL2Cpp相关推荐

  1. Unity il2cpp.exe 已停止工作 CLR20r3

    Unity 3D 在Build的时候最后卡住了,弹出如下提示,打开下边的详情会发现错误名称是CLR20r3的错误. 打开Unity最下边会发现如下错误日志: Failed running C:\Pro ...

  2. Unity il2cpp API 调用实践

    测试环境 Unity2019.4.38 il2cpp版本24.5 c# to il to cpp 在Unity4.6.1 p5以后版本中,在PlayerSettings->Other Setti ...

  3. Android Unity il2cpp 编译异常

    开发背景 利用Unity开发3d模块,将3d模块嵌入Android项目. 开发环境 Android Studio 2020.3.1 Patch3 Gradle 6.1.1 Android Gradle ...

  4. unity il2cpp 源码编译

    新建一个XCODE静态库工程 从unity安装目录中找到il2cpp源码 Editor\Data\il2cpp\libil2cpp 和依赖Editor\Data\il2cpp\external\boe ...

  5. Unity Il2cpp汉化资源位置总结

    资源可能在的位置包括  1. MONOBehavior脚本(写在逻辑里的文字)  2.UIText 控件 (写在控件里的文字) 3.global-metadata.dat文件里面.可以直接查文字,用 ...

  6. IL2CPP does not support marshaling delegates that point to instance methods to native code

    报错代码段 图中代码使用Unity IL2CPP编译时会报错 System.NotSupportedException: IL2CPP does not support marshaling dele ...

  7. 【游戏提取/超详细记录向】关于unity游戏的资源提取所需资源及方法简介(AssetBundle及libil2cpp.so等解密)

    方法一:AssetStudio提取(仅针对ab包及.assets..boundle和.unity3d未加密的情况下) 1.在我们拿到一个游戏的安装包时,首先会疑惑如何打开.下载bandzipBandi ...

  8. 知物由学|游戏开发者如何从容应对Unity手游风险?

    本文由 网易云 发布 "知物由学"是网易云易盾打造的一个品牌栏目,词语出自汉·王充<论衡·实知>.人,能力有高下之分,学习才知道事物的道理,而后才有智慧,不去求问就不会 ...

  9. Unity中的热更新的基础知识,Xlua与ILRuntime基础知识

    1.什么是热更新 热更新是指在不需要重新编译打包游戏的情况下,在线更新游戏中的一些非核心代码和资源,比如活动运营和打补丁.热更新分为资源热更新和代码热更新两种,代码热更新实际上也是把代码当成资源的一种 ...

最新文章

  1. cordova切换到安卓5.1.0,Android SDK not found
  2. struts2配置文件中的method={1}详解
  3. python 全栈开发,Day66(web应用,http协议简介,web框架)
  4. 怎么看android sdk 文档
  5. 汇编语言 段前缀的使用 复制内存单元数据到指定位置
  6. qt widget 窗口句柄的问题
  7. 《Redis+Nginx+设计模式+Spring全家桶+Dubbo》,附 PDF amp; 简历模板 下载
  8. spring事务传播行为与事务隔离等级
  9. 【基础】优化背后的数学基础
  10. 图神经网络在生化医疗方面的相关应用
  11. 怎么减小照片大小kb?
  12. 第25版 OpenStack Yoga 已发布:稳定性与创新并重
  13. 使用源码编译的方式安装Tor
  14. Python网络爬取科目一题库(1685道)2021.1.3
  15. unbuntu20.0.4 显卡驱动安装,nividia-smi无效
  16. excel 画散点图 怎么设置图片的分辨率_【R语言】barplot柱状图+散点图+误差棒
  17. sse——字符串数组
  18. Linux 磁盘分区挂载
  19. 一点点linux系统的学习心得
  20. 自控考研复习 自我梳理(三) 知识来自网络,纯为总结侵权即删(一阶系统)

热门文章

  1. 【FFmpeg笔记】03-Win10编译ffmpeg4.2.1(For Android)
  2. 几种位姿转换表示方式----tf::StampedTransform Eigen::Matrix4f Pose, 及其相互转换
  3. Webpack-2:Webpack
  4. Python探秘大众点评北京火锅店第①期:好吃的火锅在哪里?
  5. 做个时尚男人!变形金刚版男士护肤品包装
  6. yum卸载软件 不卸载依赖_如何使用Yum Remove卸载,删除软件包
  7. java BigDecimal加法/减法/乘法/除法 保留两位小数
  8. win7任务计划 重启程序失败
  9. 如何让手机的计算机能弹出音乐,电脑在待机状态下,也能跟手机一样正常播放音乐?...
  10. 收货地址卡片源码分享