Created
October 17, 2017 07:46
-
-
Save yubaoliu/7b604bfadfb94f2f675570c8455985b9 to your computer and use it in GitHub Desktop.
How to use C/C++ plugins (C/C++ lib) in Unity3D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"LowLevelPlugin.h" | |
#pragma once | |
#if UNITY_METRO | |
#define EXPORT_API __declspec(dllexport) __stdcall | |
#elif UNITY_WIN | |
#define EXPORT_API __declspec(dllexport) | |
#else | |
#define EXPORT_API | |
#endif | |
Open C source file and paste following code: | |
#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; | |
} | |
Using library in Unity3d: | |
[DllImport ("LowLevelPlugin")] | |
private static extern int [,] fillArray(int size); | |
Reference: | |
[1] https://medium.com/@rafalwilinski/tutorial-create-c-c-plugins-for-unity3d-dbde7f67454 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment