# Unity3D学习（九）：C#和C++的相互调用

1.新建一个C++空项目

2. 新建一个Unity空项目

1.C#调用C++

假设有这么一个需求：我想通过让C#调用C++的接口计算两个物体之间的平面距离（xy坐标系）。

首先，我们在C++项目DllInterface.h头文件中添加如下代码

#pragma once
#include<math.h>
#include<string.h>
#include<iostream>
#define _DllExport _declspec(dllexport) //使用宏定义缩写下

extern "C"
{
float _DllExport GetDistance(float x1, float y1, float x2, float y2);
}

float GetDistance(float x1, float y1, float x2, float y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class DllInterface {

[DllImport("CppInterface")]
public static extern float GetDistance(float x1, float y1, float x2, float y2);
}　　

using UnityEngine;

public class Main : MonoBehaviour {

private GameObject cube1;
private GameObject cube2;
// Use this for initialization
void Start () {
cube1 = GameObject.Find("Cube1");
cube2 = GameObject.Find("Cube2");
PrintDistanceViaUnity();
}

void PrintDistanceViaUnity()
{
var pos1 = cube1.transform.position;
var pos2 = cube2.transform.position;
Debug.Log("This is a log from Unity");
Debug.Log("Distance:" + DllInterface.GetDistance(pos1.x, pos1.y, pos2.x, pos2.y));
}
}

2.C++调用C#

#pragma once
#include<math.h>
#include<string.h>
#include<iostream>
#define _DllExport _declspec(dllexport)

#define UnityLog(acStr)  char acLogStr[512] = { 0 }; sprintf_s(acLogStr, "%s",acStr); Debug::Log(acLogStr,strlen(acStr));

extern "C"
{
//C++ Call C#
class Debug
{
public:
static void (*Log)(char* message,int iSize);
};

// C# call C++
void _DllExport InitCSharpDelegate(void (*Log)(char* message, int iSize));

float _DllExport GetDistance(float x1, float y1, float x2, float y2);
}

#include "DllInterface.h"

void(*Debug::Log)(char* message, int iSize);

float GetDistance(float x1, float y1, float x2, float y2)
{
UnityLog("GetDistance has been called");
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}

void InitCSharpDelegate(void(*Log)(char* message, int iSize))
{
Debug::Log = Log;
UnityLog("Cpp Message:Log has initialized");
}


using AOT;
using System;
using System.Runtime.InteropServices;
using UnityEngine;

public class DllInterface {

[DllImport("CppInterface")]
public static extern float GetDistance(float x1, float y1, float x2, float y2);

public delegate void LogDelegate(IntPtr message, int iSize);

[DllImport("CppInterface")]
public static extern void InitCSharpDelegate(LogDelegate log);

//C# Function for C++'s call
[MonoPInvokeCallback(typeof(LogDelegate))]
public static void LogMessageFromCpp(IntPtr message, int iSize)
{
Debug.Log(Marshal.PtrToStringAnsi(message, iSize));
}
}

using UnityEngine;

public class Main : MonoBehaviour {

private GameObject cube1;
private GameObject cube2;
// Use this for initialization
void Start () {
cube1 = GameObject.Find("Cube1");
cube2 = GameObject.Find("Cube2");
//pass C#'s delegate to C++
DllInterface.InitCSharpDelegate(DllInterface.LogMessageFromCpp);
PrintDistanceViaUnity();
}

void PrintDistanceViaUnity()
{
var pos1 = cube1.transform.position;
var pos2 = cube2.transform.position;
Debug.Log("This is a log from Unity");
Debug.Log("Distance:" + DllInterface.GetDistance(pos1.x, pos1.y, pos2.x, pos2.y));
}
}

Exampe项目下载

extern "c"用法解析     作者:JasonDing

posted @ 2019-03-27 16:42  0kk470  阅读(7127)  评论(0编辑  收藏  举报