前言:unity出ios包的時候,xcode工程都需要設定一些引數,特別是接入一些第三方的SDK
的時候,需要引入一些新的庫,每次出包,都需要操作一次,很麻煩,于是這算是一個自動出包的工具
主要使用類是PBXproject類
[PostProcessBuild()]
public static void OnPostProcessBuild(BuildTarget target, string xcodeprojectpath)
OnPostProcessBuild構建玩xcode工程后,回呼
1.AddCapabilityToPbx 添加授限:
高于unity2017可直接使用 pbxProject.AddCapability(IPHONE_GUID,PBXCapabilityType.SignInWithApple);
我的版本校舊,使用以下方法,檔案路徑 xcodeprojectpath/Unity-iPhone/xxx.entitlements
PlistDocument tempEntitlements = new PlistDocument();
Dictionary<string, PlistElementArray> capabilityList = new Dictionary<string, PlistElementArray>();
capabilityList[你要填的key] = (tempEntitlements.root[你要填的key] = new PlistElementArray()) as PlistElementArray;
capabilityList[你要填的key].values.Add(new PlistElementString(你要填的Value));
tempEntitlements.WriteToFile(xcodeprojectpath/Unity-iPhone/xxx.entitlements);
((PBXProject)pbxProject).SetBuildProperty(IPHONE_GUID, "CODE_SIGN_ENTITLEMENTS", relativeEntitlementFilePath);
list類似一個字典,下面是幾個授權常用的key和value :
com.apple.developer.applesignin Default 對應signin 內購授權
keychain-access-groups id 對應鑰匙串共享key授權,

這個比較特別,
aps-environment development或production 對應推送授權push notify
特別的權限 需要在info.plist添加
string UIBackground = "UIBackgroundModes";
string uibackvalue = m_platform.GetInfo(UIBackground);
if (uibackvalue != null)
{
PlistElementArray backgroudArr = dict.CreateArray(UIBackground);
backgroudArr.AddString(uibackvalue);
}
2.修改teamid和證書
pbxProject.SetBuildProperty(IPHONE_GUID, Key, Value);
一句代碼,下面是key和value對應,Automatic代表自動分發證書
Key CODE_SIGN_STYLE PROVISIONING_PROFILE_SPECIFIER DEVELOPMENT_TEAM
Value Manual/Automatic 證書名 teamid
3.引入庫
普通庫:
fileGuid = _pbx.AddFile(path, path, PBXSourceTree.Sdk);
_pbx.AddFileToBuild(tagProGuid, fileGuid);
動態庫:
fileGuid = _pbx.AddFile(path, path, PBXSourceTree.Sdk);
_pbx.AddFileToBuild(tagProGuid, fileGuid);
PBXProjectExtensions.AddFileToEmbedFrameworks(_pbx, tagProGuid, fileGuid)
添加的庫需要配置searchpath選項:
framework的庫使用 _pbx.AddBuildProperty(tagProGuid, "FRAMEWORK_SEARCH_PATHS", "$(PROJECT_DIR)/" + 路徑);
.a的庫使用 _pbx.AddBuildProperty(tagProGuid, "LIBRARY_SEARCH_PATHS", "$(PROJECT_DIR)/" + 路徑);
embed動態的庫_pbx.SetBuildProperty(tagProGuid, "LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");
4.修改buildsetting
設定
pbxProject.AddBuildProperty(IPHONE_GUID,"OTHER_LDFLAGS"," -ObjC");
pbxProject.SetBuildProperty(IPHOTHER_LDFLAGSONE_GUID, VALID_ARCHS,arm64);
5.修改info.plist
PlistDocument plist = new PlistDocument();
string filePath = Path.Combine(SaveXcodePath, "Info.plist");
plist.ReadFromFile(filePath);
#region 打開設定權限
PlistElementDict dict = plist.root.AsDict();
PlistElementArray arrartemp = dict.CreateArray("CFBundleURLTypes");
List<string> listSetURLTypees = new List<string>();
m_platform.GetXmlSetXcodeList("Info/CFBundleURLTypes/CFBundleURLSchemes", ref listSetURLTypees);
//Dictionary<string, PlistElementArray> URLTypesDict = new Dictionary<string, PlistElementArray>();
foreach (var v in listSetURLTypees)
{
Debug.Log("sssssssssssss" + v);
PlistElementDict dictemp = arrartemp.AddDict();
Dictionary<string, PlistElementArray> URLTypesDict = new Dictionary<string, PlistElementArray>();
URLTypesDict["CFBundleTypeRole"] = dictemp.CreateArray("CFBundleTypeRole");
URLTypesDict["CFBundleTypeRole"].AddString("Editor");
URLTypesDict["CFBundleURLSchemes"] = dictemp.CreateArray("CFBundleURLSchemes");
URLTypesDict["CFBundleURLSchemes"].AddString(v);
}
plist.WriteToFile(filePath);
設定xcscheme
string filePath = Path.Combine(SaveXcodePath, "Unity-iPhone.xcodeproj/xcshareddata/xcschemes/Unity-iPhone.xcscheme");
string content = File.ReadAllText(filePath);
if (content.IndexOf("enableGPUValidationMode") != -1)
return;
int index = content.IndexOf("allowLocationSimulation");
if (index == -1)
{
Logger.Error("not find allowLocationSimulation option");
return;
}
content = content.Insert(index, "enableGPUValidationMode=\"1\" ");
File.WriteAllText(filePath, content);
保存
string rootPath = PBXProject.GetPBXProjectPath(SaveXcodePath);((PBXProject)pbxProject).WriteToFile(rootPath);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/291945.html
標籤:其他
