近几日学习IOS自动化打包,特此记录下:
#if UNITY_IOS || UNITY_IPHONE using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor.iOS.Xcode; using UnityEditor.Callbacks; using UnityEditor; using System.IO; using UnityEditor.XCodeEditor; public static class XcodePost { [PostProcessBuild(700)] public static void OnPostProcessBuild(BuildTarget target,string pathToBuildProject) { if (target != BuildTarget.iOS) return; //修改属性文件 var projPath = pathToBuildProject + "/Unity-iPhone.xcodeproj/project.pbxproj"; var proj = new PBXProject(); proj.ReadFromFile(projPath); var targetGuid = proj.TargetGuidByName("Unity-iPhone"); proj.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "YES"); proj.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-ObjC"); proj.AddFrameworkToProject(targetGuid, "libz.tbd", false); proj.AddFrameworkToProject(targetGuid, "libresolv.tbd", false); proj.AddFrameworkToProject(targetGuid, "libsqlite3.tbd", false); proj.AddFrameworkToProject(targetGuid, "CoreTelephony.framework", false); //proj.AddFrameworkToProject(targetGuid, "HeroStatistics.framework", true); proj.AddFrameworkToProject(targetGuid, "SystemConfiguration.framework", false); proj.WriteToFile(projPath); //复制framework包 CopyAndReplaceDirectory("Assets/lib/HeroStatistics.framework", Path.Combine(pathToBuildProject, "Frameworks/HeroStatistics.framework")); proj.AddFileToBuild(targetGuid, proj.AddFile("Frameworks/HeroStatistics.framework", "Frameworks/HeroStatistics.framework", PBXSourceTree.Source)); //修改infolist文件 string plistPath = pathToBuildProject + "/Info.plist"; PlistDocument plist = new PlistDocument(); plist.ReadFromString(File.ReadAllText(plistPath)); PlistElementDict rootDict = plist.root; PlistElementDict pedic = rootDict.CreateDict("HeroConfigInfo"); pedic.SetString("channelname", "IOS"); plist.WriteToFile(plistPath); //修改代码文件 string unityAppControllerPath = pathToBuildProject + "/Classes/UnityAppController.mm"; XClass UnityAppController = new XClass(unityAppControllerPath); string importStr = "\n" + "#import <HeroStatistics/HeroStatisticsManager.h>"; UnityAppController.WriteBelow("#import <OpenGLES/ES2/glext.h>", importStr); string codeStr = "\n" + "-(void)viewDidLoad {\n" + "[[HeroStatisticsManager sharedInstance] initConfig];\n" + "NSLog(@\" Init Sdk ----- !!\");\n}"; UnityAppController.WriteBelow("- (void)preStartUnity {}", codeStr); string insetStr = "\n" + " [self viewDidLoad];\n"; UnityAppController.WriteBelow("UnitySetPlayerFocus(1);", insetStr); } internal static void CopyAndReplaceDirectory(string srcPath, string dstPath) { //路径下该文件夹若存在,则删除 if (Directory.Exists(dstPath)) { Directory.Delete(dstPath); } //路径下的文件若存在,则删除 if (File.Exists(dstPath)) { File.Delete(dstPath); } //创建该路径下文件夹 Directory.CreateDirectory(dstPath); Debug.Log(dstPath + "----" + srcPath); foreach (var file in Directory.GetFiles(srcPath)) { Debug.Log(Path.Combine(dstPath, Path.GetFileName(file))); File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file))); } foreach (var dir in Directory.GetDirectories(srcPath)) CopyAndReplaceDirectory(dir, Path.Combine(dstPath, Path.GetFileName(dir))); } } #endif