我应该如何从Unity3D调用我的C ++函数

时间:2015-07-04 21:43:32

标签: c++ unity3d unityscript

这有点困难,因为我甚至不知道我的C ++是否得到了很好的实现。

我想从Unity3D调用一个函数,我传递了一些参数float *points,我的C ++写入了该指针。

好吧,从C ++部分开始,我写道:

main_function.cpp 这是我在C ++中的主要功能,例如,它可以完美地用作二进制文件。

#include "main_function.h"

extern "C" void getPoints (uchar *data, float *points, int rows, int cols, const char *trackername) {
    //load detector model
    face_tracker tracker = load_ft<face_tracker>(trackername);

    //create tracker parameters
    face_tracker_params p; p.robust = false;
    p.ssize.resize(3);
    p.ssize[0] = Size(21,21);
    p.ssize[1] = Size(11,11);
    p.ssize[2] = Size(5,5);

    Mat im = Mat(Size(cols, rows), CV_8U, data);

    if(tracker.track(im,p))
        tracker.draw(im, points);

}

main_function.h

#include "opencv_hotshots/ft/ft.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

extern "C" {
    void getPoints (uchar *data, float *points, int rows, int cols, const char *trackername);
}

然后我想将我的C ++代码包装到C#中,所以我创建了:

AEDwrapper.h

// AEDwrapper.h
#pragma once

#ifdef TESTFUNCDLL_EXPORT
#define TESTFUNCDLL_API __declspec(dllexport) 
#else
#define TESTFUNCDLL_API __declspec(dllimport) 
#endif

using namespace System;
#include "stdafx.h"
#include "C:\Users\Rafael\Desktop\AEDlibrary\main_function.h"
extern "C" {
    namespace AEDwrapper {

        TESTFUNCDLL_API public ref class AED
        {
            void getPointsAED (uchar *data, float *points, int rows, int cols, const char *trackername);
        };
    }
}

AEDwrapper.cpp

// Archivo DLL principal.    
#include "stdafx.h"
#include <ctime>
#include <iostream>
#include <chrono>
#include <sys/timeb.h>

#include "AEDwrapper.h"

using namespace std::chrono;

extern "C" {
    void AEDwrapper::AED::getPointsAED (uchar *data, float *points, int rows, int cols, const char *trackername){
        getPoints(data, points, rows, cols, trackername);
    }
}

我构建了它,它变成了AEDwrapper.dll

现在在Unity3D中,我尝试在C#部分(脚本)中执行:

[DllImport ("/Assets/plugins/AEDwrapper.dll")]   
// or [DllImport ("AEDwrapper")]
private static extern unsafe void getPointsAED (byte* data, float* points, int rows, int cols, sbyte* trackername); 
//DECLARA METODO DEL DLL
  

DllNotFoundException:/Assets/plugins/AEDwrapper.dll

我知道在我的代码的早期步骤中我可能是错的,但你曾经在那之前做过吗???我有点失落。

提前谢谢。

圣拉斐尔。

编辑:

我尝试过一个简单的函数(例如:标题中的int gimmetwo();和:

int gimmetwo() {
    return 2;
}

,结果是一样的:(

编辑!!!! 7月11日

在看到像@ k-gkinis和@ nikola-dimitroff这样的答案后,我明白我必须为不同的环境编译不同的库。

在这种情况下,我为Mac OS x创建了一个Plugin.bundle:

Plugin.pch

extern "C" {
    string getmytext();
}

Plugin.cpp

#include "Plugin.pch"
string getmytext() {
    return "weowoewoeowoew";
}

我构建了它并将其导入Unity3D:

[DllImport ("AED")]
private static extern string getmytext ();

static void obtenerPuntos()
{
    Debug.Log (getmytext());
}

但我仍然得到同样的错误!!

enter image description here

我认为该插件位于正确的位置

enter image description here

怎么了?

1 个答案:

答案 0 :(得分:3)

here所示,请确保插件的检查器中有正确的选项。

此外,由于您要定位iOS,因此您必须静态链接该插件,因此您的导入属性应为:

[DllImport ("__Internal")]
你可以写

   #if UNITY_IPHONE || UNITY_XBOX360

   [DllImport ("__Internal")]

   #else

   [DllImport ("PluginName")]

   #endif

手册中的注释:

&#34; iOS本机插件只能在实际设备上部署时调用,因此建议使用额外的C#代码层包装所有本机代码方法。此代码应检查Application.platform并仅在应用程序在设备上运行时调用本机方法;当应用程序在编辑器中运行时,可以返回虚拟值。&#34;

所以:

private static extern string getmytext();

public static string getmytextWrapper()
{
        if (Application.platform != RuntimePlatform.OSXEditor)
               return getmytext();
        else
               Debug.Log("Can't call native iOS plugin on other platforms.");
       return string.Empty;
}

然后从代码中的某个地方调用getmytextWrapper。

(我没有xcode,所以我对ios的帮助不大)

PS DLLImport位于以private static extern

开头的代码段之上