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
- #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语言 类型转换
- iOS UI 07 uitableviewi3
- What Is Google C++ Mocking Framework?
- ZZULIOJ 1799: wrz的压岁钱【数学】
- [Coursera]算法基础_Week6_深搜(1)_Q2
- 根据输入的角度或弧度计算其sin值
- Unity中 Plugin 跨语言 类型转换
- 201509-《模板生成系统》
- hadoop 负载均衡
- 第十周项目2-二叉树遍历的递归算法
- 项目链接
- JS-数组
- [Coursera]算法基础_Week7_深搜(2)_Q1
- 【算法拾遗(java描述)】--- 排序算法概述
- C++ vector用法