Unity打包xcode修改工程配置代码
1 using System.IO; 2 using UnityEngine; 3 using UnityEditor; 4 using UnityEditor.iOS.Xcode; 5 using UnityEditor.Callbacks; 6 using System.Collections; 7 8 public class XcodeSettingsPostProcesser 9 { 10 11 [PostProcessBuildAttribute (0)] 12 public static void OnPostprocessBuild (BuildTarget buildTarget, string pathToBuiltProject) 13 { 14 15 // Stop processing if targe is NOT iOS 16 if (buildTarget != BuildTarget.iOS) 17 return; 18 19 // Initialize PbxProject 20 var projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj"; 21 PBXProject pbxProject = new PBXProject (); 22 pbxProject.ReadFromFile (projectPath); 23 string targetGuid = pbxProject.TargetGuidByName ("Unity-iPhone"); 24 25 // Sample of adding build property 26 pbxProject.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-all_load"); 27 28 // Sample of setting build property 29 pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO"); 30 31 // Sample of update build property 32 pbxProject.UpdateBuildProperty(targetGuid, "OTHER_LDFLAGS", new string[]{"-ObjC"}, new string[]{"-weak_framework"}); 33 34 // Sample of adding REQUIRED framwrok 35 pbxProject.AddFrameworkToProject(targetGuid, "Security.framework", false); 36 37 // Sample of adding OPTIONAL framework 38 pbxProject.AddFrameworkToProject(targetGuid, "SafariServices.framework", true); 39 40 // Sample of setting compile flags 41 var guid = pbxProject.FindFileGuidByProjectPath("Classes/UI/Keyboard.mm"); 42 var flags = pbxProject.GetCompileFlagsForFile(targetGuid, guid); 43 flags.Add("-fno-objc-arc"); 44 pbxProject.SetCompileFlagsForFile(targetGuid, guid, flags); 45 46 // Apply settings 47 File.WriteAllText (projectPath, pbxProject.WriteToString ()); 48 49 // Samlpe of editing Info.plist 50 var plistPath = Path.Combine (pathToBuiltProject, "Info.plist"); 51 var plist = new PlistDocument (); 52 plist.ReadFromFile (plistPath); 53 54 // Add string setting 55 plist.root.SetString ("hogehogeId", "dummyid"); 56 57 // Add URL Scheme 58 var array = plist.root.CreateArray ("CFBundleURLTypes"); 59 var urlDict = array.AddDict (); 60 urlDict.SetString ("CFBundleURLName", "hogehogeName"); 61 var urlInnerArray = urlDict.CreateArray ("CFBundleURLSchemes"); 62 urlInnerArray.AddString ("hogehogeValue"); 63 64 // Apply editing settings to Info.plist 65 plist.WriteToFile (plistPath); 66 } 67 }