BL_BuildPostProcess.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #if UNITY_IPHONE || UNITY_STANDALONE_OSX
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEditor.Callbacks;
  5. #if UNITY_IPHONE
  6. using UnityEditor.iOS.Xcode;
  7. using UnityEditor.iOS.Xcode.Extensions;
  8. #endif
  9. public class BL_BuildPostProcess
  10. {
  11. [PostProcessBuild]
  12. public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
  13. {
  14. if (buildTarget == BuildTarget.iOS)
  15. {
  16. #if UNITY_IPHONE
  17. LinkLibraries(path);
  18. UpdatePermission(path + "/Info.plist");
  19. #endif
  20. }
  21. }
  22. #if UNITY_IPHONE
  23. static string GetTargetGuid(PBXProject proj)
  24. {
  25. #if UNITY_2019_3_OR_NEWER
  26. return proj.GetUnityFrameworkTargetGuid();
  27. #else
  28. return proj.TargetGuidByName("Unity-iPhone");
  29. #endif
  30. }
  31. // The followings are the addtional frameworks to add to the project
  32. static string[] ProjectFrameworks = new string[] {
  33. "Accelerate.framework",
  34. "CoreTelephony.framework",
  35. "CoreText.framework",
  36. "CoreML.framework",
  37. "Metal.framework",
  38. "VideoToolbox.framework",
  39. "libiPhone-lib.a",
  40. "libresolv.tbd",
  41. };
  42. public static void LinkLibraries(string path)
  43. {
  44. string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
  45. PBXProject proj = new PBXProject();
  46. proj.ReadFromFile(projPath);
  47. string target = GetTargetGuid(proj);
  48. // embedded frameworks
  49. #if UNITY_2019_3_OR_NEWER
  50. target = proj.GetUnityMainTargetGuid();
  51. #endif
  52. const string defaultLocationInProj = "AgoraEngine/Plugins/iOS";
  53. const string AgoraRtcKitFrameworkName = "AgoraRtcKit.framework";
  54. const string AgorafdkaacFrameworkName = "Agorafdkaac.framework";
  55. const string AgoraSoundTouchFrameworkName = "AgoraSoundTouch.framework";
  56. const string AgoraCoreFrameworkName = "AgoraCore.framework";
  57. const string AgoraAIDenoiseExtensionFrameworkName = "AgoraAIDenoiseExtension.framework";
  58. string AgoraRtcKitFrameworkPath = Path.Combine(defaultLocationInProj, AgoraRtcKitFrameworkName);
  59. string AgorafdkaacFrameworkPath = Path.Combine(defaultLocationInProj, AgorafdkaacFrameworkName);
  60. string AgoraSoundTouchFrameworkPath = Path.Combine(defaultLocationInProj, AgoraSoundTouchFrameworkName);
  61. string AgoraCoreFrameworkPath = Path.Combine(defaultLocationInProj, AgoraCoreFrameworkName);
  62. string AgoraAIDenoiseExtensionFrameworkPath = Path.Combine(defaultLocationInProj, AgoraAIDenoiseExtensionFrameworkName);
  63. string fileGuid = proj.AddFile(AgoraRtcKitFrameworkPath, "Frameworks/" + AgoraRtcKitFrameworkPath, PBXSourceTree.Sdk);
  64. PBXProjectExtensions.AddFileToEmbedFrameworks(proj, target, fileGuid);
  65. fileGuid = proj.AddFile(AgorafdkaacFrameworkPath, "Frameworks/" + AgorafdkaacFrameworkPath, PBXSourceTree.Sdk);
  66. PBXProjectExtensions.AddFileToEmbedFrameworks(proj, target, fileGuid);
  67. fileGuid = proj.AddFile(AgoraSoundTouchFrameworkPath, "Frameworks/" + AgoraSoundTouchFrameworkPath, PBXSourceTree.Sdk);
  68. PBXProjectExtensions.AddFileToEmbedFrameworks(proj, target, fileGuid);
  69. fileGuid = proj.AddFile(AgoraCoreFrameworkPath, "Frameworks/" + AgoraCoreFrameworkPath, PBXSourceTree.Sdk);
  70. PBXProjectExtensions.AddFileToEmbedFrameworks(proj, target, fileGuid);
  71. fileGuid = proj.AddFile(AgoraAIDenoiseExtensionFrameworkPath, "Frameworks/" + AgoraAIDenoiseExtensionFrameworkPath, PBXSourceTree.Sdk);
  72. PBXProjectExtensions.AddFileToEmbedFrameworks(proj, target, fileGuid);
  73. proj.SetBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");
  74. // done, write to the project file
  75. File.WriteAllText(projPath, proj.WriteToString());
  76. }
  77. #endif
  78. /// <summary>
  79. /// Update the permission
  80. /// </summary>
  81. /// <param name="pListPath">path to the Info.plist file</param>
  82. static void UpdatePermission(string pListPath)
  83. {
  84. #if UNITY_IPHONE
  85. PlistDocument plist = new PlistDocument();
  86. plist.ReadFromString(File.ReadAllText(pListPath));
  87. PlistElementDict rootDic = plist.root;
  88. var micPermission = "NSMicrophoneUsageDescription";
  89. rootDic.SetString(micPermission, "Voice call need to user mic");
  90. File.WriteAllText(pListPath, plist.WriteToString());
  91. #endif
  92. }
  93. }
  94. #endif