Tanoda
NGPostProcessBuild.cs
Go to the documentation of this file.
1#if UNITY_IOS
2using UnityEditor;
3using UnityEditor.Callbacks;
4using System.IO;
5using UnityEditor.iOS.Xcode;
6#endif
7
9{
10 private const bool ENABLED = true;
11
12 private const string PHOTO_LIBRARY_USAGE_DESCRIPTION = "Save media to Photos";
13 private const bool MINIMUM_TARGET_8_OR_ABOVE = false;
14
15#if UNITY_IOS
16#pragma warning disable 0162
17 [PostProcessBuild]
18 public static void OnPostprocessBuild( BuildTarget target, string buildPath )
19 {
20 if( !ENABLED )
21 return;
22
23 if( target == BuildTarget.iOS )
24 {
25 string pbxProjectPath = PBXProject.GetPBXProjectPath( buildPath );
26 string plistPath = Path.Combine( buildPath, "Info.plist" );
27
28 PBXProject pbxProject = new PBXProject();
29 pbxProject.ReadFromFile( pbxProjectPath );
30
31#if UNITY_2019_3_OR_NEWER
32 string targetGUID = pbxProject.GetUnityFrameworkTargetGuid();
33#else
34 string targetGUID = pbxProject.TargetGuidByName( PBXProject.GetUnityTargetName() );
35#endif
36
37 // Minimum supported iOS version on Unity 2018.1 and later is 8.0
38#if !UNITY_2018_1_OR_NEWER
39 if( MINIMUM_TARGET_8_OR_ABOVE )
40 {
41#endif
42 pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework Photos" );
43 pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework MobileCoreServices" );
44 pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework ImageIO" );
45#if !UNITY_2018_1_OR_NEWER
46 }
47 else
48 {
49 pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-weak_framework Photos" );
50 pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework AssetsLibrary" );
51 pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework MobileCoreServices" );
52 pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework ImageIO" );
53 }
54#endif
55
56 pbxProject.RemoveFrameworkFromProject( targetGUID, "Photos.framework" );
57
58 File.WriteAllText( pbxProjectPath, pbxProject.WriteToString() );
59
60 PlistDocument plist = new PlistDocument();
61 plist.ReadFromString( File.ReadAllText( plistPath ) );
62
63 PlistElementDict rootDict = plist.root;
64 rootDict.SetString( "NSPhotoLibraryUsageDescription", PHOTO_LIBRARY_USAGE_DESCRIPTION );
65 rootDict.SetString( "NSPhotoLibraryAddUsageDescription", PHOTO_LIBRARY_USAGE_DESCRIPTION );
66
67 File.WriteAllText( plistPath, plist.WriteToString() );
68 }
69 }
70#pragma warning restore 0162
71#endif
72}