How can I call my C ++ function from Unity3D

This is a bit complicated because I don’t even know if my C ++ is well implemented.

I want to call a function from Unity3D, which I pass some parameters, such as float *points , and my C ++ writes in this pointer.

Well, starting with the C ++ part, I wrote:

main_function.cpp , this is my main function in C ++, which works fine as a binary, for example.

 #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); } 

Then I want to collapse the C ++ code in C #, so I do:

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); }; } } 

and 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); } } 

I will build it and it will become in AEDwrapper.dll .

Now in Unity3D, in the C # part (script), I'm trying to do:

 [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

I know I could be wrong in the early steps of my code, but have you ever done this before? I am lost.

Thanks in advance.

Raphael.


EDIT:

I tried a simple function (ex: int gimmetwo(); in the header and:

 int gimmetwo() { return 2; } 

and the result is the same: (

CHANGE !!!! July 11th

After looking at answers like @ k-gkinis and @ nikola-dimitroff, I realized that I had to compile different libraries for different environments.

In this case, I created Plugin.bundle for Mac OS x with:

Plugin.pch :

 extern "C" { string getmytext(); } 

Plugin.cpp

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

I built it and I imported it into Unity3D:

 [DllImport ("AED")] private static extern string getmytext (); static void obtenerPuntos() { Debug.Log (getmytext()); } 

but I still get the same error!

enter image description here

The plugin is in the right place, I think

enter image description here

What's wrong?

+7
c ++ unity3d unityscript
source share
1 answer

As you can see here , make sure that you have the correct parameters in the inspector for the plugin.

Also, since you are targeting iOS, you must statically link the plugin, so your import attribute should be:

 [DllImport ("__Internal")] 

you could write

  #if UNITY_IPHONE || UNITY_XBOX360 [DllImport ("__Internal")] #else [DllImport ("PluginName")] #endif 

Note from the manual:

"Native iOS plugins can only be called when deployed on the device itself, so it’s recommended that you wrap all your code methods with an additional layer of C # code. This code should check Application.platform and call your own methods only when the application is running on the device, dummy values ​​may be returned when the application starts in the editor. "

So:

 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; } 

and then call getmytextWrapper somewhere in your code.

(I don't have xcode, so I can't help you on ios)

PS DLLImport goes above the fragment that starts with a private static extern

+3
source share

All Articles