Sunday, July 17, 2011

Plugins for Unity3D

Hi there,

during the following weeks I will write a small series of posts about plugins for Unity3D. In particular how to create plugins for Android and Windows applications.

While Unity3D already comes with a big amount of features, not all of them are available on all platforms. Especially the mobile platforms lack of many functionalities. Using plugins, you can use additional features in your project. You can integrate your own sound plugin, give access to a particular render engine or use other external APIs. Plugins can also be very useful if you already possess a library with functionalities that you are interested in using.

Unity3D allows you to write your individual plugin, which you can access through interfaces (C# or Javascript) inside your project. The integration of your own plugin is not necessarily difficult but can easily become cumbersome, if you are not familiar with concepts, like Dllimport or JNI.

The basic structure of a Unity3D plugin consists of multiple layers. At Unity level a Javascript/C# interface layer, a layer that serves as a wrapper (like JNI), and finally your_library layer (C/C++/Java). The underlying concept of such an approach is to define interfaces (interface layer) that will give you inside Unity access to methods implemented outside of Unity, for example in a particular library (your_library Layer). Such access is often realized using a wrapper, like JNI (wrapper layer). JNI (Java Native Interface) is a popular tool to access native (C/C++) code from Java, which you might know if have already worked with the Android NDK (Native Development Kit).

In the following, I present an overview of the some common methods to access external functionalities on the Android and the Windows platform.

Android
A very easy way to access a particular Android function from Unity3D, without the need to implement a particular plugin, is the AndroidJNI interface provided by Unity3D. The wrapping is done internally by Unity3D, so you do not have to worry about that. The drawback is that you can only access functions provided by the Android SDK Unity3D is linked to.

Accessing Android from Unity3D via AndroidJNI interface

There are basically two ways to implement Unity plugins for Android applications. Firstly, using Java and the Android SDK or secondly in C/C++ using the Android NDK, as illustrated in the following illustration.

Accessing Android from Unity3D using JNI

Windows
Plugins for Windows applications are implemented using a so called Dllimport, that gives access to an external shared library (dll) under Windows.

Access to an external dll library


In the several follow up posts I will go in more detail on each of these approaches.

Links
Unity3D Plugins Documentation

2 comments:

  1. what happened to your articles and tutorials on plugins for unity3d? web needs more of that!

    ReplyDelete
  2. Hi,

    you are completely right and did not forget it. There was too much work in the last months and this got out of sight.

    But I certainly will publish more of this.

    Please stay tuned.

    bye

    ReplyDelete