XCodePostProcessBuild.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #if UNITY_IOS
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using UnityEditor;
  7. using UnityEditor.Callbacks;
  8. using UnityEditor.iOS.Xcode;
  9. using UnityEngine;
  10. public static class XCodePostProcessBuild
  11. {
  12. [PostProcessBuild(int.MaxValue)]
  13. public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
  14. {
  15. if (target != BuildTarget.iOS)
  16. {
  17. Debug.LogWarning("Target is not iOS. XCodePostProcessBuild will not run");
  18. return;
  19. }
  20. string projectPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
  21. var pbxProject = new PBXProject();
  22. pbxProject.ReadFromString(File.ReadAllText(projectPath));
  23. #if UNITY_2019_3_OR_NEWER
  24. string mainTarget = pbxProject.GetUnityMainTargetGuid();
  25. string frameworkTarget = pbxProject.GetUnityFrameworkTargetGuid();
  26. #else
  27. string mainTarget = pbxProject.TargetGuidByName(PBXProject.GetUnityTargetName());
  28. string frameworkTarget = mainTarget;
  29. #endif
  30. DisableBitcode(pbxProject,mainTarget,frameworkTarget);
  31. pbxProject.SetBuildProperty(frameworkTarget, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");
  32. pbxProject.AddBuildProperty(frameworkTarget, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/Frameworks");
  33. SetBugly(frameworkTarget,pbxProject,pathToBuiltProject);
  34. pbxProject.WriteToFile (projectPath);
  35. }
  36. static void DisableBitcode(PBXProject pbxProject,string mainTarget,string frameworkTarget)
  37. {
  38. pbxProject.SetBuildProperty(mainTarget, "ENABLE_BITCODE", "NO");
  39. pbxProject.SetBuildProperty(frameworkTarget, "ENABLE_BITCODE", "NO");
  40. }
  41. static void SetBugly(string frameworkTarget,PBXProject pbxProject, string pathToBuiltProject)
  42. {
  43. var buglyPath = "Bugly";
  44. AddDirectory(pbxProject, pathToBuiltProject, $"{buglyPath}/Plugins/BuglyPlugins/iOS", "Bugly", null);
  45. pbxProject.AddFileToBuild(frameworkTarget, pbxProject.AddFile("Bugly/Bugly.framework", "Bugly/Bugly.framework", PBXSourceTree.Source));
  46. pbxProject.AddFileToBuild(frameworkTarget, pbxProject.AddFile("Bugly/BuglyBridge/libBuglyBridge.a", "Bugly/libBuglyBridge.a", PBXSourceTree.Source));
  47. pbxProject.AddFileToBuild(frameworkTarget, pbxProject.AddFile("Bugly/BuglyBridge/BuglyBridge.h", "Bugly/BuglyBridge.h", PBXSourceTree.Source));
  48. pbxProject.AddFileToBuild(frameworkTarget,pbxProject.AddFile("usr/lib/libz.dylib", "Bugly/libz.dylib", PBXSourceTree.Sdk));
  49. pbxProject.AddFileToBuild(frameworkTarget,pbxProject.AddFile("usr/lib/libc++.dylib", "Bugly/libc++.dylib", PBXSourceTree.Sdk));
  50. pbxProject.AddFrameworkToProject(frameworkTarget, "SystemConfiguration.framework", false);
  51. pbxProject.AddFrameworkToProject(frameworkTarget, "Security.framework", false);
  52. pbxProject.AddFrameworkToProject(frameworkTarget, "JavaScriptCore.framework", true);
  53. pbxProject.AddBuildProperty(frameworkTarget, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/Bugly");
  54. pbxProject.AddBuildProperty(frameworkTarget, "LIBRARY_SEARCH_PATHS", "$(SRCROOT)/Bugly/BuglyBridge");
  55. }
  56. public static void AddDirectory(PBXProject project, string pathToBuiltProject, string assetPath,
  57. string xcodePath, Action<string> callback,bool recursiveDir = false,bool curDirFiles = false)
  58. {
  59. var path = Path.Combine(Application.dataPath, assetPath);
  60. var targetPath = Path.Combine(pathToBuiltProject, xcodePath);
  61. CopyDirectory(path, targetPath);
  62. var info = new DirectoryInfo(targetPath);
  63. if(recursiveDir)
  64. {
  65. var directories = info.GetDirectories();
  66. foreach (var dirInfo in directories)
  67. {
  68. string fileGuid = project.AddFile(xcodePath + "/" + dirInfo.Name, xcodePath + "/" + dirInfo.Name, PBXSourceTree.Source);
  69. if (callback != null)
  70. {
  71. callback(fileGuid);
  72. }
  73. }
  74. }
  75. if (curDirFiles)
  76. {
  77. var filesList = info.GetFiles();
  78. foreach (var fileInfo in filesList)
  79. {
  80. string fileGuid = project.AddFile(xcodePath + "/" + fileInfo.Name, xcodePath + "/" + fileInfo.Name, PBXSourceTree.Source);
  81. if (callback != null)
  82. {
  83. callback(fileGuid);
  84. }
  85. }
  86. }
  87. }
  88. public static void CopyDirectory(string sourcePath, string destinationPath)
  89. {
  90. if (destinationPath.EndsWith(".meta") || destinationPath.EndsWith(".DS_Store"))
  91. return;
  92. DirectoryInfo info = new DirectoryInfo(sourcePath);
  93. Directory.CreateDirectory(destinationPath);
  94. foreach (FileSystemInfo fsi in info.GetFileSystemInfos())
  95. {
  96. string destName = Path.Combine(destinationPath, fsi.Name);
  97. if (destName.EndsWith(".meta") || destName.EndsWith(".DS_Store"))
  98. continue;
  99. if (fsi is System.IO.FileInfo) //如果是文件,复制文件
  100. File.Copy(fsi.FullName, destName);
  101. else //如果是文件夹,新建文件夹,递归
  102. {
  103. Directory.CreateDirectory(destName);
  104. CopyDirectory(fsi.FullName, destName);
  105. }
  106. }
  107. }
  108. }
  109. #endif