BL_BuildPostProcess.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. using System.Collections.Generic;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEditor.Callbacks;
  5. #if UNITY_IOS
  6. using UnityEditor.iOS.Xcode;
  7. using UnityEditor.iOS.Xcode.Extensions;
  8. #endif
  9. using UnityEngine;
  10. public class BL_BuildPostProcess
  11. {
  12. [PostProcessBuild]
  13. public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
  14. {
  15. if (buildTarget == BuildTarget.iOS)
  16. {
  17. #if UNITY_IOS
  18. LinkLibraries(path);
  19. UpdatePermission(path + "/Info.plist");
  20. #endif
  21. }
  22. }
  23. //public static void DisableBitcode(string projPath)
  24. //{
  25. // PBXProject proj = new PBXProject();
  26. // proj.ReadFromString(File.ReadAllText(projPath));
  27. // string target = GetTargetGuid(proj);
  28. // proj.SetBuildProperty(target, "ENABLE_BITCODE", "false");
  29. // File.WriteAllText(projPath, proj.WriteToString());
  30. //}
  31. #if UNITY_IOS
  32. static string GetTargetGuid(PBXProject proj)
  33. {
  34. #if UNITY_2019_3_OR_NEWER
  35. return proj.GetUnityMainTargetGuid();
  36. #else
  37. return proj.TargetGuidByName("Unity-iPhone");
  38. #endif
  39. }
  40. #endif
  41. #if UNITY_IOS
  42. static void LinkLibraries(string path)
  43. {
  44. // linked library
  45. string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
  46. PBXProject proj = new PBXProject();
  47. proj.ReadFromFile(projPath);
  48. string target = GetTargetGuid(proj);
  49. string defaultLocationInProj = "Agora-RTC-Plugin/Agora-Unity-RTC-SDK/Plugins/iOS";
  50. DirectoryInfo directoryInfo = new DirectoryInfo(Path.Combine(path, "Frameworks/"+ defaultLocationInProj));
  51. FileInfo[] fileInfos = directoryInfo.GetFiles();
  52. DirectoryInfo[] directoryInfos = directoryInfo.GetDirectories();
  53. Debug.Log("fileInfo:"+ fileInfos.Length);
  54. List<string> frameworks = new List<string>();
  55. foreach (var fileInfo in fileInfos)
  56. {
  57. frameworks.Add(fileInfo.Name);
  58. //Debug.Log(fileInfo.Name);
  59. }
  60. foreach (var fileInfo in directoryInfos)
  61. {
  62. frameworks.Add(fileInfo.Name);
  63. //Debug.Log(fileInfo.Name);
  64. }
  65. foreach (var file in frameworks)
  66. {
  67. string fullPath = Path.Combine(defaultLocationInProj, file);
  68. string fileGuid = proj.AddFile(fullPath, "Frameworks/" + fullPath, PBXSourceTree.Sdk);
  69. PBXProjectExtensions.AddFileToEmbedFrameworks(proj, target, fileGuid);
  70. }
  71. proj.SetBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");
  72. // done, write to the project file
  73. File.WriteAllText(projPath, proj.WriteToString());
  74. }
  75. static void UpdatePermission(string pListPath)
  76. {
  77. PlistDocument plist = new PlistDocument();
  78. plist.ReadFromString(File.ReadAllText(pListPath));
  79. PlistElementDict rootDic = plist.root;
  80. var cameraPermission = "NSCameraUsageDescription";
  81. var micPermission = "NSMicrophoneUsageDescription";
  82. rootDic.SetString(cameraPermission, "Video need to use camera");
  83. rootDic.SetString(micPermission, "Voice call need to user mic");
  84. File.WriteAllText(pListPath, plist.WriteToString());
  85. }
  86. #endif
  87. }