Unity supports Plugin, and there is some code that we can write in C + + to Plugin for C # calls, but the type conversion between different languages can be very tangled. For example, what is the string in C # to C + +? What is the string in C + + inside C #?
Example from Unity official
C + + platform code is as follows:
Win32/64
#if _msc_ver//This is defined if 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//------------------------------------------------------------------------//Plugin itself Link following functions C-style (required for plugins) extern "C" {//The functions we'll call from Unity.//const EXPO Rt_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
Android
/*this is a simple plugin, a bunch of functions that does simple Things.*/extern "C" {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;}}
Mac/ios
extern "C" {const char* Printhello (); int printanumber (); int addtwointegers (int, int); float addtwofloats (float, float);}
/*this is a simple plugin, a bunch of functions that does simple things.*/#include "plugin.pch" const char* Printhello () {Retu RN "Hello";} int Printanumber () {return 5;} int addtwointegers (int a, int b) {return a + B;} Float Addtwofloats (float A, float b) {return a + B;}
C # calling Code
Using unityengine;using system.collections;using system;using System.runtime.interopservices;public class Pluginimport:monobehaviour {//lets Make we calls from the Plugin[dllimport ("Asimpleplugin")]private static extern int Printanumber (); [DllImport ("Asimpleplugin")]private static extern IntPtr Printhello (); [DllImport ("Asimpleplugin")]private static extern int addtwointegers (int i1,int i2); [DllImport ("Asimpleplugin")]private static extern float addtwofloats (float f1,float f2); void Start () {Debug.Log ( Printanumber ());D Ebug. Log (Marshal.ptrtostringauto (Printhello ()));D Ebug. Log (Addtwointegers (2,2));D Ebug. Log (Addtwofloats (2.5f,4f));}}
As mentioned in Unity3d doc, type conversions between managed and unmanaged can be referenced in the documentation on MSDN.
Https://msdn.microsoft.com/en-us/library/fzhhdwae.aspx
How to use Native library refer to Mono's documentation
http://www.mono-project.com/docs/advanced/pinvoke/
The above two pages document provides a detailed description of the conversion between native library usage and hosting versus unmanaged.
Plugin cross-language type conversions in unity