webgl unity
Last year we launched a series of technical blog posts on WebGL, starting with a couple of posts on memory. Now it’s time to continue with a new topic. Have you ever needed/wanted to re-use existing C/C++ code in a web page? Perhaps a graphics effect written in OpenGL ES? With Unity WebGL there is a way!
去年,我们在WebGL上发布了一系列技术博客文章,从内存中的几篇文章开始。 现在是时候继续一个新主题了。 您是否曾经需要/想要重复使用网页中的现有C / C ++代码? 也许是用OpenGL ES编写的图形效果? 有了Unity WebGL,就有了一种方法!
Unity supports two types of plugins: managed and native. On WebGL, managed plugins are supported like on other platforms. The only difference is that the managed assembly of the plug-in is converted, along with the engine and user’s managed code, to JavaScript (asm.js/wasm to be specific).
Unity支持两种类型的插件 :托管和本机。 与其他平台一样, 在WebGL上 也支持 托管插件 。 唯一的区别是,插件的托管程序集与引擎和用户的托管代码一起转换为JavaScript(具体来说为asm.js / wasm)。
What about native plugins? Does it even make sense to talk about something “native” on the web? When referring to native as something specific to the underlying architecture (mac, win32/64, etc.), it certainly does not. However, Unity WebGL does support several other types of plugins: JavaScript, C/C++ and pre-compiled LLVM byte code.
那 本机插件 呢? 在网络上谈论“本机”甚至有意义吗? 当将native称为特定于基础架构的东西(mac,win32 / 64等)时,它当然不是。 但是,Unity WebGL确实支持其他几种类型的插件:JavaScript,C / C ++和预编译的LLVM字节码。
In the Unity User Manual, there are a couple of examples of both JavaScript and C plugins that demonstrate how to interact with them via scripts. So, since we can use C/C++ sources, what’s stopping us from accessing the Low-Level plugin interface for rendering purposes? Well… nothing in Unity 5.5 ;-)
在Unity 用户手册中 ,有两个JavaScript和C插件示例,它们演示了如何 通过脚本与它们进行 交互 。 因此,由于我们可以使用C / C ++源,是什么阻止了我们 出于渲染目的而 访问 Low-Level插件接口 ? 好吧... Unity 5.5中什么都没有;-)
In fact, Unity 5.5 adds the missing hooks to allow you to register a low-level plugin:
实际上,Unity 5.5添加了缺少的钩子,以允许您注册低级插件:
As you can see, the amount of code we had to add is massive. Now let’s see what you need to do to implement your plug-in!
如您所见,我们必须添加大量的代码。 现在,让我们看看实现插件所需要做的!
实作 (Implementation)
First of all, you need to copy the Plugin API headers from the Unity install path Editor/Data/PluginAPI to the location that will contain your plug-in source file(s).
首先,您需要将Unity安装路径Editor / Data / PluginAPI中的Plugin API标头复制到将包含您的插件源文件的位置。
The headers you are interested in are IUnityInterface.h and IUnityGraphics.h, which declare the interfaces needed for the plugin. Remember that these headers are specific to a version of Unity, so it’s important to keep them in sync with the Editor.
您感兴趣的标头是IUnityInterface.h和IUnityGraphics.h ,它们声明了插件所需的接口。 请记住,这些标头特定于Unity版本,因此使它们与编辑器保持同步很重要。
The function you need to call to register your plugin is UnityRegisterRenderingPlugin:
注册插件需要调用的函数是UnityRegisterRenderingPlugin :
|
extern "C" void UnityRegisterRenderingPlugin(PluginLoadFunc loadPlugin, PluginUnloadFunc unloadPlugin);
|
|
extern "C" void UnityRegisterRenderingPlugin ( PluginLoadFunc loadPlugin , PluginUnloadFunc unloadPlugin ) ;
|
However, first you need to implement both Load and Unload callbacks to get the IUnityGraphics interface and register/unregister the graphics device event callback for your low-level rendering. Here is an example:
但是,首先,您需要同时实现Load和Unload回调,以获取IUnityGraphics接口并为低级渲染注册/注销图形设备事件回调。 这是一个例子: