- Home /
Custom dll does not work with build -> works with editor - Unity 2019.4.38f
I must be misunderstanding the documentation.
I have the following code in a dll compiled in c++
:
#include <cmath>
#include <limits>
struct Vector2
{
float x = 0, y = 0;
Vector2 (float x, float y) : x(x), y(y) {}
Vector2 () = default;
};
struct Vector3
{
float x = 0, y = 0, z = 0;
Vector3 (float x, float y, float z) : x(x), y(y), z(z) {}
Vector3 () = default;
};
extern "C"__declspec(dllexport) float Radians(Vector2 a, Vector2 b)
{
return atan2f(a.y - b.y, a.x - b.x);
}
extern "C"__declspec(dllexport) float Angle(Vector2 from, Vector2 to)
{
float angle = Radians(from, to) * (180.0f / (22.0f/7.0f));
if (angle < 0.0f)
angle += 360.0f;
return angle;
}
I use the following g++ command to export:
g++ -shared -o tMath.dll tMath.cpp -Wl,--out-implib,tMath.dll
The files are placed in a folder called Plugins
.
I Import the functions into my Math.cs Library Like so:
[DllImport("Assets/Plugins/tMath.dll")]
public static extern float Angle(Vector2 a, Vector2 b);
I am using the code in color tool I created. I don't need to use it, but I want to grow in more than one language as I develop, which is why I am trying to use c++
for my math functions.
Important details:
The dll works fine in the editor.
The dll works fine when it is first built. The second run and subsequent runs does/do not.
The code works if I dont use the dll and instead use a
C#
variation of the same code.
However, it does not work when built, on any of my test computers. What am I missing?
This is a link to a video showing you the "second run" portion of the issue. This video clearly shows me that the dll is linked when running it the first time. After that there is no link, which points to the dll reference being still in memory, and the program accessing it. After closing and reopening, that reference is no longer there.
So there is obviously an issue with locating the DLL. I just cannot grasp what I am doing wrong.
I am assuming by the lack of replies this is a complicated issue.
Your answer
Follow this Question
Related Questions
Unity plugins 0 Answers
DLLNotFoundException 0 Answers
Problem importing a mixed-mode (C++/CLI) library 2 Answers
How to implement Emscripten linking support for WebGl build? 0 Answers
Can I use multiple plugins in the Unity 5 32-bit editor? 2 Answers