# 4. iOS平台导入

利用Unity编译出Xcode工程后,打开Xcode工程,按各第三方平台指引引入其需要的SDK并链接其依赖的系统framework及lib等。 在Unity的sdk包里已经包含所有的第三方Framework包,可根据需要删除不需要的sdk包. 根据以上罗列的信息引入各第三方网络所需SDK并根据各SDK要求引入系统framework和lib之后需要在Build Settings进行以下配置:

  1. 在Xcode工程的Build Settings中,搜索bitcode,并将其值改为NO(当前版本Unity(2018.02)编译出来的Xcode工程中,此项设置默认为Yes),如图:

1

  1. 在Xcode 工程的Build Settings中,搜索runpath search paths,并将其值改为@executable_path/Frameworks 如图:

2

  1. 在Xcode 工程的Build Settings中,搜索other linker flags,在默认值基础上增加-ObjC, -fobjc-arc 如图:

3

  1. 在Xcode 工程的Build Settings中,搜索C Language Dialect,将其值改为GNU99[-std=gnu99] 如图:

4

可以使用以下方法以编程方式完成上述所有配置 C# Editor Script:

#if (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
using UnityEditor.iOS.Xcode;
#endif

public static class MyBuildPostprocess
{
    [PostProcessBuild(999)]
    public static void OnPostProcessBuild(BuildTarget buildTarget, string path)
    {
        #if (UNITY_5 && UNITY_IOS) || UNITY_IPHONE
            if (buildTarget == BuildTarget.iOS)
            {
                string projectPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";

                PBXProject pbxProject = new PBXProject();
                pbxProject.ReadFromFile(projectPath);

                //unity 2019版使用
                // string target = pbxProject.GetUnityMainTargetGuid();
                //unity 2018,2017版可使用
                string target = pbxProject.TargetGuidByName("Unity-iPhone");            
                pbxProject.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
                pbxProject.SetBuildProperty(target, "GCC_ENABLE_OBJC_EXCEPTIONS", "YES");
                pbxProject.SetBuildProperty(target, "GCC_C_LANGUAGE_STANDARD", "gnu99");

                pbxProject.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");
                pbxProject.AddBuildProperty(target, "OTHER_LDFLAGS", "-fobjc-arc");
                pbxProject.AddFileToBuild(target, pbxProject.AddFile("usr/lib/libxml2.tbd", "Libraries/libxml2.tbd", PBXSourceTree.Sdk));
                pbxProject.AddFileToBuild(target, pbxProject.AddFile("usr/lib/libresolv.9.tbd", "Libraries/libresolv.9.tbd", PBXSourceTree.Sdk));
                pbxProject.WriteToFile (projectPath);

                var plistPath = Path.Combine(path, "Info.plist");
                PlistDocument plist = new PlistDocument();
                plist.ReadFromFile(plistPath);
                plist.root.SetString("GADApplicationIdentifier", "ca-app-pub-9488501426181082/7319780494");
                plist.root.SetBoolean("GADIsAdManagerApp", true);
                plist.WriteToFile(plistPath);
        }
        #endif
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

::: waring 注意 您需要将此代码写到C#文件并放到Unity3D IDEEditor目录下: :::

5

如果一切顺利,则应编译您的项目。