using UnityEditor; using UnityEngine; using UnityEditor.Callbacks; using System.IO; using System.Collections; using System.Collections.Generic; using ChillyRoom.UnityEditor.iOS.Xcode; public class NewBehaviourScript { [PostProcessBuild(100)] public static void OnPostprocessBuild(BuildTarget buildTarget, string buildPath) { if (buildTarget != BuildTarget.iOS) { return; } string projectPath = PBXProject.GetPBXProjectPath(buildPath); PBXProject project = new PBXProject(); project.ReadFromFile(projectPath); //关闭BitCode string target = project.TargetGuidByName("Unity-iPhone"); project.SetBuildProperty(target, "ENABLE_BITCODE", "NO"); //添加引用库 string targetGuid = project.TargetGuidByName("Unity-iPhone"); project.AddFrameworkToProject(targetGuid, "libc++.tbd", false); project.AddFrameworkToProject(targetGuid, "libz.tbd", false); project.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", false); project.AddFrameworkToProject(targetGuid, "CoreTelephony.framework", false); project.AddFrameworkToProject(targetGuid, "QuartzCore.framework", false); project.AddFrameworkToProject(targetGuid, "CoreText.framework", false); project.AddFrameworkToProject(targetGuid, "CoreGraphics.framework", false); project.AddFrameworkToProject(targetGuid, "UIKit.framework", false); project.AddFrameworkToProject(targetGuid, "Foundation.framework", false); project.AddFrameworkToProject(targetGuid, "CFNetwork.framework", false); project.AddFrameworkToProject(targetGuid, "CoreMotion.framework", false); project.AddFrameworkToProject(targetGuid, "AlipaySDK.framework", false); project.AddFrameworkToProject(targetGuid, "Security.framework", false); // //App名称本地化 string plistPath = Path.Combine(buildPath, "Info.plist"); PlistDocument plist = new PlistDocument(); plist.ReadFromFile(plistPath); plist.root.SetString("Bundle display name", "${CFBundleDisplayName }"); var infoDirs = Directory.GetDirectories(Application.dataPath + "/AppleDependency/"); for (var i = 0; i < infoDirs.Length; ++i) { var files = Directory.GetFiles(infoDirs[i], "*.strings"); project.AddLocalization(Path.GetFileNameWithoutExtension(files[i]), "InfoPlist.strings", "InfoPlist.strings"); } //修改UnityAppController.mm string scriptPath = buildPath + "/Classes/UnityAppController.mm"; List lines = new List(); StreamReader streamReader = new StreamReader(scriptPath); while (!streamReader.EndOfStream) { lines.Add(streamReader.ReadLine()); } streamReader.Close(); lines.Insert ( 0, "#import " ); lines.Insert ( 231, "if ([url.host isEqualToString:@\"safepay\"]) {\n" + "//跳转支付宝钱包进行支付,处理支付结果\n" + "[[AlipaySDK defaultService] processOrderWithPaymentResult: url standbyCallback:^ (NSDictionary * resultDic) {\n" + "NSLog(@\"result 1 = % @\", resultDic);\n" + " }];\n" + "}\n" ); lines.Insert ( 247, "// NOTE: 9.0以后使用新API接口\n" + "-(BOOL)application:(UIApplication*)app openURL: (NSURL*)url options: (NSDictionary*)options\n" + "{\n" + "if ([url.host isEqualToString: @\"safepay\"])\n" + "{\n" + "//跳转支付宝钱包进行支付,处理支付结果\n" + "[[AlipaySDK defaultService]\n" + "processOrderWithPaymentResult:url standbyCallback:^(NSDictionary* resultDic) {\n" + "NSLog(@\"result 2 = % @\", resultDic);\n" + "}];\n" + "}\n" + "return YES;\n" + "}\n" ); StreamWriter streamWriter = new StreamWriter(scriptPath); for (int i = 0; i < lines.Count; i++) { streamWriter.WriteLine(lines[i]); } streamWriter.Close(); //添加URLScheme PlistElementArray urlArray = plist.root.CreateArray("CFBundleURLTypes"); PlistElementDict urlDict = urlArray.AddDict(); urlDict.SetString("CFBundleURLName", "TestName"); PlistElementArray urlInnerArray = urlDict.CreateArray("TestScheme"); urlInnerArray.AddString("TestValue"); //保存Plist plist.WriteToFile(plistPath); //保存PBXProject File.WriteAllText(projectPath, project.WriteToString()); } }