The future of scripting in Unity

Recently we talked about Unity and WebGL . In that post we briefly spoke about how scripting works in WebGL, using new technology called “IL2CPP”. However, IL2CPP represents a lot more than just a scripting solution for WebGL; it’s our own high performance .NET Runtime, to be rolled out on more platforms.

But before we delve into the future, let’s talk about the present.

Scripting in Unity today

We leverage Mono (and WinRT on Windows Store Apps and Windows Phone) to bring the ease of use of C#, access to 3rd party libraries, and near native performance to Unity. However, there are some challenges:

  • C# runtime performance still lags behind C/C++
  • Latest and greatest .NET language and runtime features are not supported inUnity’s current version of Mono .
  • With around 23 platforms and architecture permutations, a large amount of effort is required for porting, maintaining, and offering feature and quality parity.
  • Garbage collection can cause pauses while running

These issues have remained in the front of our minds over the past few years as we sought to address them. Concurrently, investigations into supporting scripting for WebGL were occurring. As each progressed forward, these two paths converged into a single approach.

With the problem scope clear, experimentation on different ways to solve this were tried. Some of them were promising; others were not. But ultimately we found an innovative solution and it proved to be the right way forward.

That way forward is IL2CPP.

IL2CPP: the quick and dirty intro

IL2CPP consists of two pieces: an Ahead of Time (AOT) compiler and a Virtual Machine (VM) .

These two parts represent our own implementation of the Common Language Infrastructure , similar to .NET or Mono. It is compatible with the current scripting implementation in Unity.

Fundamentally, it differs from the current implementation in that the IL2CPP compiler converts assemblies into C++ source code. It then leverages the standard platform C++ compilers to produce native binaries.

At runtime this code is executed with additional services (like a GC, metadata, platform specific resources) that are provided by the IL2CPP VM.

The benefits of IL2CPP.

Let’s talk about each one of the previously mentioned issues and how IL2CPP addresses each of them.

Performance

IL2CPP seeks to provide the ease of use and productivity of C# with the performance of C++.

It allows the current, productive scripting workflow to remain the same while giving an immediate performance boost. We’ve seen 2x-3x performance improvements in some of our script-heavy benchmarks. This performance boost is due to a few reasons.

  • C++ compilers and linkers provide a vast array of advanced optimisations previously unavailable.
  • Static analysis is performed on your code for optimisation of both size and speed.
  • Unity-focused optimisations to the scripting runtime.

While IL2CPP is definitely still a work in progress, these early performance gains are indicative of great things to come.

.NET Upgrade

A very frequent request we get is to provide an upgraded runtime. While .NET has advanced over the past years, Unity currently supports .NET 2.0/3.5 era functionality for both the C# compiler and the class libraries. Many users have requested access to newer features, both for their code as well as 3rd party libraries.

To complement IL2CPP, as it matures, we will also be upgrading to recent versions of the Mono C# compiler, base class libraries, and runtime for use in the editor (The editor will not switch to IL2CPP, for fast iteration during development). These two things combined will bring a modern version of .NET to Unity.

It’s also important to note that we are collaborating with Microsoft to bring current and future .NET functionality to Unity, ensuring compatibility and quality.

Portability and Maintenance

While this area may sound like an internal issue for Unity to deal with, it also affects you. The Mono virtual machine has extensive amounts of platform and architecture specific code. When we bring Unity to a new platform, a large amount of our effort goes into porting and maintaining the Mono VM for that platform. Features (and bugs) may exist on some platforms but not others. This affects the value which Unity strives to provide to you; easy deployment of the same content to different platforms.

IL2CPP addresses these issues in a number of ways:

  • All code generation is done to C++ rather than architecture specific machine code. The cost of porting and maintenance of architecture specific code generation is now more amortised.
  • Feature development and bug fixing proceed much faster. For us, days of mucking in architecture specific files are replaced by minutes of changing C++. Features and bug fixes are immediately available for all platforms. In it’s current state, IL2CPP support is being ported to new platforms in short amount of time.

Additionally, platform or architecture specific compilers can be expected to optimise much better than a singular code generator. This allows us to reuse all the effort that has gone into the C++ compilers, rather than reinventing it ourselves.

Garbage Collection

IL2CPP is not tied to any one specific garbage collector, instead interacting with a pluggable API. In its current iteration IL2CPP uses an upgraded version of libgc , even as we look at multiple options. Aside from just the GC itself, we are investigating reducing GC pressure by analysis done in the IL2CPP compiler.

While we don’t have a lot more to share at the moment, research is ongoing. We know this is important to many of you, we’ll continue working on it and keep you informed in future blog posts. Unrelated to IL2CPP, but worth mentioning in the context of garbage collection, Unity 5 will see more and more allocation free APIs.

What IL2CPP is not

IL2CPP is not recreating the whole .NET or Mono toolchain. We will continue to use the Mono C# compiler (and perhaps later,  Roslyn ). We will continue to use the Mono class libraries. All currently supported features and 3rd party libraries which work with Mono AOT should continue to work with IL2CPP. We are only seeking to provide a replacement for the Mono VM and AOT compiler, and will keep on leveraging the wonderful Mono Project .

When can I try IL2CPP?

By now we hope you are just as excited as we are to use IL2CPP and wondering when you can get your hands on it! An early version of IL2CPP will be available as part of WebGL publishing in Unity 5.

Beyond WebGL, we are continuing development of IL2CPP for other platforms. In fact, we already have working implementations on a number of our supported platforms. We expect to be rolling out at least one additional platform later this year. Our current plan is to have iOS be the next platform shipping with IL2CPP support.

The planned upgrades of our Mono toolchain will follow after IL2CPP is available on more platforms and has matured.

One platform that will never be supported by IL2CPP is the WebPlayer; this is due to security implications. And as noted earlier, the editor will remain to be using Mono.

Additionally, you can see the IL2CPP runtime in action today. As mentioned, the  two WebGL demos we posted  are IL2CPP-powered.

What’s next?

We are still hard at work on IL2CPP: implementing new features, optimising code generation, fixing bugs, and supporting more platforms. We’ll keep posting more in-depth blogs as we make progress and talk about it with you on theforums.

The Scripting Team.

时间: 2024-11-08 18:52:31

The future of scripting in Unity的相关文章

Unity将来时:IL2CPP是什么?

作者:小玉链接:https://zhuanlan.zhihu.com/p/19972689来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. Unity3D 想必大家都不陌生,独立游戏制作者们很多人都在用它,甚至一些大公司也用在很商业的游戏制作上.Unity3D最大的一个特点是一次制作,多平台部署,而 这一核心功能是靠Mono实现的.可以说Mono是Unity3D核心的核心,是Unity3D跨平台的根本.但是在2014年年中的时 候,Unity3D官方博客上却发了一篇

Unity Networking API文档翻译(一):Networking概述

写在翻译前的话:      我使用过Photon,研究过Ulink这些Unity提供的多人在线游戏服务器组件,这些商业组件虽然很好很强大.但是对于一个独立开发者来说,4000多软妹币还是点多.总想找一个免费的.对于Unity5.2版本后提供的Unity Networking组件(简称UNet),国外已经有很多开发者在使用了,虽然稳定性还有待考验,但是unity官方是会不停更新的,主要的是免费的啊!官网没有中文翻译,所以自己准备把这块翻译下,有不好的地方敬请指出,共同完善这个中文文档.翻译的过程就

关于 Unity WebGL 的探索(一)

到今天为止,项目已经上线一个多月了,目前稳定运行,各种 bug 也是有的.至少得到了苹果的两次推荐和 TapTap 一次首页推荐,也算是结项后第一时间对我们项目的一个肯定. 出于各种各样的可描述和不可描述之原因,我们现在需要把项目移植到 Web 端,第一次被告知这个需求时我直接给出了不可能的答复,之前从来没有考虑过这个平台的兼容性,现在项目算是做完了结果要这样折腾一番我觉得是需要消耗非常可怕的人力物力但未必能有很好的效果,性价比很低,但是最终我还是妥协了,硬着头皮接下来,也硬着头皮上,毕竟,技术

Unity3D用户手册

Welcome to Unity. 欢迎使用Unity. Unity is made to empower users to create the best interactive entertainment or multimedia experience that they can. This manual is designed to help you learn how to use Unity, from basic to advanced techniques. It can be

Unity3D脚本手册

翻译:脚本_树 This is a short overview of how scripting inside Unity works. Scripting inside Unity consists of attaching custom script objects called behaviours to game objects. Different functions inside the script objects are called on certain events. Th

Digital Tutors - Introduction to Scripting Shaders in Unity 学习笔记

1. Overview 2. Understanding Shader definition:code that define what the material can do in the environment classification: Surface Shader--容易编写,受光照等因素影响 Vertex and Fragment Shader(Pixels)--非常灵活,代码量大,写得好能提高渲染效率和降低开销 Fixed Function Shader--固定管线shader

碰撞器与触发器[Unity]

请看原帖,移步:Unity3d碰撞检测中碰撞器与触发器的区别 要产生碰撞必须为游戏对象添加刚体(Rigidbody)和碰撞器,刚体可以让物体在物理影响下运动.碰撞体是物理组件的一类,它要与刚体一起添加到游戏对象上才能触发碰撞.如果两个刚体相互撞在一起,除非两个对象有碰撞体时物理引擎才会计算碰撞,在物理模拟中,没有碰撞体的刚体会彼此相互穿过. 物体发生碰撞的必要条件: 两个物体都必须带有碰撞器(Collider),其中一个物体还必须带有Rigidbody刚体. 在unity3d中,能检测碰撞发生的

【转】Unity中的协同程序-使用Promise进行封装(一)

原文:http://gad.qq.com/program/translateview/7170767 译者:陈敬凤(nunu)    审校:王磊(未来的未来) 每个Unity的开发者应该都对协同程序非常的熟悉.对于很多Unity的开发者而言,协同程序就是用来编写大量异步和延时任务的一种方法.如果你不在乎速度的话,有非常非常多的特殊方法可以在任何所需的时间暂停和恢复执行.在实践中,它们可以营造一种并发函数的幻觉 (虽然他们与线程无关!).然而,协同程序会有一些问题,许多程序员在使用协同程序的时候会

unity内部:内存 和 性能(以及Unity5的升级优化)

      我们的脚本代码里经常会需要访问gameObject引用或者某个组件的引用,最好的方式当然是在脚本Awake的时候就把这些可能访问的东西都缓存下来:如果需要访问临时gameObject实例的某属性或者临时某组件的gameObject实例,在能够确保组件一定存在(可以使用[RequireComponent( typeof(AudioSource ))] 如果没有自动添加移除不了!)的情况下,可以用属性访问,毕竟属性访问比GetComponent要快上一倍,但是如果不能确定组件是否存在,甚