【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环境中打包

相关推荐
肆忆_6 小时前
# 用 5 个问题学懂 C++ 虚函数(入门级)
c++
不想写代码的星星10 小时前
虚函数表:C++ 多态背后的那个男人
c++
端平入洛2 天前
delete又未完全delete
c++
端平入洛3 天前
auto有时不auto
c++
哇哈哈20214 天前
信号量和信号
linux·c++
多恩Stone4 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
蜡笔小马4 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
超级大福宝4 天前
N皇后问题:经典回溯算法的一些分析
数据结构·c++·算法·leetcode
weiabc4 天前
printf(“%lf“, ys) 和 cout << ys 输出的浮点数格式存在细微差异
数据结构·c++·算法
问好眼4 天前
《算法竞赛进阶指南》0x01 位运算-3.64位整数乘法
c++·算法·位运算·信息学奥赛