c++ dll的unity3d插件中的DllNotFoundException [英] DllNotFoundException in unity3d plugin for c++ dll
问题描述
我正在开发 Unity Plugin 项目并尝试从 c# 文件导入 c++ 原生 dll.但我不断收到 dllnotfoundexception.
I am working on the Unity Plugin project and try to import the c++ native dll from c# file. But I keep getting dllnotfoundexception.
c++ dll 代码:
extern "C" {
extern __declspec( dllexport ) bool IGP_IsActivated();
}
c# 代码:
[DllImport("mydll")]
private static extern bool IGP_IsActivated();
Dll 就位并且 FIle.Exists 正常工作.所有依赖的 dll 都存在于相同的层次结构中,但我仍然以 dllnotfound 异常结束.
Dll is in place and FIle.Exists work properly. All dependent dlls are present at same hierarchy, but I still end up in dllnotfound exception.
任何帮助,非常感谢!!
Any help, much appreciated!!
推荐答案
感谢这个 Unity 论坛帖子 我想出了一个很好的解决方案,它在运行时修改 PATH
-环境变量:
Thanks to this Unity forum post I came up with a nice solution which modifies the PATH
-environment variable at runtime:
- 将所有的 DLL(包括 Unity 接口的 DLL 及其依赖的 DLL)放在
ProjectAssetsWhereverWorksBestPlugins
中. 将以下静态构造函数放入使用插件的类中:
- Put all DLLs (both the DLLs which Unity interfaces with and their dependent DLLs) in
ProjectAssetsWhereverWorksBestPlugins
. Put the following static constructor into a class which uses the plugin:
static MyClassWhichUsesPlugin() // static Constructor
{
var currentPath = Environment.GetEnvironmentVariable("PATH",
EnvironmentVariableTarget.Process);
#if UNITY_EDITOR_32
var dllPath = Application.dataPath
+ Path.DirectorySeparatorChar + "SomePath"
+ Path.DirectorySeparatorChar + "Plugins"
+ Path.DirectorySeparatorChar + "x86";
#elif UNITY_EDITOR_64
var dllPath = Application.dataPath
+ Path.DirectorySeparatorChar + "SomePath"
+ Path.DirectorySeparatorChar + "Plugins"
+ Path.DirectorySeparatorChar + "x86_64";
#else // Player
var dllPath = Application.dataPath
+ Path.DirectorySeparatorChar + "Plugins";
#endif
if (currentPath != null && currentPath.Contains(dllPath) == false)
Environment.SetEnvironmentVariable("PATH", currentPath + Path.PathSeparator
+ dllPath, EnvironmentVariableTarget.Process);
}
将 [InitializeOnLoad]
添加到类以确保构造函数为 在编辑器启动时运行:
Add [InitializeOnLoad]
to the class to make sure that the constructor is run at editor launch:
[InitializeOnLoad]
public class MyClassWhichUsesPlugin
{
...
static MyClassWhichUsesPlugin() // static Constructor
{
...
}
}
使用此脚本无需复制 DLL.Unity 编辑器在 Assets/.../Plugins/...
文件夹中找到它们,可执行文件在 ..._Data/Plugins
目录中找到它们(其中它们在构建时会自动复制).
With this script there is no need to copy around DLLs. The Unity editor finds them in the Assets/.../Plugins/...
-folder and the executable finds them in ..._Data/Plugins
-directory (where they get automatically copied when building).
这篇关于c++ dll的unity3d插件中的DllNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!