刘帅的博客

Unity2019-适配iOS

Word count: 663Reading time: 3 min
2022/11/26

背景

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

Unity 2018的结构:

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

Unity2019以后:

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

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

问题一

错误描述

1
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。

设置代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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)

1
2
3
[!] 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) ********的作用:继承上层的配置(或者基类的配置)。查看方式如图

问题二

错误描述

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

解决方式

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

1
2
3
4
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);

问题三

错误描述

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

解决方式

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

1
2
3
4
5
6
7
8
9
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://blog.mzying.com/index.php/archives/191/

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

CATALOG
  1. 1. 背景
  2. 2. 问题一
    1. 2.1. 错误描述
    2. 2.2. 解决方式
  3. 3. 问题二
    1. 3.1. 错误描述
    2. 3.2. 解决方式
  4. 4. 问题三
    1. 4.1. 错误描述
    2. 4.2. 解决方式
  5. 5. 问题四
    1. 5.1. 错误描述
    2. 5.2. 解决方式
  6. 6. 参考链接: