UE4引用第三方dll,打包时自动拷贝

项目或者差距的*.Build.cs文件会在项目build或者sln刷新的时候进行调用,在build.cs里进行引用文件的拷贝就可以了,以下是示例项目,重点注意:拷贝后需要将文件添加进运行时依赖,这样在项目打包exe 的时候才能查找到依赖,拷贝到exe运行环境种,否则只是拷贝到了工程的执行目录,打包exe后依然会丢失

using UnrealBuildTool;
using System.IO;

public class VideoRecorder : ModuleRules
{
     void CopyFileToRuntime(){
        string[] files=new string[]{"avcodec-58.dll","avdevice-58.dll","avfilter-7.dll","avformat-58.dll","avutil-56.dll","postproc-55.dll","swresample-3.dll","swscale-5.dll",};
        var pdir=Path.GetFullPath(
                  Path.Combine(ModuleDirectory, "../../")
            );
        var targetPath=Path.Combine(pdir, "Binaries/Win64");
        var basePath=Path.Combine(pdir, "../../Plugins/UEVideoRecorder-master/ThirdParty");
        System.Console.WriteLine(pdir);
        foreach (var item in files)
        {
            var t=Path.Combine(targetPath, item);
            var b=Path.Combine(basePath, item);
           if(!File.Exists(t)){
               File.Copy(b,t);
               System.Console.WriteLine("拷贝运行时dll"+item);
           }
           //这里表示将文件添加到运行时依赖,这样在build为exe的时候会查找依赖,将文件一起拷贝,否则不会拷贝,会丢失dll
           RuntimeDependencies.Add(t);
        }
    }
    public VideoRecorder(ReadOnlyTargetRules Target) : base(Target)
    {
        //刷新项目sln,打开uproject项目的时候执行到此方法
        Type = ModuleType.External;

        const string LibraryPath = "../ThirdParty/VideoRecorder";

        PublicIncludePaths.Add(Path.Combine(LibraryPath, "include"));

        if (Target.Platform == UnrealTargetPlatform.Win64)
        {
            PublicLibraryPaths.Add(Path.Combine(LibraryPath, "lib", "x64", "Release"));
            PublicAdditionalLibraries.Add("VideoRecorder.lib");
            CopyFileToRuntime();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq769919187/article/details/122255273