PostProcessor.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. //App名称本地化
  39. string plistPath = Path.Combine(buildPath, "Info.plist");
  40. PlistDocument plist = new PlistDocument();
  41. plist.ReadFromFile(plistPath);
  42. plist.root.SetString("Bundle display name", "${CFBundleDisplayName }");
  43. //var infoDirs = Directory.GetDirectories(Application.dataPath + "/AppleDependency/");
  44. //for (var i = 0; i < infoDirs.Length; ++i)
  45. //{
  46. // var files = Directory.GetFiles(infoDirs[i], "*.strings");
  47. // project.AddLocalization(Path.GetFileNameWithoutExtension(files[i]), "InfoPlist.strings", "InfoPlist.strings");
  48. //}
  49. //修改UnityAppController.mm
  50. string scriptPath = buildPath + "/Classes/UnityAppController.mm";
  51. List<string> lines = new List<string>();
  52. StreamReader streamReader = new StreamReader(scriptPath);
  53. while (!streamReader.EndOfStream)
  54. {
  55. lines.Add(streamReader.ReadLine());
  56. }
  57. streamReader.Close();
  58. lines.Insert
  59. (
  60. 0,
  61. "#import <AlipaySDK/AlipaySDK.h>"
  62. );
  63. lines.Insert
  64. (
  65. 231,
  66. "if ([url.host isEqualToString:@\"safepay\"]) {\n" +
  67. "//跳转支付宝钱包进行支付,处理支付结果\n" +
  68. "[[AlipaySDK defaultService] processOrderWithPaymentResult: url standbyCallback:^ (NSDictionary * resultDic) {\n" +
  69. "NSLog(@\"result 1 = % @\", resultDic);\n" +
  70. " }];\n" +
  71. "}\n"
  72. );
  73. lines.Insert
  74. (
  75. 247,
  76. "// NOTE: 9.0以后使用新API接口\n" +
  77. "-(BOOL)application:(UIApplication*)app openURL: (NSURL*)url options: (NSDictionary<NSString*, id>*)options\n" +
  78. "{\n" +
  79. "if ([url.host isEqualToString: @\"safepay\"])\n" +
  80. "{\n" +
  81. "//跳转支付宝钱包进行支付,处理支付结果\n" +
  82. "[[AlipaySDK defaultService]\n" +
  83. "processOrderWithPaymentResult:url standbyCallback:^(NSDictionary* resultDic) {\n" +
  84. "NSLog(@\"result 2 = % @\", resultDic);\n" +
  85. "}];\n" +
  86. "}\n" +
  87. "return YES;\n" +
  88. "}\n"
  89. );
  90. StreamWriter streamWriter = new StreamWriter(scriptPath);
  91. streamWriter.Close();
  92. //添加URLScheme
  93. PlistElementArray urlArray = plist.root.CreateArray("CFBundleURLTypes");
  94. PlistElementDict urlDict = urlArray.AddDict();
  95. urlDict.SetString("CFBundleURLName", "TestName");
  96. PlistElementArray urlInnerArray = urlDict.CreateArray("TestScheme");
  97. urlInnerArray.AddString("TestValue");
  98. //保存Plist
  99. plist.WriteToFile(plistPath);
  100. //保存PBXProject
  101. File.WriteAllText(projectPath, project.WriteToString());
  102. }
  103. }