Raf*_*ñoz 9 c++ unity-game-engine 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);
}
Run Code Online (Sandbox Code Playgroud)
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);
}
Run Code Online (Sandbox Code Playgroud)
然后我想将我的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);
};
}
}
Run Code Online (Sandbox Code Playgroud)
和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);
}
}
Run Code Online (Sandbox Code Playgroud)
我建立它,它变成了一个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
Run Code Online (Sandbox Code Playgroud)
DllNotFoundException:/Assets/plugins/AEDwrapper.dll
我知道在我的代码的早期步骤中我可能是错的,但你曾经在那之前做过吗??? 我有点迷茫.
先感谢您.
拉斐尔.
编辑:
我尝试过一个简单的函数(例如:int gimmetwo();在标题中:
int gimmetwo() {
return 2;
}
Run Code Online (Sandbox Code Playgroud)
结果是一样的:(
编辑!!!! 7月11日
在看到像@ k-gkinis和@ nikola-dimitroff这样的答案之后,我明白我必须为不同的环境编译不同的库.
在这种情况下,我为Mac OS x创建了一个Plugin.bundle:
Plugin.pch:
extern "C" {
string getmytext();
}
Run Code Online (Sandbox Code Playgroud)
Plugin.cpp
#include "Plugin.pch"
string getmytext() {
return "weowoewoeowoew";
}
Run Code Online (Sandbox Code Playgroud)
我构建它并将其导入Unity3D:
[DllImport ("AED")]
private static extern string getmytext ();
static void obtenerPuntos()
{
Debug.Log (getmytext());
}
Run Code Online (Sandbox Code Playgroud)
但我仍然得到同样的错误!!

我猜这个插件在正确的位置

怎么了?
如此处所示,请确保您在插件的检查器中具有正确的选项。
另外,因为您的目标是 iOS,所以您必须静态链接该插件,因此您的导入属性应该是:
[DllImport ("__Internal")]
Run Code Online (Sandbox Code Playgroud)
你可以写
#if UNITY_IPHONE || UNITY_XBOX360
[DllImport ("__Internal")]
#else
[DllImport ("PluginName")]
#endif
Run Code Online (Sandbox Code Playgroud)
手册中的注释:
“iOS 原生插件只有在部署到实际设备上时才能调用,因此建议使用额外的 C# 代码层包装所有原生代码方法。此代码应检查 Application.platform 并仅在应用程序运行时调用原生方法设备;当应用程序在编辑器中运行时,可以返回虚拟值。”
所以:
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;
}
Run Code Online (Sandbox Code Playgroud)
然后从代码中的某个位置调用 getmytextWrapper 。
(我没有xcode,所以在ios上我帮不了你太多)
PS DLLImport 位于以 private static extern 开头的代码片段之上
| 归档时间: |
|
| 查看次数: |
2416 次 |
| 最近记录: |