iOS适配Unity-2019

  • Post author:
  • Post category:其他




iOS适配Unity-2019



背景

由于2019起,Unity的Xcode工程,更改了项目结构。

Unity 2018的结构:

请添加图片描述

可以看Targets只有一个Unity-iPhone,Unity-iPhone直接依赖管理三方库。

Unity2019以后:

请添加图片描述

Targets多了一个UnityFramework,UnityFramework管理三方库,Unity-iPhone依赖于UnityFramwork。

所以升级后,会有若干的问题,以下是对问题的解决方式。



问题一



错误描述

error: exportArchive: Missing signing identifier at "/var/folders/fr//T/XcodeDistPipeline.~~~/Root/Payload/XX.app/Frameworks/UnityFramework.framework/Frameworks/libswiftAVFoundation.dylib".



解决方式

此问题是由于

ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES

在UnityFramework为YES导致,需要设置为NO。

正确的设置为:

Unity-iPhone的

ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES

为YES。

Unity-UnityFramework的

ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES

为NO。

请添加图片描述

设置代码如下:

var projPath = UnityEditor.iOS.Xcode.PBXProject.GetPBXProjectPath(projectPath);
var xcodeProject = new UnityEditor.iOS.Xcode.PBXProject();
xcodeProject.ReadFromFile(projPath);
                
string xcodeTarget = xcodeProject.GetUnityMainTargetGuid();
var unityTargetGuid = xcodeProject.GetUnityFrameworkTargetGuid();                
string projectGuid = xcodeProject.ProjectGuid();

//设置Project
xcodeProject.SetBuildProperty(projectGuid, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");
//设置Unity-iPhone
xcodeProject.SetBuildProperty(unityTargetGuid, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");//设置Project
//设置UnityFramework
xcodeProject.SetBuildProperty(xcodeTarget, "ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES", "NO");

设置中如果报如下错误,可以采用设置为$(inherited)

[!] The Unity-iPhone [Release] target overrides the ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES build setting defined in Pods/Target Support Files/Pods-Unity-iPhone/Pods-Unity-iPhone.release.xcconfig'. This can lead to problems with the CocoaPods installation     
- Use the $(inherited)` flag, or
- Remove the build settings from the target.


ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES

的作用:Xcode把Swift的标准库嵌入到Target中。


$(inherited)

********的作用:继承上层的配置(或者基类的配置)。查看方式如图

请添加图片描述



问题二



错误描述

Xcode build Error : 'UnityFramework/UnityFramework.h' file not found



解决方式

我在Xcode 14.1上未发现此问题,但是在Xcode 13.2上发现了此问题。解决方式为修改为相对引用

string mainAppPath = Path.Combine(projectPath, "MainApp", "main.mm");
string mainContent = File.ReadAllText(mainAppPath); 
string newContent = mainContent.Replace("#include <UnityFramework/UnityFramework.h>", @"#include ""../UnityFramework/UnityFramework.h""");
File.WriteAllText(mainAppPath, newContent);



问题三



错误描述

level=fatal msg="Please provide an auth token with USYM_UPLOAD_AUTH_TOKEN environment variable"



解决方式

如果没有使用到这个服务,可以设置一个假的参数,2019以前的Unity只需要对主Target设置即可,但是2019及以后的版本,要对UnityFramework也要设置下。

var projPath = UnityEditor.iOS.Xcode.PBXProject.GetPBXProjectPath(projectPath);
var xcodeProject = new UnityEditor.iOS.Xcode.PBXProject();
xcodeProject.ReadFromFile(projPath);
                
string xcodeTarget = xcodeProject.GetUnityMainTargetGuid();
var unityTargetGuid = xcodeProject.GetUnityFrameworkTargetGuid();

xcodeProject.SetBuildProperty(xcodeTarget, "USYM_UPLOAD_AUTH_TOKEN", "FakeToken");
xcodeProject.SetBuildProperty(unityTargetGuid, "USYM_UPLOAD_AUTH_TOKEN", "FakeToken");



问题三



错误描述

在Unity端,发现对iOS工程的buildSetting设置无效。



解决方式

此问题的排查思路,可以尝试修改

[PostProcessBuild(999)]

的优先级试下,有可能为其他的SDK把起进行了再次修改。

Apple 参数设置参考:

https://developer.apple.com/library/archive/documentation/DeveloperTools/Reference/XcodeBuildSettingRef/1-Build_Setting_Reference/build_setting_ref.html



参考链接:


https://lsnumber1.github.io/2022/11/26/Unity2019-%E9%80%82%E9%85%8DiOS/#/%E8%83%8C%E6%99%AF


https://blog.mzying.com/index.php/archives/191/


https://forum.unity.com/threads/xcode-build-error-unityframework-unityframework-h-file-not-found.838318/



版权声明:本文为qin_shi原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。