PostProcessBuild_iOS.cs 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #if UNITY_IOS && UNITY_2017_1_OR_NEWER
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using UnityEditor.Callbacks;
  8. using UnityEditor.iOS.Xcode;
  9. using UnityEditor.iOS.Xcode.Extensions;
  10. //-----------------------------------------------------------------------------
  11. // Copyright 2012-2022 RenderHeads Ltd. All rights reserved.
  12. //-----------------------------------------------------------------------------
  13. namespace RenderHeads.Media.AVProMovieCapture.Editor
  14. {
  15. public class PostProcessBuild_iOS
  16. {
  17. [PostProcessBuild]
  18. public static void ModifyProject(BuildTarget buildTarget, string path)
  19. {
  20. if (buildTarget != BuildTarget.iOS)
  21. return;
  22. string projectPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
  23. PBXProject project = new PBXProject();
  24. project.ReadFromFile(projectPath);
  25. #if UNITY_2019_3_OR_NEWER
  26. string targetGuid = project.GetUnityMainTargetGuid();
  27. #else
  28. string targetGuid = project.TargetGuidByName(PBXProject.GetUnityTargetName());
  29. #endif
  30. string fileGuid = project.FindFileGuidByProjectPath("Frameworks/Plugins/RenderHeads/AVProMovieCapture/Runtime/Plugins/iOS/AVProMovieCapture.framework");
  31. if (fileGuid != null)
  32. {
  33. PBXProjectExtensions.AddFileToEmbedFrameworks(project, targetGuid, fileGuid);
  34. }
  35. else
  36. {
  37. Debug.LogWarning("Failed to find AVProMovieCapture.framework in the generated project. You will need to manually set AVProMovieCapture.framework to 'Embed & Sign' in the Xcode project's framework list.");
  38. }
  39. project.SetBuildProperty(targetGuid, "LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");
  40. project.WriteToFile(projectPath);
  41. }
  42. [PostProcessBuild]
  43. public static void ModfifyPlist(BuildTarget buildTarget, string path)
  44. {
  45. if (buildTarget != BuildTarget.iOS)
  46. return;
  47. string plistPath = Path.Combine(path, "Info.plist");
  48. if (!File.Exists(plistPath))
  49. {
  50. Debug.LogWarning(@"Unable to locate Info.plist, you may need to add the following keys yourself:
  51. NSPhotoLibraryUsageDescription,
  52. NSPhotoLibraryAddUsageDescription");
  53. return;
  54. }
  55. Debug.Log("Modifying the Info.plist file at: " + plistPath);
  56. PlistDocument plist = new PlistDocument();
  57. plist.ReadFromFile(plistPath);
  58. PlistElementDict rootDict = plist.root;
  59. // Enable file sharing so that files can be pulled off of the device with iTunes
  60. rootDict.SetBoolean("UIFileSharingEnabled", true);
  61. // Enable this so that the files app can access the captured movies
  62. rootDict.SetBoolean("LSSupportsOpeningDocumentsInPlace", true);
  63. SerializedObject settings = Settings.GetSerializedSettings();
  64. SerializedProperty propPhotoLibraryUsageDescription = settings.FindProperty("_photoLibraryUsageDescription");
  65. string photoLibraryUsageDescription = propPhotoLibraryUsageDescription.stringValue;
  66. if (photoLibraryUsageDescription != null && photoLibraryUsageDescription.Length > 0)
  67. {
  68. Debug.Log("Adding 'NSPhotoLibraryUsageDescription' to Info.plist");
  69. rootDict.SetString("NSPhotoLibraryUsageDescription", photoLibraryUsageDescription);
  70. }
  71. SerializedProperty propPhotoLibraryAddUsageDescription = settings.FindProperty("_photoLibraryAddUsageDescription");
  72. string photoLibraryAddUsageDescription = propPhotoLibraryAddUsageDescription.stringValue;
  73. if (photoLibraryAddUsageDescription != null && photoLibraryAddUsageDescription.Length > 0)
  74. {
  75. Debug.Log("Adding 'NSPhotoLibraryAddUsageDescription' to Info.plist");
  76. rootDict.SetString("NSPhotoLibraryAddUsageDescription", photoLibraryAddUsageDescription);
  77. }
  78. File.WriteAllText(plistPath, plist.WriteToString());
  79. Debug.Log("Finished modifying the Info.plist");
  80. }
  81. }
  82. }
  83. #endif // UNITY_IOS && UNITY_2017_1_OR_NEWER