1. 程式人生 > 實用技巧 >IOS自動化打包,自動匯入並修改檔案

IOS自動化打包,自動匯入並修改檔案

近幾日學習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