为什么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. klock 分布式锁重大更新
  2. ssm项目jsp加载不出来图片_16. SSM 搭建
  3. MVC Razor 语法(转)
  4. 2-hadoop-Hadoop以及生态
  5. 分页请求json数据_Python爬虫入门教程 28-100 虎嗅网文章数据抓取 pyspider
  6. 推荐学习网页标准的20个好去处
  7. Android RecyclerView根据不同的item设置不同的背景颜色
  8. 产品经理如何搭建自己的知识地图?
  9. 【TSP问题】TSP问题有关解法
  10. 计算机excel猪肉价格分析,猪肉价格的统计模型.doc
  11. 【最佳实践】行云管家数据库运维审计解决方案
  12. 【redis源码学习】rax,我愿称之为“升级版字典树”
  13. MPSK 通信系统的 Monte Carlo 仿真
  14. 小丁的Spring笔记一(概述)
  15. Unix macOS 编写 who 命令
  16. 数据库多表联查的方式
  17. java边缘检测算子代码_图像边缘检测(Canny 算法)的Java实现
  18. 影视金曲-爱你一生嫌未够_昆仑奴插曲》
  19. 用 Python 爬了猫眼3000+《指环王》影评,看看20年前的魔幻电影鼻祖在当下影迷眼中的样子...
  20. 邹鹏程:操作系统工程视角下的AIoT

热门文章

  1. linux mutt dns,使用mutt处理电子邮件
  2. vue3时间格式转换为yyyy/mm/dd,yyyy-MM-dd,yyyy-MM-dd hh:mm:ss,hh:mm,yyyy-MM-ddThh:mm:ss+08:00
  3. 字符串(字符串匹配)
  4. 张小龙不是广东人 | 小林和腾讯不得不说的故事(五)
  5. R 语言中的 Kolmogorov-Smirnov 检验
  6. python ks检验_在python scipy中实现Kolmogorov Smirnov检验
  7. unison_如何在Ubuntu 16.04上使用Unison备份大目录
  8. html中ul和li标签的用法
  9. mysql 的 归并排序_归并与归并排序
  10. JAVA isAssignableFrom 判断父子关系