Ingen beskrivning
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NCPostProcessBuild.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. using System.IO;
  2. using UnityEngine;
  3. using UnityEditor;
  4. #if UNITY_IOS
  5. using UnityEditor.Callbacks;
  6. using UnityEditor.iOS.Xcode;
  7. #endif
  8. namespace NativeCameraNamespace
  9. {
  10. [System.Serializable]
  11. public class Settings
  12. {
  13. private const string SAVE_PATH = "ProjectSettings/NativeCamera.json";
  14. public bool AutomatedSetup = true;
  15. public string CameraUsageDescription = "The app requires access to the camera to take pictures or record videos with it.";
  16. public string MicrophoneUsageDescription = "The app will capture microphone input in the recorded video.";
  17. private static Settings m_instance = null;
  18. public static Settings Instance
  19. {
  20. get
  21. {
  22. if( m_instance == null )
  23. {
  24. try
  25. {
  26. if( File.Exists( SAVE_PATH ) )
  27. m_instance = JsonUtility.FromJson<Settings>( File.ReadAllText( SAVE_PATH ) );
  28. else
  29. m_instance = new Settings();
  30. }
  31. catch( System.Exception e )
  32. {
  33. Debug.LogException( e );
  34. m_instance = new Settings();
  35. }
  36. }
  37. return m_instance;
  38. }
  39. }
  40. public void Save()
  41. {
  42. File.WriteAllText( SAVE_PATH, JsonUtility.ToJson( this, true ) );
  43. }
  44. #if UNITY_2018_3_OR_NEWER
  45. [SettingsProvider]
  46. public static SettingsProvider CreatePreferencesGUI()
  47. {
  48. return new SettingsProvider( "Project/yasirkula/Native Camera", SettingsScope.Project )
  49. {
  50. guiHandler = ( searchContext ) => PreferencesGUI(),
  51. keywords = new System.Collections.Generic.HashSet<string>() { "Native", "Camera", "Android", "iOS" }
  52. };
  53. }
  54. #endif
  55. #if !UNITY_2018_3_OR_NEWER
  56. [PreferenceItem( "Native Camera" )]
  57. #endif
  58. public static void PreferencesGUI()
  59. {
  60. EditorGUI.BeginChangeCheck();
  61. Instance.AutomatedSetup = EditorGUILayout.Toggle( "Automated Setup", Instance.AutomatedSetup );
  62. EditorGUI.BeginDisabledGroup( !Instance.AutomatedSetup );
  63. Instance.CameraUsageDescription = EditorGUILayout.DelayedTextField( "Camera Usage Description", Instance.CameraUsageDescription );
  64. Instance.MicrophoneUsageDescription = EditorGUILayout.DelayedTextField( "Microphone Usage Description", Instance.MicrophoneUsageDescription );
  65. EditorGUI.EndDisabledGroup();
  66. if( EditorGUI.EndChangeCheck() )
  67. Instance.Save();
  68. }
  69. }
  70. public class NCPostProcessBuild
  71. {
  72. #if UNITY_IOS
  73. [PostProcessBuild]
  74. public static void OnPostprocessBuild( BuildTarget target, string buildPath )
  75. {
  76. if( !Settings.Instance.AutomatedSetup )
  77. return;
  78. if( target == BuildTarget.iOS )
  79. {
  80. string pbxProjectPath = PBXProject.GetPBXProjectPath( buildPath );
  81. string plistPath = Path.Combine( buildPath, "Info.plist" );
  82. PBXProject pbxProject = new PBXProject();
  83. pbxProject.ReadFromFile( pbxProjectPath );
  84. #if UNITY_2019_3_OR_NEWER
  85. string targetGUID = pbxProject.GetUnityFrameworkTargetGuid();
  86. #else
  87. string targetGUID = pbxProject.TargetGuidByName( PBXProject.GetUnityTargetName() );
  88. #endif
  89. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework MobileCoreServices" );
  90. pbxProject.AddBuildProperty( targetGUID, "OTHER_LDFLAGS", "-framework ImageIO" );
  91. File.WriteAllText( pbxProjectPath, pbxProject.WriteToString() );
  92. PlistDocument plist = new PlistDocument();
  93. plist.ReadFromString( File.ReadAllText( plistPath ) );
  94. PlistElementDict rootDict = plist.root;
  95. if( !string.IsNullOrEmpty( Settings.Instance.CameraUsageDescription ) )
  96. rootDict.SetString( "NSCameraUsageDescription", Settings.Instance.CameraUsageDescription );
  97. if( !string.IsNullOrEmpty( Settings.Instance.MicrophoneUsageDescription ) )
  98. rootDict.SetString( "NSMicrophoneUsageDescription", Settings.Instance.MicrophoneUsageDescription );
  99. File.WriteAllText( plistPath, plist.WriteToString() );
  100. }
  101. }
  102. #endif
  103. }
  104. }