Unity native plugin
Simplest Plugin example Example code from here: https://docs.unity3d.com/Manual/PluginsForDesktop.html
Follow this tutorial
- On the menu bar, choose File, New, Project.
- In the left pane of the New Project dialog box, expand Installed, Templates, Visual C++, and then select Win32.
- In the center pane, select Win32 Console Application.
- Specify a name for the project (for example, SimpleNativeLibrary). Choose the OK button.
- On the Overview page of the Win32 Application Wizard dialog box, choose the Next button.
- On the Application Settings page, under Application type, select DLL.
- Choose the Finish button to create the project.
If you don't need the precompiled headers.
- Open the project properties, in C/C++, Precompiled Headers.
- In the options on the right, under Precompiled Header, select Not Using Precompiled Headers
- Delete all source and headers files added automatically beside SimpleNativeLibrary.cpp
- Put code you want to include in this library
// SimpleNativeLibrary.cpp : Defines the exported functions for the DLL application.
//
#if _MSC_VER // this is defined when compiling with Visual Studio
#define EXPORT_API __declspec(dllexport) // Visual Studio needs annotating exported functions with this
#else
#define EXPORT_API // XCode does not need annotating exported functions, so define is empty
#endif
// Link following functions C-style (required for plugins)
extern "C"
{
// The functions we will call from Unity.
//
const EXPORT_API char* PrintHello() {
return "Hello";
}
int EXPORT_API PrintANumber() {
return 5;
}
int EXPORT_API AddTwoIntegers(int a, int b) {
return a + b;
}
float EXPORT_API AddTwoFloats(float a, float b) {
return a + b;
}
} // end of export C block
- Build the library.
Reference guides: this, and this
- Open Android Studio and click on New Project
- Fill Application Name (for example SimpleNativeLibrary), Company Domain and check Include C++ Support
- Continue like all other default android project and in the last step before click on Finish you need to set your C++ Standard! I use Toolchain Default
- if you got an exception with this message : Error:NDK not configured. Download it with SDK manager.) you should follow this steps:
- open Project Structure under File tab.
- set your NDK direction in Android NDK location and Done!
- In the project structure, under app module, open CMakeLists.txt
- Change the name of the library, under add_library and target_link_libraries sections. Replace native-lib to SimpleNativeLibrary
- Go to app, src, main, cpp and paste the C++ code in the source file there.
#include <jni.h>
#include <string>
// Link following functions C-style (required for plugins)
extern "C"
{
// The functions we will call from Unity.
const char* PrintHello() {
return "Hello";
}
int PrintANumber() {
return 5;
}
int AddTwoIntegers(int a, int b) {
return a + b;
}
float AddTwoFloats(float a, float b) {
return a + b;
}
} // end of export C block
- Sync gradle and build the library
- Open Unity and create a new project.
- Create folder Plugins in Assets folder. Put the compiled library in there.
- Create a new C# script UseNativeLibrary
- Add code to access the library in it:
using UnityEngine;
using System;
using System.Runtime.InteropServices;
public class UseNativeLibrary : MonoBehaviour {
//Lets make our calls from the Plugin
[DllImport("SimpleNativeLibrary")]
private static extern int PrintANumber();
[DllImport("SimpleNativeLibrary")]
private static extern IntPtr PrintHello();
[DllImport("SimpleNativeLibrary")]
private static extern int AddTwoIntegers(int i1, int i2);
[DllImport("SimpleNativeLibrary")]
private static extern float AddTwoFloats(float f1, float f2);
// Use this for initialization
void Start()
{
Debug.Log(PrintANumber());
Debug.Log(Marshal.PtrToStringAuto(PrintHello()));
Debug.Log(AddTwoIntegers(2, 2));
Debug.Log(AddTwoFloats(2.5F, 4F));
}
// Update is called once per frame
void Update () {
}
}
- In your scene, add an empty GameObject and drag UseNativeLibrary script to it.
- Press Run and you should see debug info in the console