Unity配置Xcode脚本

Unity制作IPA安装包时,由于项目的需要,接入第三方的SDK,需要导入不同的第三方库,如果每次都手动导入比较浪费时间,而且有可能出错的风险,这里制作一个脚本,通过Unity提供的方法OnPostprocessBuild(在Build时会调用)。

大致思路:在生成的配置文件中,写入自己需要库或者配置

话不多说,代码走起来,在代码中加入注释,如果不懂的可以留言,大家相互学习一下。

#if UNITY_IOS
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEditor;
using System.IO;

namespace GoldenDungeon {
public class XcodeSettingsPostProcesser {
    [PostProcessBuildAttribute(0)]
    public static void OnPostprocessBuild(BuildTarget target, string pathToBuiltProject) {
        if (target != BuildTarget.iOS) {
            return;
        }
        string projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
        PBXProject pbxProject = new PBXProject();
        pbxProject.ReadFromFile(projectPath);
        //设置安装包的名字
        string targetGuid = pbxProject.TargetGuidByName("Unity-iPhone");
        //在Other Linker Flags 添加 -ObjC
        pbxProject.AddBuildProperty(targetGuid,"OTHER_LDFLAGS","-ObjC");
        //设置Enable Bitcode 为No
        pbxProject.SetBuildProperty(targetGuid,"ENABLE_BITCODE","No");
        //添加.framework库
        pbxProject.AddFrameworkToProject(targetGuid,"Security.framework",false);
        pbxProject.AddFrameworkToProject(targetGuid,"CoreGraphics.framework",false);
        pbxProject.AddFrameworkToProject(targetGuid,"WebKit.framework",false);
        pbxProject.AddFrameworkToProject(targetGuid,"CoreTelephony.framework",true);
        
        pbxProject.AddFileToBuild(targetGuid, pbxProject.AddFile("usr/lib/libsqlite3.0.tbd", "Frameworks/libsqlite3.0.tbd", PBXSourceTree.Sdk));
        pbxProject.AddFileToBuild(targetGuid, pbxProject.AddFile("usr/lib/libz.tbd", "Frameworks/libz.tbd", PBXSourceTree.Sdk));
        pbxProject.AddFileToBuild(targetGuid, pbxProject.AddFile("usr/lib/libc++.tbd", "Frameworks/libc++.tbd", PBXSourceTree.Sdk));
        //写入文件,如果不写入不会生效的
        File.WriteAllText(projectPath,pbxProject.WriteToString());
        //为plist写入项目中需要的配置 一般项目中接入微信会需要
        string plistPath = System.IO.Path.Combine(pathToBuiltProject, "Info.plist");
        PlistDocument plist = new PlistDocument();
        plist.ReadFromFile(plistPath);
        PlistElementDict dict = plist.root.CreateDict("NSAppTransportSecurity");
        dict.SetBoolean("NSAllowsArbitraryLoads", true);
        //iOS跳转需要配置
        PlistElementArray array = plist.root.CreateArray ("LSApplicationQueriesSchemes");
        array.AddString("weixinULAPI");
        array.AddString("weixin");
        
        PlistElementArray urlTypes = plist.root.CreateArray("CFBundleURLTypes");
        dict = urlTypes.AddDict();
        dict.SetString("CFBundleURLName", "weixin");
        PlistElementArray schemes = dict.CreateArray("CFBundleURLSchemes");

        schemes.AddString("APPID");

        dict = urlTypes.AddDict();
        dict.SetString("CFBundleURLName", "pay");
        schemes = dict.CreateArray("CFBundleURLSchemes");
        schemes.AddString("xxx-xxxx");
        
        dict = urlTypes.AddDict();
        dict.SetString("CFBundleURLName", "app");
        schemes = dict.CreateArray("CFBundleURLSchemes");
        //bundleID
        schemes.AddString("com.xxx.xx.xxxxx");
        //写入plist
        plist.WriteToFile (plistPath);
    }
}
}
#endif

猜你喜欢

转载自blog.csdn.net/qq_33515628/article/details/106361332