gpt4 book ai didi

unity-game-engine - C++ dll 的 unity3d 插件中的 DllNotFoundException

转载 作者:行者123 更新时间:2023-12-02 02:46:53 24 4
gpt4 key购买 nike

我正在开发 Unity Plugin 项目,并尝试从 c# 文件导入 c++ native dll。但我不断收到 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 异常。

任何帮助,非常感谢!!

最佳答案

感谢这个Unity forum post我想出了一个很好的解决方案,它在运行时修改 PATH -环境变量:

  • 所有 DLL(Unity 与之交互的 DLL 及其依赖的 DLL)放入 Project\Assets\Wherever\Works\Best\Plugins 中。
  • 将以下静态构造函数放入使用该插件的类中:

    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] 添加到类中,以确保构造函数为 run at editor launch :

     [InitializeOnLoad]
    public class MyClassWhichUsesPlugin
    {
    ...
    static MyClassWhichUsesPlugin() // static Constructor
    {
    ...
    }
    }

使用此脚本,无需复制 DLL。 Unity 编辑器在 Assets/.../Plugins/... 文件夹中找到它们,可执行文件在 ..._Data/Plugins 目录中找到它们(其中它们在构建时会自动复制)。

关于unity-game-engine - C++ dll 的 unity3d 插件中的 DllNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10003028/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com