Plugin cross-language type conversions in unity

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.