Skip to content

moisesjbc/unity-plugins-test

Repository files navigation

unity-plugins-test

Screenshot of the project sample 3D scene viewed through Google Cardboard

This is a test about creating a rendering plugin for Unity. When used together, we get a scene with some models rendered by Unity and some others rendered by the plugin itself.

This project has been tested on Windows and Android only.

## Dependencies

Android dependencies

Building and running the project

  1. Install all the dependencies listed in previous section (except for Easyloggin++, which is already included in this repository).

  2. Clone this repository. This will create a local directory which we’ll refer to as LOCAL_REPO_DIR in the following steps.

git clone git@github.com:moisesjbc/unity-plugins-test.git
  1. Run cmake-gui on the directory LOCAL_REPO_DIR/RenderingPlugin.

  2. Generate a project for Visual Studio, Xcode, or your preferred build tool.

  3. Close cmake-gui and open the project generated in the previous step.

  4. Build the plugin.

  5. Now run Unity forcing it to use OpenGL.

"C:\Program Files\Unity\Editor\Unity.exe" -force-opengl
  1. From Unity, open the project LOCAL_REPO_DIR/UnityProject

  2. Open the scene in Assets/scene.unity

  3. Press play and enjoy this masterpiece!

Working on the project

IMPORTANT NOTE FOR DEVELOPERS

When recompiling the plugin, this must be moved to the LOCAL_REPO_DIR/UnityProject/Assets/Plugins directory in order for Unity to use it. But there is a problem, according to this link, "On Windows, the DLL file would be locked for writing, stopping you from overwiting it". This means that, in order to update the plugin, the Unity project must be closed while we overwrite the old version of the plugin with the new one. Once we reopen Unity project, it should load the new version of the plugin.

Editing the plugin’s inner code

When editing variables or functions which aren’t called directly by Unity, simply make the desired change and recompile the plugin.

**NOTE: ** if your changes affect the CMakeLists.txt specification (ie. removing source files, adding libraries, etc), please edit CMakeLists.txt so the project can be built from zero with the new changes :)

Altering the communication Unity - Plugin

Let’s suppose that you have added a C++ function with the following signature

    int add( int a, int b );

to the plugin, and you want to call that function from Unity. For that, make the following:

  1. Make sure that your function’s declaration is inside an extern “C” block and that it includes the EXPORT_API macro.

    #include <RenderingPlugin.h>
    
    extern “C” {
        int EXPORT_API add( int a, int b );
    }
  2. From Unity, open the script file you want to call the function from (ie. UnityProject/UseRenderingPlugin.cs). Then, “import” your function.

    public class UseRenderingPlugin : MonoBehaviour
    {
        #if UNITY_IPHONE && !UNITY_EDITOR
        [DllImport ("__Internal")]
        #else
        [DllImport ("RenderingPlugin")]
        #endif
        …
        private static extern int add( int a, int b );}
  3. Now you can use your awesome plugin function from Unity!

    IEnumerator Start () {
        …
        Debug.Log( "3 + 5 = " + add ( 3, 5 )  );}