PostProcessor.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using UnityEditor;
  2. using UnityEngine;
  3. using UnityEditor.Callbacks;
  4. using System.IO;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using ChillyRoom.UnityEditor.iOS.Xcode;
  8. public class NewBehaviourScript
  9. {
  10. [PostProcessBuild(100)]
  11. public static void OnPostprocessBuild(BuildTarget buildTarget, string buildPath)
  12. {
  13. if (buildTarget != BuildTarget.iOS)
  14. {
  15. return;
  16. }
  17. string projectPath = PBXProject.GetPBXProjectPath(buildPath);
  18. PBXProject project = new PBXProject();
  19. project.ReadFromFile(projectPath);
  20. //关闭BitCode
  21. string target = project.TargetGuidByName("Unity-iPhone");
  22. project.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
  23. //添加引用库
  24. string targetGuid = project.TargetGuidByName("Unity-iPhone");
  25. project.AddFrameworkToProject(targetGuid, "libc++.tbd", false);
  26. project.AddFrameworkToProject(targetGuid, "libz.tbd", false);
  27. project.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", false);
  28. project.AddFrameworkToProject(targetGuid, "CoreTelephony.framework", false);
  29. project.AddFrameworkToProject(targetGuid, "QuartzCore.framework", false);
  30. project.AddFrameworkToProject(targetGuid, "CoreText.framework", false);
  31. project.AddFrameworkToProject(targetGuid, "CoreGraphics.framework", false);
  32. project.AddFrameworkToProject(targetGuid, "UIKit.framework", false);
  33. project.AddFrameworkToProject(targetGuid, "Foundation.framework", false);
  34. project.AddFrameworkToProject(targetGuid, "CFNetwork.framework", false);
  35. project.AddFrameworkToProject(targetGuid, "CoreMotion.framework", false);
  36. project.AddFrameworkToProject(targetGuid, "AlipaySDK.framework", false);
  37. project.AddFrameworkToProject(targetGuid, "Security.framework", false);
  38. //
  39. //App名称本地化
  40. string plistPath = Path.Combine(buildPath, "Info.plist");
  41. PlistDocument plist = new PlistDocument();
  42. plist.ReadFromFile(plistPath);
  43. plist.root.SetString("Bundle display name", "${CFBundleDisplayName }");
  44. var infoDirs = Directory.GetDirectories(Application.dataPath + "/AppleDependency/");
  45. for (var i = 0; i < infoDirs.Length; ++i)
  46. {
  47. var files = Directory.GetFiles(infoDirs[i], "*.strings");
  48. project.AddLocalization(Path.GetFileNameWithoutExtension(files[i]), "InfoPlist.strings", "InfoPlist.strings");
  49. }
  50. //修改UnityAppController.mm
  51. string scriptPath = buildPath + "/Classes/UnityAppController.mm";
  52. List<string> lines = new List<string>();
  53. StreamReader streamReader = new StreamReader(scriptPath);
  54. while (!streamReader.EndOfStream)
  55. {
  56. lines.Add(streamReader.ReadLine());
  57. }
  58. streamReader.Close();
  59. lines.Insert
  60. (
  61. 0,
  62. "#import <AlipaySDK/AlipaySDK.h>"
  63. );
  64. lines.Insert
  65. (
  66. 231,
  67. "if ([url.host isEqualToString:@\"safepay\"]) {\n" +
  68. "//跳转支付宝钱包进行支付,处理支付结果\n" +
  69. "[[AlipaySDK defaultService] processOrderWithPaymentResult: url standbyCallback:^ (NSDictionary * resultDic) {\n" +
  70. "NSLog(@\"result 1 = % @\", resultDic);\n" +
  71. " }];\n" +
  72. "}\n"
  73. );
  74. lines.Insert
  75. (
  76. 247,
  77. "// NOTE: 9.0以后使用新API接口\n" +
  78. "-(BOOL)application:(UIApplication*)app openURL: (NSURL*)url options: (NSDictionary<NSString*, id>*)options\n" +
  79. "{\n" +
  80. "if ([url.host isEqualToString: @\"safepay\"])\n" +
  81. "{\n" +
  82. "//跳转支付宝钱包进行支付,处理支付结果\n" +
  83. "[[AlipaySDK defaultService]\n" +
  84. "processOrderWithPaymentResult:url standbyCallback:^(NSDictionary* resultDic) {\n" +
  85. "NSLog(@\"result 2 = % @\", resultDic);\n" +
  86. "}];\n" +
  87. "}\n" +
  88. "return YES;\n" +
  89. "}\n"
  90. );
  91. StreamWriter streamWriter = new StreamWriter(scriptPath);
  92. for (int i = 0; i < lines.Count; i++)
  93. {
  94. streamWriter.WriteLine(lines[i]);
  95. }
  96. streamWriter.Close();
  97. //添加URLScheme
  98. PlistElementArray urlArray = plist.root.CreateArray("CFBundleURLTypes");
  99. PlistElementDict urlDict = urlArray.AddDict();
  100. urlDict.SetString("CFBundleURLName", "TestName");
  101. PlistElementArray urlInnerArray = urlDict.CreateArray("TestScheme");
  102. urlInnerArray.AddString("TestValue");
  103. //保存Plist
  104. plist.WriteToFile(plistPath);
  105. //保存PBXProject
  106. File.WriteAllText(projectPath, project.WriteToString());
  107. }
  108. }