Unity中 Plugin 跨语言 类型转换

来源:互联网 发布:a5源码下载 编辑:程序博客网 时间:2024/05/23 11:29

原文:http://blog.csdn.net/huutu/article/details/46490339


Unity 支持Plugin ,有一些代码我们可以用C++ 来编写成 Plugin 供C#调用,但是对于不同语言之间的类型转换就会很纠结。比如说 C# 里面的 string 到C++ 里面是什么?C++里面的string到C#里面是什么?


引自Unity官方的例子

C++咯平台代码如下:

Win32/64

[cpp] view plaincopy
  1. #if _MSC_VER // this is defined when compiling with Visual Studio  
  2. #define EXPORT_API __declspec(dllexport) // Visual Studio needs annotating exported functions with this  
  3. #else  
  4. #define EXPORT_API // XCode does not need annotating exported functions, so define is empty  
  5. #endif  
  6.   
  7. // ------------------------------------------------------------------------  
  8. // Plugin itself  
  9.   
  10.   
  11. // Link following functions C-style (required for plugins)  
  12. extern "C"  
  13. {  
  14.   
  15. // The functions we will call from Unity.  
  16. //  
  17. const EXPORT_API char*  PrintHello(){  
  18.     return "Hello";  
  19. }  
  20.   
  21. int EXPORT_API PrintANumber(){  
  22.     return 5;  
  23. }  
  24.   
  25. int EXPORT_API AddTwoIntegers(int a, int b) {  
  26.     return a + b;  
  27. }  
  28.   
  29. float EXPORT_API AddTwoFloats(float a, float b) {  
  30.     return a + b;  
  31. }  
  32.   
  33. // end of export C block  


Android

[cpp] view plaincopy
  1. /* 
  2.     This is a simple plugin, a bunch of functions that do simple things. 
  3. */  
  4.   
  5. extern "C" {  
  6.     const char* PrintHello(){  
  7.         return "Hello";  
  8.     }  
  9.   
  10.     int PrintANumber(){  
  11.         return 5;  
  12.     }  
  13.   
  14.     int AddTwoIntegers(int a, int b) {  
  15.         return a + b;  
  16.     }  
  17.   
  18.     float AddTwoFloats(float a, float b) {  
  19.         return a + b;  
  20.     }  
  21. }  

MAC/IOS

[cpp] view plaincopy
  1. extern "C" {  
  2.     const char* PrintHello ();  
  3.     int PrintANumber ();  
  4.     int AddTwoIntegers(intint);  
  5.     float AddTwoFloats(floatfloat);  
  6. }  

[cpp] view plaincopy
  1. /* 
  2.     This is a simple plugin, a bunch of functions that do simple things. 
  3. */  
  4.   
  5. #include "Plugin.pch"  
  6.   
  7.   
  8. const char* PrintHello(){  
  9.     return "Hello";  
  10. }  
  11.   
  12. int PrintANumber(){  
  13.     return 5;  
  14. }  
  15.   
  16. int AddTwoIntegers(int a, int b) {  
  17.     return a + b;  
  18. }  
  19.   
  20. float AddTwoFloats(float a, float b) {  
  21.     return a + b;  
  22. }  




C#调用代码

[csharp] view plaincopy
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System;  
  4. using System.Runtime.InteropServices;  
  5.   
  6. public class PluginImport : MonoBehaviour {  
  7.     //Lets make our calls from the Plugin  
  8.     [DllImport ("ASimplePlugin")]  
  9.     private static extern int PrintANumber();  
  10.       
  11.     [DllImport ("ASimplePlugin")]  
  12.     private static extern IntPtr PrintHello();  
  13.       
  14.     [DllImport ("ASimplePlugin")]  
  15.     private static extern int AddTwoIntegers(int i1,int i2);  
  16.   
  17.     [DllImport ("ASimplePlugin")]  
  18.     private static extern float AddTwoFloats(float f1,float f2);      
  19.       
  20.     void Start () {  
  21.         Debug.Log(PrintANumber());  
  22.         Debug.Log(Marshal.PtrToStringAuto (PrintHello()));  
  23.         Debug.Log(AddTwoIntegers(2,2));  
  24.         Debug.Log(AddTwoFloats(2.5F,4F));  
  25.     }  
  26. }  


在Unity3d Doc中提到,托管与非托管之间的类型转换可以参照 MSDN中的文档。

[html] view plaincopy
  1. https://msdn.microsoft.com/en-us/library/fzhhdwae.aspx  

Native Library的使用方法参照 Mono的文档

[html] view plaincopy
  1. http://www.mono-project.com/docs/advanced/pinvoke/  

上面两个网页文档很详细的介绍了Native Library使用以及托管与非托管之间的转换。
0 0
原创粉丝点击