Hello,
I have been trying to get a native plugin written in C++ to work in Unity.
This is my first time writing such a plugin, so I was playing around with some test code to see if I could get it working.
However, I am running into an issue I can't wrap my head around.
Unity reports an error with the use of my plugin whenever I use a C++ style convention, like new (I have no experience writing code in C, so apologies if that statement is not true).
Below is the C++ code I am using:
#define UNITY_EXPORT __declspec(dllexport)
int value = 0;
class AA {
public:
int index;
};
static AA* aaa = 0;
static void DoInit(){
aaa = new AA();
}
extern "C" {
UNITY_EXPORT void A(int x){
value = x;
}
UNITY_EXPORT int B(){
return value;
}
UNITY_EXPORT void Init(){
DoInit();
}
}
And this is the MonoBehaviour script:
public class TestDLL : MonoBehaviour {
[DllImport("libUnitySimpleTest", EntryPoint="A")]
public static extern void A(int x);
[DllImport("libUnitySimpleTest", EntryPoint="B")]
public static extern int B();
[DllImport("libUnitySimpleTest", EntryPoint="Init")]
public static extern void Init();
// Use this for initialization
void Start () {
Init ();
}
// Update is called once per frame
void Update () {
A (2);
int r = B ();
transform.position = new Vector3 (r, 0.5f, 0);
}
}
The error message is: "Failed to load 'Assets/Plugin/[pluginname].dll' with error '%1 is not a valid Win32 application.
After a lot of trial and error, I figured out that this error happens when I do the "aaa = new AA();" call. If I comment this line, it works fine. Likewise, if I try to for example create a char* array using the new keyword, the error appears, but creating the array using malloc() works fine.
Any idea why this happens or how I can fix it?
If it matters, I am not using the Pro version of Unity.
Thanks in advance.
Yme
↧