11/08/2024 16:35:55
解决方案
目前涉及到的渠道有 Facebook、Line、Garena
MSDKGarena: 在5.17及之后的版本, 需要处理UnrealEngine的swift混编问题
MSDKFacebook:在5.15及之后的版本,需要处理UnrealEngine的swift混编问题
MSDKLine: 在5.7及之后的版本, 需要处理UnrealEngine的swift混编问题
背景 UnrealEngine引擎通常版本在iOS平台不支持Objective-c和Swift的混编,当前并未找到官方关于swift混编问题的解决方案,以下方法是基于网上经验解决混编问题并验证可行的方案。
配置操作 以下以UE4.23来作为示例
1. 修改XcodeProject.cs文件 /Applications/UnrealEngine/UE_4.23/Engine/Source/Programs/UnrealBuildTool/ProjectFiles/Xcode/XcodeProject.cs 修改如下: 在函数:
private void AppendProjectBuildConfiguration(StringBuilder Content, string ConfigName, string ConfigGuid)
中添加如下代码:
// Enable Swift
Content.Append("\t\t\t\tCLANG_ENABLE_MODULES = YES;" + ProjectFileGenerator.NewLine);
Content.Append("\t\t\t\tSWIFT_VERSION = 5.0;" + ProjectFileGenerator.NewLine);
Content.Append("\t\t\t\tLIBRARY_SEARCH_PATHS = \"$(TOOLCHAIN_DIR)/usr/lib/swift/$(PLATFORM_NAME)\";" + ProjectFileGenerator.NewLine);
if (ConfigName == "Debug")
{
Content.Append("\t\t\t\tSWIFT_OPTIMIZATION_LEVEL = \"-Onone\";" + ProjectFileGenerator.NewLine);
}
Content.Append("\t\t\t\tALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;" + ProjectFileGenerator.NewLine);
Content.Append("\t\t\t\tEMBEDDED_CONTENT_CONTAINS_SWIFT = YES;" + ProjectFileGenerator.NewLine);
参照如下:
2. 修改IOSToolChain.cs文件 /Applications/UnrealEngine/UE_4.23/Engine/Source/Programs/UnrealBuildTool/Platform/IOS/IOSToolChain.cs 修改如下: 在函数:
string GetLinkArguments_Global(LinkEnvironment LinkEnvironment)
中添加如下代码
// enable swift support
Result += " -rpath \"/usr/lib/swift\"";
Result += " -rpath \"@executable_path/Frameworks\"";
// /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/lib/swift/
String swiftLibPath = String.Format(" -L {0}Platforms/{1}.platform/Developer/SDKs/{1}{2}.sdk/usr/lib/swift",
Settings.Value.XcodeDeveloperDir, bIsDevice? Settings.Value.DevicePlatformName : Settings.Value.SimulatorPlatformName, Settings.Value.IOSSDKVersion);
Result += swiftLibPath;
Log.TraceInformation("Add swift lib path : {0}", swiftLibPath);
///Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos
swiftLibPath = String.Format(" -L {0}Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/{1}",
Settings.Value.XcodeDeveloperDir, bIsDevice? Settings.Value.DevicePlatformName.ToLower() : Settings.Value.SimulatorPlatformName.ToLower());
Result += swiftLibPath;
Log.TraceInformation("Add swift lib path : {0}", swiftLibPath);
///Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.0/iphoneos
swiftLibPath = String.Format(" -L {0}Toolchains/XcodeDefault.xctoolchain/usr/lib/swift-5.0/{1}",
Settings.Value.XcodeDeveloperDir, bIsDevice? Settings.Value.DevicePlatformName.ToLower() : Settings.Value.SimulatorPlatformName.ToLower());
Result += swiftLibPath;
示例:
如果在 XCode 12 上编译,需要在上面的基础上做以下调整:
// 该行代码需要前置(前置的代码位置见下面示例图片)
// Added by uwellpeng: enable swift support, make sure '/usr/lib/swift' goes before '@executable_path/Frameworks'
Result += " -rpath \"/usr/lib/swift\"";
// XCode 12 多了swiftcompatabiliy51 的库,需要新增以下代码
if (Settings.Value.IOSSDKVersionFloat >= 14.0f)
{
Result += String.Format(" -lswiftCompatibility51");
}
示例:
3. 重新编译UBT
使用 msbuild 工具重新编译 UnrealBuildTool ,即在/Applications/UnrealEngine/UE_4.23/Engine/Source/Programs/UnrealBuildTool 目录运行 Terminal 指令 msbuild 来重新编译
完成上述三个步骤即可在解决UnrealEngine上swift的混编问题
All rights reserved.