From 9b1575d6e126b98b79b58c487e8cfd09125d1e3b Mon Sep 17 00:00:00 2001 From: yasirkula Date: Mon, 5 Jul 2021 21:25:41 +0300 Subject: [PATCH] Moved iOS build post-processor settings to 'Project Settings/yasirkula/Native Share' --- .github/README.md | 2 +- .../NativeShare/Editor/NSPostProcessBuild.cs | 83 ++++++++++++++++--- Plugins/NativeShare/README.txt | 5 +- package.json | 2 +- 4 files changed, 78 insertions(+), 14 deletions(-) diff --git a/.github/README.md b/.github/README.md index 1d4f46e..f1a558c 100644 --- a/.github/README.md +++ b/.github/README.md @@ -30,7 +30,7 @@ For reference, the legacy documentation is available at: https://github.com/yasi There are two ways to set up the plugin on iOS: -- **a. Automated Setup:** *(optional)* change the value of **PHOTO_LIBRARY_USAGE_DESCRIPTION** in *Plugins/NativeShare/Editor/NSPostProcessBuild.cs* +- **a. Automated Setup:** *(optional)* change the value of **Photo Library Usage Description** at *Project Settings/yasirkula/Native Share* - **b. Manual Setup:** see: https://github.com/yasirkula/UnityNativeShare/wiki/Manual-Setup-for-iOS ## HOW TO diff --git a/Plugins/NativeShare/Editor/NSPostProcessBuild.cs b/Plugins/NativeShare/Editor/NSPostProcessBuild.cs index 081f299..bcb7f2b 100644 --- a/Plugins/NativeShare/Editor/NSPostProcessBuild.cs +++ b/Plugins/NativeShare/Editor/NSPostProcessBuild.cs @@ -1,24 +1,88 @@ -#if UNITY_IOS -using System.IO; +using System.IO; using UnityEditor; using UnityEngine; +#if UNITY_IOS using UnityEditor.Callbacks; using UnityEditor.iOS.Xcode; #endif namespace NativeShareNamespace { - public class NSPostProcessBuild + [System.Serializable] + public class Settings { - private const bool ENABLED = true; - private const string PHOTO_LIBRARY_USAGE_DESCRIPTION = "The app requires access to Photos to save media to it."; + private const string SAVE_PATH = "ProjectSettings/NativeShare.json"; + + public bool AutomatedSetup = true; + public string PhotoLibraryUsageDescription = "The app requires access to Photos to save media to it."; + + private static Settings m_instance = null; + public static Settings Instance + { + get + { + if( m_instance == null ) + { + try + { + if( File.Exists( SAVE_PATH ) ) + m_instance = JsonUtility.FromJson( File.ReadAllText( SAVE_PATH ) ); + else + m_instance = new Settings(); + } + catch( System.Exception e ) + { + Debug.LogException( e ); + m_instance = new Settings(); + } + } + + return m_instance; + } + } + public void Save() + { + File.WriteAllText( SAVE_PATH, JsonUtility.ToJson( this, true ) ); + } + +#if UNITY_2018_3_OR_NEWER + [SettingsProvider] + public static SettingsProvider CreatePreferencesGUI() + { + return new SettingsProvider( "Project/yasirkula/Native Share", SettingsScope.Project ) + { + guiHandler = ( searchContext ) => PreferencesGUI(), + keywords = new System.Collections.Generic.HashSet() { "Native", "Share", "Android", "iOS" } + }; + } +#endif + +#if !UNITY_2018_3_OR_NEWER + [PreferenceItem( "Native Share" )] +#endif + public static void PreferencesGUI() + { + EditorGUI.BeginChangeCheck(); + + Instance.AutomatedSetup = EditorGUILayout.Toggle( "Automated Setup", Instance.AutomatedSetup ); + + EditorGUI.BeginDisabledGroup( !Instance.AutomatedSetup ); + Instance.PhotoLibraryUsageDescription = EditorGUILayout.DelayedTextField( new GUIContent( "Photo Library Usage Description", "Shown to user when they select the 'Save to Photos' option in share sheet" ), Instance.PhotoLibraryUsageDescription ); + EditorGUI.EndDisabledGroup(); + + if( EditorGUI.EndChangeCheck() ) + Instance.Save(); + } + } + + public class NSPostProcessBuild + { #if UNITY_IOS -#pragma warning disable 0162 [PostProcessBuild] public static void OnPostprocessBuild( BuildTarget target, string buildPath ) { - if( !ENABLED ) + if( !Settings.Instance.AutomatedSetup ) return; if( target == BuildTarget.iOS ) @@ -29,13 +93,12 @@ public static void OnPostprocessBuild( BuildTarget target, string buildPath ) plist.ReadFromString( File.ReadAllText( plistPath ) ); PlistElementDict rootDict = plist.root; - rootDict.SetString( "NSPhotoLibraryUsageDescription", PHOTO_LIBRARY_USAGE_DESCRIPTION ); - rootDict.SetString( "NSPhotoLibraryAddUsageDescription", PHOTO_LIBRARY_USAGE_DESCRIPTION ); + rootDict.SetString( "NSPhotoLibraryUsageDescription", Settings.Instance.PhotoLibraryUsageDescription ); + rootDict.SetString( "NSPhotoLibraryAddUsageDescription", Settings.Instance.PhotoLibraryUsageDescription ); File.WriteAllText( plistPath, plist.WriteToString() ); } } -#pragma warning restore 0162 #endif } } \ No newline at end of file diff --git a/Plugins/NativeShare/README.txt b/Plugins/NativeShare/README.txt index fe41281..5febdcd 100644 --- a/Plugins/NativeShare/README.txt +++ b/Plugins/NativeShare/README.txt @@ -3,6 +3,7 @@ Online documentation & example code available at: https://github.com/yasirkula/UnityNativeShare E-mail: yasirkula@gmail.com + 1. ABOUT This plugin helps you natively share files (images, videos, documents, etc.) and/or plain text on Android & iOS. A ContentProvider is used to share the media on Android. @@ -17,10 +18,10 @@ For reference, the legacy documentation is available at: https://github.com/yasi There are two ways to set up the plugin on iOS: a. Automated Setup for iOS -- change the value of PHOTO_LIBRARY_USAGE_DESCRIPTION in Plugins/NativeShare/Editor/NSPostProcessBuild.cs (optional) +- (optional) change the value of 'Photo Library Usage Description' at 'Project Settings/yasirkula/Native Share' b. Manual Setup for iOS -- set the value of ENABLED to false in NSPostProcessBuild.cs +- set the value of 'Automated Setup' to false at 'Project Settings/yasirkula/Native Share' - build your project - enter a Photo Library Usage Description to Info.plist in Xcode (in case user decides to save the shared media to Photos) - also enter a Photo Library Additions Usage Description to Info.plist in Xcode, if exists diff --git a/package.json b/package.json index 80aebc3..b811e6b 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "com.yasirkula.nativeshare", "displayName": "Native Share", - "version": "1.4.1", + "version": "1.4.2", "documentationUrl": "https://github.com/yasirkula/UnityNativeShare", "changelogUrl": "https://github.com/yasirkula/UnityNativeShare/releases", "licensesUrl": "https://github.com/yasirkula/UnityNativeShare/blob/master/LICENSE.txt",