NGPostProcessBuild.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using System.IO;
  2. using UnityEditor;
  3. using UnityEngine;
  4. #if UNITY_IOS
  5. using UnityEditor.Callbacks;
  6. using UnityEditor.iOS.Xcode;
  7. #endif
  8. namespace NativeGalleryNamespace
  9. {
  10. [System.Serializable]
  11. public class Settings
  12. {
  13. private const string SAVE_PATH = "ProjectSettings/NativeGallery.json";
  14. public bool AutomatedSetup = true;
  15. #if !UNITY_2018_1_OR_NEWER
  16. public bool MinimumiOSTarget8OrAbove = false;
  17. #endif
  18. public string PhotoLibraryUsageDescription = "The app requires access to Photos to interact with it.";
  19. public string PhotoLibraryAdditionsUsageDescription = "The app requires access to Photos to save media to it.";
  20. public bool DontAskLimitedPhotosPermissionAutomaticallyOnIos14 = true; // See: https://mackuba.eu/2020/07/07/photo-library-changes-ios-14/
  21. private static Settings m_instance = null;
  22. public static Settings Instance
  23. {
  24. get
  25. {
  26. if( m_instance == null )
  27. {
  28. try
  29. {
  30. if( File.Exists( SAVE_PATH ) )
  31. m_instance = JsonUtility.FromJson<Settings>( File.ReadAllText( SAVE_PATH ) );
  32. else
  33. m_instance = new Settings();
  34. }
  35. catch( System.Exception e )
  36. {
  37. Debug.LogException( e );
  38. m_instance = new Settings();
  39. }
  40. }
  41. return m_instance;
  42. }
  43. }
  44. public void Save()
  45. {
  46. File.WriteAllText( SAVE_PATH, JsonUtility.ToJson( this, true ) );
  47. }
  48. #if UNITY_2018_3_OR_NEWER
  49. [SettingsProvider]
  50. public static SettingsProvider CreatePreferencesGUI()
  51. {
  52. return new SettingsProvider( "Project/yasirkula/Native Gallery", SettingsScope.Project )
  53. {
  54. guiHandler = ( searchContext ) => PreferencesGUI(),
  55. keywords = new System.Collections.Generic.HashSet<string>() { "Native", "Gallery", "Android", "iOS" }
  56. };
  57. }
  58. #endif
  59. #if !UNITY_2018_3_OR_NEWER
  60. [PreferenceItem( "Native Gallery" )]
  61. #endif
  62. public static void PreferencesGUI()
  63. {
  64. EditorGUI.BeginChangeCheck();
  65. Instance.AutomatedSetup = EditorGUILayout.Toggle( "Automated Setup", Instance.AutomatedSetup );
  66. EditorGUI.BeginDisabledGroup( !Instance.AutomatedSetup );
  67. #if !UNITY_2018_1_OR_NEWER
  68. Instance.MinimumiOSTarget8OrAbove = EditorGUILayout.Toggle( "Deployment Target Is 8.0 Or Above", Instance.MinimumiOSTarget8OrAbove );
  69. #endif
  70. Instance.PhotoLibraryUsageDescription = EditorGUILayout.DelayedTextField( "Photo Library Usage Description", Instance.PhotoLibraryUsageDescription );
  71. Instance.PhotoLibraryAdditionsUsageDescription = EditorGUILayout.DelayedTextField( "Photo Library Additions Usage Description", Instance.PhotoLibraryAdditionsUsageDescription );
  72. Instance.DontAskLimitedPhotosPermissionAutomaticallyOnIos14 = EditorGUILayout.Toggle( new GUIContent( "Don't Ask Limited Photos Permission Automatically", "See: https://mackuba.eu/2020/07/07/photo-library-changes-ios-14/. It's recommended to keep this setting enabled" ), Instance.DontAskLimitedPhotosPermissionAutomaticallyOnIos14 );
  73. EditorGUI.EndDisabledGroup();
  74. if( EditorGUI.EndChangeCheck() )
  75. Instance.Save();
  76. }
  77. }
  78. public class NGPostProcessBuild
  79. {
  80. #if UNITY_IOS
  81. [PostProcessBuild( 1 )]
  82. public static void OnPostprocessBuild( BuildTarget target, string buildPath )
  83. {
  84. if( !Settings.Instance.AutomatedSetup )
  85. return;
  86. if( target == BuildTarget.iOS )
  87. {
  88. string pbxProjectPath = PBXProject.GetPBXProjectPath( buildPath );
  89. string plistPath = Path.Combine( buildPath, "Info.plist" );
  90. PBXProject pbxProject = new PBXProject();
  91. pbxProject.ReadFromFile( pbxProjectPath );
  92. #if UNITY_2019_3_OR_NEWER
  93. string targetGUID = pbxProject.GetUnityFrameworkTargetGuid();
  94. #else
  95. string targetGUID = pbxProject.TargetGuidByName( PBXProject.GetUnityTargetName() );
  96. #endif
  97. // Minimum supported iOS version on Unity 2018.1 and later is 8.0
  98. #if !UNITY_2018_1_OR_NEWER
  99. if( !Settings.Instance.MinimumiOSTarget8OrAbove )
  100. {
  101. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-weak_framework Photos" );
  102. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-weak_framework PhotosUI" );
  103. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework AssetsLibrary" );
  104. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework MobileCoreServices" );
  105. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework ImageIO" );
  106. }
  107. else
  108. #endif
  109. {
  110. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-weak_framework PhotosUI" );
  111. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework Photos" );
  112. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework MobileCoreServices" );
  113. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework ImageIO" );
  114. }
  115. pbxProject.RemoveFrameworkFromProject( targetGUID, "Photos.framework" );
  116. File.WriteAllText( pbxProjectPath, pbxProject.WriteToString() );
  117. PlistDocument plist = new PlistDocument();
  118. plist.ReadFromString( File.ReadAllText( plistPath ) );
  119. PlistElementDict rootDict = plist.root;
  120. rootDict.SetString( "NSPhotoLibraryUsageDescription", Settings.Instance.PhotoLibraryUsageDescription );
  121. rootDict.SetString( "NSPhotoLibraryAddUsageDescription", Settings.Instance.PhotoLibraryAdditionsUsageDescription );
  122. if( Settings.Instance.DontAskLimitedPhotosPermissionAutomaticallyOnIos14 )
  123. rootDict.SetBoolean( "PHPhotoLibraryPreventAutomaticLimitedAccessAlert", true );
  124. File.WriteAllText( plistPath, plist.WriteToString() );
  125. }
  126. }
  127. #endif
  128. }
  129. }