XCodePostProcessBuild.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #if UNITY_IOS || UNITY_TVOS
  2. #define UNITY_XCODE_EXTENSIONS_AVAILABLE
  3. #endif
  4. using UnityEditor;
  5. using System.IO;
  6. using UnityEngine;
  7. #if UNITY_IOS && UNITY_EDITOR
  8. using UnityEditor.Callbacks;
  9. using UnityEditor.iOS.Xcode;
  10. #endif
  11. using AppleAuth.Editor;
  12. #if UNITY_XCODE_EXTENSIONS_AVAILABLE
  13. //using UnityEditor.iOS.Xcode;
  14. #endif
  15. public static class XCodePostProcessBuild
  16. {
  17. #if UNITY_IOS && UNITY_EDITOR
  18. private static readonly string[] csAddFrameworks = new string[]{
  19. "Security.framework","WebKit.framework", "CoreGraphics.framework"
  20. };
  21. [PostProcessBuild(1)]
  22. public static void OnPostprocessBuild(BuildTarget target, string path)
  23. {
  24. if (BuildTarget.iOS != target)
  25. {
  26. return;
  27. }
  28. string projectPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
  29. SetFrameworksAndBuildSettings(projectPath);
  30. SetInfoList(path, "com.xmjssvr.BowArrow", "wxfe29f3f64e3c5d16");
  31. SetAssociatedDomains(projectPath, "xmjssvr.cn");
  32. setSignInWithApple(projectPath);
  33. }
  34. private static void SetFrameworksAndBuildSettings(string path)
  35. {
  36. PBXProject proj = new PBXProject();
  37. proj.ReadFromString(File.ReadAllText(path));
  38. string target = proj.GetUnityMainTargetGuid();
  39. string frameworkGuid = proj.GetUnityFrameworkTargetGuid();
  40. Debug.Log("Target Name is " + target);
  41. // 设置 BuildSettings
  42. proj.AddBuildProperty(target, "Other Linker Flags", "-Objc -all_load");
  43. proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
  44. proj.SetBuildProperty(frameworkGuid, "ENABLE_BITCODE", "NO");
  45. //根据微信SDK文档的要求,加入相关的Frameworks
  46. for (int i = 0; i < csAddFrameworks.Length; ++i)
  47. {
  48. if (!proj.ContainsFramework(target, csAddFrameworks[i]))
  49. proj.AddFrameworkToProject(target, csAddFrameworks[i], false);
  50. }
  51. File.WriteAllText(path, proj.WriteToString());
  52. }
  53. public static void SetInfoList(string buildPath, string wxUrlName, string wxScheme)
  54. {
  55. string listPath = buildPath + "/Info.plist";
  56. PlistDocument plist = new PlistDocument();
  57. plist.ReadFromString(File.ReadAllText(listPath));
  58. // 在“info”标签栏的“URL type“添加“URL scheme”,值为你在微信后台注册的应用程序的 AppID
  59. PlistElementArray urlArray = plist.root.CreateArray("CFBundleURLTypes");
  60. PlistElementDict dict = urlArray.AddDict();
  61. dict.SetString("CFBundleTypeRole", "Editor");
  62. dict.SetString("CFBundleURLName", wxUrlName);
  63. PlistElementArray urlSchemes = dict.CreateArray("CFBundleURLSchemes");
  64. urlSchemes.AddString(wxScheme);
  65. // 在 “info”标签栏的“LSApplicationQueriesSchemes“添加weixin wechat和weixinULAPI
  66. PlistElementArray wxArray = plist.root.CreateArray("LSApplicationQueriesSchemes");
  67. wxArray.AddString("weixin");
  68. wxArray.AddString("wechat");
  69. wxArray.AddString("weixinULAPI");
  70. PlistElementDict ed = plist.root;
  71. ed.SetString("NSBluetoothAlwaysUsageDescription", "您是否允此App使用蓝牙,来搜索和连接设备?");
  72. ed.SetString("NSBluetoothPeripheralUsageDescription", "您是否允此App使用蓝牙,来搜索和连接设备?");
  73. ed.SetString("NSCameraUsageDescription", "App需要您的同意,才能访问照相机");
  74. ed.SetString("NSPhotoLibraryUsageDescription", "App需要您的同意,才能访问相册");
  75. ed.SetString("NSLocationWhenInUseUsageDescription", "我们需要通过您的地理位置信息获取您周边的相关数据");
  76. ed.SetString("NSLocationAlwaysUsageDescription", "我们需要通过您的地理位置信息获取您周边的相关数据");
  77. File.WriteAllText(listPath, plist.WriteToString());
  78. }
  79. // 设置Associated Domains
  80. public static void SetAssociatedDomains(string pbxProjectPath, string domainUrl)
  81. {
  82. //默认 Target Name, 你自己的可能不一样
  83. string targetName = "Unity-iPhone";
  84. //Set the entitlements file name to what you want but make sure it has this extension
  85. string entitlementsFileName = "my_app.entitlements";
  86. var entitlements = new ProjectCapabilityManager(pbxProjectPath, entitlementsFileName, targetName);
  87. entitlements.AddAssociatedDomains(new string[] { "applinks:" + domainUrl });
  88. entitlements.WriteToFile();
  89. }
  90. // 设置Associated Domains
  91. public static void setSignInWithApple(string pbxProjectPath)
  92. {
  93. //默认 Target Name, 你自己的可能不一样
  94. string targetName = "Unity-iPhone";
  95. //Set the entitlements file name to what you want but make sure it has this extension
  96. string entitlementsFileName = "my_app.entitlements";
  97. var project = new PBXProject();
  98. project.ReadFromString(System.IO.File.ReadAllText(pbxProjectPath));
  99. var entitlements = new ProjectCapabilityManager(pbxProjectPath, entitlementsFileName, targetName, project.GetUnityMainTargetGuid());
  100. entitlements.AddSignInWithAppleWithCompatibility(project.GetUnityFrameworkTargetGuid());
  101. entitlements.WriteToFile();
  102. }
  103. #endif
  104. }