Tutorial — Create C/C++ plugins for Unity3D

Benchmarks

  • Fill 10000 x 10000 Integer array with series:
    [Unity C#] 1.0258 seconds vs 0.2346 seconds [C Library]
  • Fill 500 x 500 Integer array with series:
    [Unity C#] 3.794 milliseconds vs 0.35 milliseconds [C Library]
  • Fill 10000 x 10000 Integer array with Random numbers:
    [Unity C#] 4.26 seconds vs 0.995 seconds [C Library]
  • Fill 10000 x 10000 Integer array with Perlin Noise:
    [Unity C#] 6.015 seconds vs 2.394 seconds [C Library]
  • Fill 500 x 500 Integer array with Perlin Noise:
    [Unity C#] 20,47 milliseconds vs 5.697 milliseconds [C Library]

Part 1. Creating C++ Library

How to create Unity3D C/C++ Plugin
Select “New File…”
#ifndef __LowLevelPlugin__LowLevelPlugin__ 
#define __LowLevelPlugin__LowLevelPlugin__
#include <stdio.h>
#endif /* defined(__LowLevelPlugin__LowLevelPlugin__) */
#pragma once #if UNITY_METRO 
#define EXPORT_API __declspec(dllexport) __stdcall
#elif UNITY_WIN
#define EXPORT_API __declspec(dllexport)
#else
#define EXPORT_API
#endif
#pragma once
#if UNITY_METRO
#define EXPORT_API __declspec(dllexport) __stdcall
#include <stdlib.h> 
#include <math.h>
#include "LowLevelPlugin.h"
extern "C" int ** EXPORT_API fillArray(int size) {
int i = 0, j = 0;
int ** array = (int**) calloc(size, sizeof(int*));
for(i = 0; i < size; i++) {
array[i] = (int*) calloc(size, sizeof(int));
for(j = 0; j < size; j++) {
array[i][j] = i * size + j;
}
}
return array;
}
extern "C" int ** EXPORT_API fillArray(int size) {
int ** array = (int**) calloc(size, sizeof(int*));
array[i] = (int*) calloc(size, sizeof(int));

Part 2. Using library in Unity3d

  1. At the top of your C# script add
  1. Add external function declaration. In our case its
[DllImport ("LowLevelPlugin")]
private static extern int [,] fillArray(int size);
int [,] array;
array = fillArray(size);

Part 3. Run benchmarks on your own! (optional)

using UnityEngine; 
using System.Collections;
using System.Runtime.InteropServices;
public class PerformanceTests : MonoBehaviour {
public int size = 512;
[DllImport ("LowLevelPlugin")]
private static extern int[,] fillArray(int size);
void Start () {
ArrayFillTest();
}
private void ArrayFillTest() {
var start = Time.realtimeSinceStartup;
int[,] tab = fillArray(size);
Debug.Log( (Time.realtimeSinceStartup-start).ToString("f6") + " secs"); start = Time.realtimeSinceStartup;
int[,] array = new int[size,size];
for(int i = 0; i < size; i++) {
for(int j = 0; j < size; j++) {
array[i,j] = i * size + j;
}
}
Debug.Log( (Time.realtimeSinceStartup-start).ToString("f6") + " secs");
}
}

--

--

--

Founder of https://dynobase.dev, AWS Certified Architect, love minimalism and procedural content generation.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rafal Wilinski

Rafal Wilinski

Founder of https://dynobase.dev, AWS Certified Architect, love minimalism and procedural content generation.

More from Medium

Git Good with Unity

Initializing A Git Repository in Unity

How to setup Git for a Unity project

Virtual Cameras w/ Cinemachine in Unity 2020 — Part 4