UE5.4 添加依赖项
跳到导航
跳到搜索
一般使用 .build.cs 文件配置常规虚幻引擎C++模块,第三方库也不例外。要创建无源代码且可供其他模块消耗的模块,则如下所示创建插件的 .build.cs:
using System; using System.IO; using UnrealBuildTool; public class MyThirdPartyLibrary : ModuleRules { public MyThirdPartyLibrary(ReadOnlyTargetRules Target) : base(Target) { Type = ModuleType.External; // 添加需要设置的宏 PublicDefinitions.Add("WITH_MYTHIRDPARTYLIBRARY=1"); // 添加插件的包含路径 PublicIncludePaths.Add(Path.Combine(ModuleDirectory, "inc")); // 添加导入库或静态库 PublicAdditionalLibraries.Add(Path.Combine(ModuleDirectory, "lib", "foo.a")); } }
运行时依赖性
打包游戏时为在可执行文件旁暂存第三方DLL,可在 build.cs中声明其为运行时依赖性。
RuntimeDependencies.Add(Path.Combine(PluginDirectory, "Binaries/Win64/Foo.dll"));
此操作假设DLL已存在于给定目录中,插件将在该位置手动进行加载。若希望在编译时将DLL复制到可执行文件使用的相同输出目录,可通过重载 RuntimeDependencies.Add 方法执行。
RuntimeDependencies.Add("$(TargetOutputDir)/Foo.dll", Path.Combine(PluginDirectory, "Source/ThirdParty/bin/Foo.dll"));
跨平台示例
using System; using System.IO; using UnrealBuildTool; public class PLUGIN_EXAMPLE : ModuleRules { private string GetExampleLibraryPath(ReadOnlyTargetRules Target) { if (Target.Platform == UnrealTargetPlatform.Win64) { return Path.Combine(ModuleDirectory, "..", "ThirdParty", "lib_win64_example.lib"); } if (Target.Platform == UnrealTargetPlatform.Linux) { return Path.Combine(ModuleDirectory, "..", "ThirdParty", "lib_linux_example.a"); } if (Target.Platform == UnrealTargetPlatform.Mac) { return Path.Combine(ModuleDirectory, "..", "ThirdParty", "lib_mac_example.a"); } return null; } public PLUGIN_EXAMPLE(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = ModuleRules.PCHUsageMode.UseExplicitOrSharedPCHs; PrivateDependencyModuleNames.AddRange( new string[] { "CoreUObject", "Engine", "Core" } ); string LibraryPath = GetExampleLibraryPath(Target); if (LibraryPath != null) { PublicAdditionalLibraries.Add(LibraryPath); } } }