├── README.md ├── StartScript.cs ├── plugin.cpp └── plugin.h /README.md: -------------------------------------------------------------------------------- 1 | # UnityUnmanagedPlugins 2 | This is a database of sample c++ functions that can be run from Unity C# script. 3 | New functions will be added as well as a full c++ in Unity tutorial in this readme file. 4 | 5 | ### Section 0 - Creating the plugin 6 | To create an unmanaged plugin for Unity you need to do the following: 7 | 8 | - create c++ file with DLL_EXPORT defined like in plugin.h example (or just copy the sample files - plugin.h and plugin.cpp) 9 | - compile project into a DLL (if you are using CodeBlocks or similar IDE make sure to compile it with compiler appropriate to your architecture - 32 or 64 bit, also do not compile for Debug as Unity will have problems reading the plugin) 10 | - move created dll file to Unity (Assets/Plugins), make sure to do it while Unity Editor is closed - plugins can only be updated this way 11 | - create a C# script with the sample code in Unity and attach it to an empty gameobject, rename "cpp" in dllimport sections to your plugin name 12 | 13 | ### Section 1 - Simple arguments and return types 14 | 15 | 1.1. This function showcases how to receive simple type from c++. 16 | 17 | 1.2. This function showcases how to pass simple type to c++. 18 | 19 | 1.3. This function showcases how to pass argument by reference to c++. 20 | 21 | ### Section 2 - Callbacks 22 | 23 | 2.1. This function showcases how to trigger simple Unity callback from c++. 24 | 25 | 2.2. This function showcases how to trigger Unity callback that returns simple type from c++. 26 | 27 | 2.3. This function showcases how to trigger Unity callback that takes simple argument from c++. 28 | 29 | ### Further sections - WIP 30 | -------------------------------------------------------------------------------- /StartScript.cs: -------------------------------------------------------------------------------- 1 | using UnityEngine; 2 | using System; 3 | using System.Runtime.InteropServices; 4 | 5 | public class StartScript : MonoBehaviour { 6 | 7 | //------------------------------------------------------------------------------------------------ 8 | //------------------- SECTION 1 - Simple types --------------------------------------------- 9 | //------------------------------------------------------------------------------------------------ 10 | 11 | //------------------- 1.1. Returning simple type --------------------------------------------- 12 | [DllImport("cpp", EntryPoint = "SimpleTypeReturnFun")] 13 | private static extern int SimpleTypeReturnFun(); 14 | 15 | //------------------- 1.2. Passing simple type ----------------------------------------------- 16 | [DllImport("cpp", EntryPoint = "SimpleTypeArgFun")] 17 | private static extern int SimpleTypeArgFun(int n); 18 | 19 | //------------------- 1.3. Passing reference ------------------------------------------------- 20 | [DllImport("cpp", EntryPoint = "ReferenceArgumentFun")] 21 | private static extern int ReferenceArgumentFun(ref int n); 22 | 23 | 24 | 25 | 26 | //------------------------------------------------------------------------------------------------ 27 | //------------------- SECTION 2 - Callbacks --------------------------------------------------- 28 | //------------------------------------------------------------------------------------------------ 29 | 30 | //------------------- 2.1. Simple Callback --------------------------------------------------- 31 | private delegate void SimpleCallback(); 32 | [DllImport("cpp", EntryPoint = "SimpleCallbackFun")] 33 | private static extern void SimpleCallbackFun(SimpleCallback c); 34 | 35 | //------------------- 2.2. Simple Callback with return type ---------------------------------- 36 | private delegate int SimpleReturnCallback(); 37 | [DllImport("cpp", EntryPoint = "SimpleReturnCallbackFun")] 38 | private static extern int SimpleReturnCallbackFun(SimpleReturnCallback c); 39 | 40 | //------------------- 2.3. Simple Callback taking single argument ---------------------------- 41 | private delegate void SimpleArgCallback(int n); 42 | [DllImport("cpp", EntryPoint = "SimpleArgCallbackFun")] 43 | private static extern void SimpleArgCallbackFun(SimpleArgCallback c); 44 | 45 | 46 | void Start () { 47 | //Running section 1 test cases 48 | Run1_1(); 49 | Run1_2(); 50 | Run1_3(); 51 | 52 | //Running section 2 test cases 53 | Run2_1(); 54 | Run2_2(); 55 | Run2_3(); 56 | } 57 | 58 | //------------------- Section 1 test functions ------------------------------------------------- 59 | private void Run1_1() { 60 | Debug.Log("1.1. Output: \t" + SimpleTypeReturnFun()); 61 | } 62 | 63 | private void Run1_2() { 64 | Debug.Log("1.2. Output: \t" + SimpleTypeArgFun(2)); 65 | } 66 | 67 | private void Run1_3() { 68 | int tempInt = 3; 69 | ReferenceArgumentFun(ref tempInt); 70 | Debug.Log("1.3. Output: \t" + tempInt); 71 | } 72 | 73 | 74 | 75 | //------------------- Section 2 test functions ------------------------------------------------- 76 | private void Run2_1() { 77 | SimpleCallback sc = new SimpleCallback(SimpleCallbackUnityFun); 78 | SimpleCallbackFun(sc); 79 | } 80 | private void SimpleCallbackUnityFun() { 81 | Debug.Log("2.1. Output: \tSimple callback."); 82 | } 83 | 84 | 85 | private void Run2_2() { 86 | SimpleReturnCallback sc = new SimpleReturnCallback(SimpleReturnCallbackUnityFun); 87 | Debug.Log("2.2. Output: \t" + SimpleReturnCallbackFun(sc)); 88 | } 89 | private int SimpleReturnCallbackUnityFun() { 90 | return 5; 91 | } 92 | 93 | 94 | private void Run2_3() { 95 | SimpleArgCallback sc = new SimpleArgCallback(SimpleArgCallbackUnityFun); 96 | SimpleArgCallbackFun(sc); 97 | } 98 | private void SimpleArgCallbackUnityFun(int n) { 99 | Debug.Log("2.3. Output: \t" + n); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /plugin.cpp: -------------------------------------------------------------------------------- 1 | #include "plugin.h" 2 | 3 | #include 4 | 5 | extern "C"{ 6 | int DLL_EXPORT SimpleTypeReturnFun(){ //1.1. 7 | return 1; 8 | } 9 | 10 | int DLL_EXPORT SimpleTypeArgFun(int n){ //1.2. 11 | return n; 12 | } 13 | 14 | void DLL_EXPORT ReferenceArgumentFun(int &n){ //1.3. 15 | n=4; 16 | } 17 | 18 | void DLL_EXPORT SimpleCallbackFun(SimpleCallback c){ //2.1. 19 | c(); 20 | } 21 | 22 | int DLL_EXPORT SimpleReturnCallbackFun(SimpleReturnCallback c){ //2.2. 23 | return c(); 24 | } 25 | 26 | void DLL_EXPORT SimpleArgCallbackFun(SimpleArgCallback c){ //2.3. 27 | c(6); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /plugin.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #ifdef BUILD_DLL 4 | #define DLL_EXPORT __declspec(dllexport) 5 | #else 6 | #define DLL_EXPORT __declspec(dllimport) 7 | #endif 8 | 9 | extern "C"{ 10 | int DLL_EXPORT SimpleTypeReturnFun(); //1.1. 11 | int DLL_EXPORT SimpleTypeArgFun(int n); //1.2. 12 | void DLL_EXPORT ReferenceArgumentFun(int& n); //1.3. 13 | 14 | typedef void (*SimpleCallback)(); 15 | void DLL_EXPORT SimpleCallbackFun(SimpleCallback c); //2.1. 16 | typedef int (*SimpleReturnCallback)(); 17 | int DLL_EXPORT SimpleReturnCallbackFun(SimpleReturnCallback c); //2.2. 18 | typedef void (*SimpleArgCallback)(int n); 19 | void DLL_EXPORT SimpleArgCallbackFun(SimpleArgCallback c); //2.3. 20 | } 21 | --------------------------------------------------------------------------------