Unity 打包到XCode自動化設定引數
阿新 • • 發佈:2022-04-01
[PostProcessBuild] public static void OnPostprocessBuild(BuildTarget buildTarget, string buildPath) { if (buildTarget != BuildTarget.iOS) return; string pbxProjPath = PBXProject.GetPBXProjectPath(buildPath); var pbxProject = new PBXProject(); pbxProject.ReadFromString(File.ReadAllText(pbxProjPath)); string targetGuid = pbxProject.GetUnityMainTargetGuid(); string frameworkGuid = pbxProject.GetUnityFrameworkTargetGuid(); // 設定關閉Bitcode pbxProject.SetBuildProperty(targetGuid, ENABLE_BITCODE_KEY, "NO"); pbxProject.SetBuildProperty(frameworkGuid, ENABLE_BITCODE_KEY, "NO"); // xcode archive filed var token = pbxProject.GetBuildPropertyForAnyConfig(targetGuid, "USYM_UPLOAD_AUTH_TOKEN"); if (string.IsNullOrEmpty(token)) { token = "FakeToken"; } pbxProject.SetBuildProperty(targetGuid, USYM_UPLOAD_AUTH_TOKEN, token); pbxProject.SetBuildProperty(frameworkGuid, USYM_UPLOAD_AUTH_TOKEN, token); //新增系統庫(Unity預設會新增部分庫,不能新增重複的庫,否則會有問題) //例 pbxProject.AddFrameworkToProject(frameworkGuid, "CoreTelephony.framework", false); //新增tbd //例 string fileGuidSqlite = pbxProject.AddFile("usr/lib/libsqlite3.tbd", "Libraries/libsqlite3.tbd", PBXSourceTree.Sdk); //新增引用的資源 如 .bundle檔案 //例 string p = .bundle地址; string bun = pbxProject.AddFile(p, "名稱.bundle", PBXSourceTree.Absolute); pbxProject.AddFileToBuild(targetGuid, bun); //修改Info.plist檔案 SetInfoPlist(buildPath); File.WriteAllText(pbxProjPath, pbxProject.WriteToString()); UnityEngine.Debug.Log("PBXProject : ---->" + pbxProject.WriteToString()); } public static void SetInfoPlist(string buildPath) { List<string> privacySensiticeData = new List<string>(); PlistDocument plist = GetInfoPlist(buildPath); //選擇語言(字串) plist.root.SetString("NSPhotoLibraryAddUsageDescription", "此App需要您的同意,才能儲存圖片到您的相簿"); plist.root.SetString("NSPhotoLibraryUsageDescription", "此App需要您的同意才能讀取媒體資料庫"); //(bool值) plist.root.SetBoolean("ITSAppUsesNonExemptEncryption", false); //設定LSApplicationQueriesSchemes(陣列) //例 PlistElementArray loginChannelsArr; loginChannelsArr = plist.root.CreateArray("LSApplicationQueriesSchemes"); loginChannelsArr.AddString("mqqapi"); loginChannelsArr.AddString("mqq"); //配置(字典) PlistElementDict plistDic; plistDic = plist.root.CreateDict("NSAppTransportSecurity"); plistDic.SetBoolean("NSAllowsArbitraryLoads", true); // 新增 url scheme PlistElementArray urlTypes = plist.root.CreateArray("CFBundleURLTypes"); PlistElementDict wxUrl = urlTypes.AddDict(); wxUrl.SetString("CFBundleTypeRole", "Editor"); wxUrl.SetString("CFBundleURLName", "weixin"); wxUrl.SetString("CFBundleURLSchemes", "wx..............."); PlistElementArray wxUrlScheme = wxUrl.CreateArray("CFBundleURLSchemes"); wxUrlScheme.AddString( "wx..............."); plist.WriteToFile(GetInfoPlistPath(buildPath)); }