using System; using System.Collections; using System.Collections.Generic; using System.IO; using UnityEditor; using UnityEditor.Build; using UnityEditor.Build.Reporting; #if UNITY_IOS using UnityEditor.iOS.Xcode; #endif using UnityEngine; namespace CoreBluetoothEditor { public class BuildPostprocessor : IPostprocessBuildWithReport { public int callbackOrder { get { return 1; } } public void OnPostprocessBuild(BuildReport report) { #if UNITY_IOS var buildTarget = report.summary.platform; if (buildTarget == BuildTarget.iOS) { ProcessForiOS(report); } Debug.Log("BuildPostprocessor.OnPostprocessBuild for target " + report.summary.platform + " at path " + report.summary.outputPath); #endif } #if UNITY_IOS void ProcessForiOS(BuildReport report) { var buildOutputPath = report.summary.outputPath; UpdateInfoPlist(buildOutputPath, InfoPlistData()); UpdatePBXProject(buildOutputPath, project => { DisableBitcode(project); }); } void UpdateInfoPlist(string buildOutputPath, Dictionary plistData) { var plistPath = Path.Combine(buildOutputPath, "Info.plist"); var plist = new PlistDocument(); plist.ReadFromFile(plistPath); var rootDict = plist.root; foreach (var kvp in plistData) { rootDict.SetString(kvp.Key, kvp.Value); } plist.WriteToFile(plistPath); } Dictionary InfoPlistData() { return new Dictionary() { { "NSBluetoothAlwaysUsageDescription", "use Bluetooth" } }; } void UpdatePBXProject(string buildOutputPath, Action action) { var pbxProjectPath = PBXProject.GetPBXProjectPath(buildOutputPath); var project = new PBXProject(); project.ReadFromFile(pbxProjectPath); action(project); project.WriteToFile(pbxProjectPath); } void DisableBitcode(PBXProject project) { var mainTargetGuid = project.GetUnityMainTargetGuid(); project.SetBuildProperty(mainTargetGuid, "ENABLE_BITCODE", "NO"); var unityFrameworkTargetGuid = project.GetUnityFrameworkTargetGuid(); project.SetBuildProperty(unityFrameworkTargetGuid, "ENABLE_BITCODE", "NO"); } #endif } }