Unity中 Plugin 跨语言 类型转换
来源:互联网 发布:eclipse软件使用教程 编辑:程序博客网 时间:2024/06/18 05:58
Unity 支持Plugin ,有一些代码我们可以用C++ 来编写成 Plugin 供C#调用,但是对于不同语言之间的类型转换就会很纠结。比如说 C# 里面的 string 到C++ 里面是什么?C++里面的string到C#里面是什么?
引自Unity官方的例子
C++咯平台代码如下:
Win32/64
#if _MSC_VER // this is defined when 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 will call from Unity.//const EXPORT_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 do 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 do simple things.*/#include "Plugin.pch"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;}
C#调用代码
using UnityEngine;using System.Collections;using System;using System.Runtime.InteropServices;public class PluginImport : MonoBehaviour {//Lets make our 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());Debug.Log(Marshal.PtrToStringAuto (PrintHello()));Debug.Log(AddTwoIntegers(2,2));Debug.Log(AddTwoFloats(2.5F,4F));}}
在Unity3d Doc中提到,托管与非托管之间的类型转换可以参照 MSDN中的文档。
https://msdn.microsoft.com/en-us/library/fzhhdwae.aspx
Native Library的使用方法参照 Mono的文档
http://www.mono-project.com/docs/advanced/pinvoke/
上面两个网页文档很详细的介绍了Native Library使用以及托管与非托管之间的转换。
0 0
- Unity中 Plugin 跨语言 类型转换
- Unity中 Plugin 跨语言 类型转换
- C语言中类型转换
- C语言中类型转换
- C语言中类型转换问题?
- C语言中强制类型转换总结
- C语言运算中类型转换
- C 语言中强制类型转换
- C语言中数值类型转换
- C语言中类型转换问题
- c++语言中类型的转换
- Unity中的类型转换格式
- 类型转换--c语言
- C语言类型转换
- C语言类型转换
- C语言类型转换
- C语言类型转换 .
- C语言 类型转换
- Maximum Product Subarray -- leetcode
- 黑马程序员——基础知识总结_接口,多态
- 求职的途径
- Android之文件读写工具类
- Mat函数的定义与vector存储到Mat中的问题
- Unity中 Plugin 跨语言 类型转换
- 链式栈的接口实现
- C++ 虚析构函数 分析
- unity开发:Qt C++与unity之间UDP网络通信
- 四个服务器设计模型
- iOS实现tableView下拉搜索功能
- 推荐系统——SVD/SVD++
- [8] MQTT,mosquitto,Eclipse Paho---MQTT消息格式之SUBSCRIBE(消息订阅)消息分析
- opencv读取视频文件并显示的程序详细解释