【UE5 C++课程系列笔记】30——自动拷贝DLL及其他资源

前言

在上一篇(【UE5 C++课程系列笔记】29------在UE中使用第三方库的流程)博客中,我们已经实现了在UE中使用外部的.dll文件。本篇博客将介绍在缺少"Binaries"和"Intermediate"文件夹的情况下,如何将.dll文件等外部资源自动拷贝,从而防止因为缺少外部资源而造成打包失败的情况

步骤

  1. 可以看到项目插件所需要的第三方库的存放路径如下

在"xx项目\Plugins\MyThirdPartyLibrary\Source\ThirdParty\MyThirdPartyLibraryLibrary\MyThirdPartyLibraryLibrary.Build.cs"文件中,可以看到之前写了如下代码,其中第22行代码的作用是:将第三方库的DLL文件添加到Unreal Engine项目的运行时依赖中,确保在打包或运行游戏时,该DLL文件会被正确地复制到输出目录中(就是将打包之前路径下的文件拷贝到打包之后对应的路径下," RuntimeDependencies.Add()"中填入的路径是打包之前的路径)

复制后的DLL文件存放路径如下

因此,如果我们把"Binaries"和"Intermediate"文件夹从插件文件夹中移除将无法正确打包。

  1. 为了解决上述问题,我们需要在"MyThirdPartyLibraryLibrary.Build.cs"文件中添加如下代码

完整代码如下,其中函数CopyDllToPluginBinaries()的作用是将指定的DLL文件复制到插件的Binaries目录中,并确保该DLL文件在Unreal Engine项目打包或运行时被正确包含。传入的参数"InFilePath"表示需要复制的DLL文件的源路径。

cs 复制代码
// Fill out your copyright notice in the Description page of Project Settings.

using System;
using System.IO;
using UnrealBuildTool;

public class MyThirdPartyLibraryLibrary : ModuleRules
{
	private void CopyDllToPluginBinaries(string InFilePath, ReadOnlyTargetRules Target)
	{
        //确定目标目录
        string TargetDirectory = Path.GetFullPath(Path.Combine(PluginDirectory, "Binaries/ThirdParty", "MyThirdPartyLibraryLibrary", "Win64"));

        //获取文件名
        string FileName = Path.GetFileName(InFilePath);

        //打印源文件路径、目标目录和文件名
        Console.WriteLine("--SourceFilePath:" + InFilePath);
		Console.WriteLine("--TargetDirectory:" + TargetDirectory + "--FileName:" + FileName);

        //创建目标目录(如果不存在)
        if (!Directory.Exists(TargetDirectory))
		{
			Directory.CreateDirectory(TargetDirectory);
		}

        //复制文件
        string TargetFilePath = Path.Combine(TargetDirectory, FileName);
		if (!File.Exists(TargetFilePath))
		{
			File.Copy(InFilePath, TargetFilePath, true);
		}

        //将复制的DLL文件添加到运行时依赖中
        RuntimeDependencies.Add("$(PluginDir)/Binaries/ThirdParty/MyThirdPartyLibraryLibrary/Win64/ExampleLibrary.dll");
    }

	public MyThirdPartyLibraryLibrary(ReadOnlyTargetRules Target) : base(Target)
	{
		Type = ModuleType.External;
		PublicSystemIncludePaths.Add("$(ModuleDir)/Public");

		if (Target.Platform == UnrealTargetPlatform.Win64)
		{
			// Add the import library
			PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory, "x64", "Release", "ExampleLibrary.lib"));

			// Delay-load the DLL, so we can load it from the right place first
			PublicDelayLoadDLLs.Add("ExampleLibrary.dll");

			CopyDllToPluginBinaries(Path.Combine(ModuleDirectory, "x64", "Release", "ExampleLibrary.dll"), Target);

        }
	}
}
  1. 此时我们再删除"Binaries"和"Intermediate"文件夹

删除后如下

重新编译一下

编译后可以重新看到创建了"Binaries"和"Intermediate"文件夹

并且在"Binaries"中包含了所需的DLL文件

  1. 打开"MyThirdPartyLibraryLibrary.Build.cs"

添加如下代码,表示只允许项目在Windows环境中打包

相关推荐
mit6.8242 小时前
[实现Rpc] 通信类抽象层 | function | using | 解耦合设计思想
c++·网络协议·rpc
laimaxgg2 小时前
Qt常用控件之单选按钮QRadioButton
开发语言·c++·qt·ui·qt5
ox00804 小时前
C++ 设计模式-命令模式
c++·设计模式·命令模式
Blasit5 小时前
C++ Qt建立一个HTTP服务器
服务器·开发语言·c++·qt·http
..过云雨5 小时前
04.类和对象(下)(初始化列表、static静态成员、友元friend[类外函数使用类私有成员]、内部类、匿名对象等)
开发语言·c++
刃神太酷啦5 小时前
树(数据结构·)
数据结构·c++·蓝桥杯c++组
清水加冰5 小时前
【算法精练】背包问题(01背包问题)
c++·算法
Mr.kanglong8 小时前
【C++】智能指针
开发语言·c++
不灭锦鲤9 小时前
c语言(函数)
c语言·c++·算法
Unique_yt9 小时前
02.19 构造函数
开发语言·c++·算法