unity--5.x自帶API打包xcode新增framework、plist
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode; // XcodeAPI
using System.Collections;
using System.IO;
public class XcodeProjectMod : MonoBehaviour
{
// 一點實用功能(參考http://goo.gl/fzYig8)
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);
foreach (var file in Directory.GetFiles(srcPath))
File.Copy(file, Path.Combine(dstPath, Path.GetFileName(file)));
foreach (var dir in Directory.GetDirectories(srcPath))
CopyAndReplaceDirectory(dir, Path.Combine(dstPath, Path.GetFileName(dir)));
}
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iPhone)
{
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string target = proj.TargetGuidByName("Unity-iPhone");
// 新增系統框架
proj.AddFrameworkToProject(target, "AssetsLibrary.framework", false);
// 新增自己的框架
CopyAndReplaceDirectory("Assets/Lib/mylib.framework", Path.Combine(path, "Frameworks/mylib.framework"));
proj.AddFileToBuild(target, proj.AddFile("Frameworks/mylib.framework", "Frameworks/mylib.framework", PBXSourceTree.Source));
// 新增檔案
var fileName = "my_file.xml";
var filePath = Path.Combine("Assets/Lib", fileName);
File.Copy(filePath, Path.Combine(path, fileName));
proj.AddFileToBuild(target, proj.AddFile(fileName, fileName, PBXSourceTree.Source));
// 設定為響應ipa無法用Yosemite寫入的錯誤
proj.SetBuildProperty(target, "CODE_SIGN_RESOURCE_RULES_PATH", "$(SDKROOT)/ResourceRules.plist");
// 設定/新增框架的搜尋路徑
proj.SetBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(inherited)");
proj.AddBuildProperty(target, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/Frameworks");
File.WriteAllText(projPath, proj.WriteToString());
}
}
}