using UnityEditor.Callbacks;
using UnityEditor;
using System.IO;
#if UNITY_IOS
using UnityEditor.iOS.Xcode;
#endif
public static class UnityiOSPostProcessBuildSetting
{
//括号(10)是函数调用的优先度
[PostProcessBuild(10)]
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS)
{
#if UNITY_IOS
//初始化
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string target = proj.TargetGuidByName("Unity-iPhone");
// 添加flag
// proj.AddBuildProperty(target, "OTHER_LDFLAGS", "-ObjC");
//添加framework
// proj.AddFrameworkToProject(target, "AssetsLibrary.framework", false);
// proj.AddFrameworkToProject(target, "IAPSupport.framework", false);
//添加lib
// AddLibToProject(proj, target, "libc++.tbd");
// AddLibToProject(proj, target, "libz.tbd");
// 关闭Bitcode设置
proj.SetBuildProperty(target, "ENABLE_BITCODE", "NO");
// 打开C语言模块设置
proj.SetBuildProperty(target, "CLANG_ENABLE_MODULES", "YES");
{
// 修改info.plist的代码
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict rootDict = plist.root;
//
PlistElement array = null;
if (rootDict.values.ContainsKey("LSApplicationQueriesSchemes"))
{
array = rootDict["LSApplicationQueriesSchemes"].AsArray();
}
else
{
array = rootDict.CreateArray("LSApplicationQueriesSchemes");
}
rootDict.values.TryGetValue("LSApplicationQueriesSchemes", out array);
PlistElementArray Qchemes = array.AsArray();
Qchemes.AddString("fb");
//添加SDK要求接入的权限[权限名,申请说明]
rootDict.SetString("NSBluetoothPeripheralUsageDescription", " Advertisement would like to use bluetooth.");
}
// 应用修改
File.WriteAllText(projPath, proj.WriteToString());
#endif
}
else if (buildTarget == BuildTarget.Android)
{
//TODO
}
}
#if UNITY_IOS
//添加lib方法
static void AddLibToProject(PBXProject inst, string targetGuid, string lib) {
string fileGuid = inst.AddFile("usr/lib/" + lib, "Frameworks/" + lib, PBXSourceTree.Sdk);
inst.AddFileToBuild(targetGuid, fileGuid);
}
#endif
}
网友评论