利用unity的PBXObject来调整打ios包的设置

TAG
Unity
编写日期
Oct 31, 2022
参考文档链接:
 
1.获取ios工程对象
通过OnPostprocessBuild方法里面的path可以很简单的得到一个PBXObject对象
public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
  string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
  PBXProject proj = new PBXProject();
  proj.ReadFromFile(projPath);
}
 
2.通过PBXObject获取到目标Target
基本上所有的设置都会在这个目标里完成 设置签名,路径(Framework search pathLibrary search pathOther link flags)等的地方
 
3..设置自动签名
proj.SetBuildProperty(target, "CODE_SIGN_IDENTITY", "Apple Development");
proj.SetBuildProperty(target, "CODE_SIGN_STYLE", "Automatic");
proj.SetTeamId(target, teamId); //teamId 是对应开发者的团队id (在苹果后台可以看到)
 
4.添加系统的Framework
proj.AddFrameworkToProject(target, "AdSupport.framework", true);
proj.AddFrameworkToProject(target, "CoreTelephony.framework", true);
proj.AddFrameworkToProject(target, "StoreKit.framework", true); //内购需要 否则PBXCapabilityType.InAppPurchase会加不上
 
5.设置bitcode和Other link flags
// 设置 BitCode
proj.SetBuildProperty(target, "ENABLE_BITCODE", "false");
// 设置 other link flags -ObjC
proj.AddBuildProperty (target, "OTHER_LDFLAGS", "-ObjC");
 
6.添加系统的tbd库 (和添加Framework不一样)
// 自定义一个添加方法
private static void AddLibToProject(PBXProject proj, string target, string lib) {
   string file = proj.AddFile("usr/lib/" + lib, "Frameworks/" + lib, PBXSourceTree.Sdk);
    proj.AddFileToBuild(target, file);
}
// 调用
AddLibToProject(proj,target,"libc++.1.tbd");
AddLibToProject(proj,target,"libresolv.tbd");
 
7.添加自定义的动态库(Embed&Sign)
string defaultLocationInProj = Application.dataPath+"/Editor/Plugins/iOS";     //framework 存放的路径
const string coreFrameworkName = "boxjing.framework";  // framework 的文件名
string framework = Path.Combine(defaultLocationInProj, coreFrameworkName);
string fileGuid = proj.AddFile(framework, "Frameworks/" + coreFrameworkName, PBXSourceTree.Sdk);
PBXProjectExtensions.AddFileToEmbedFrameworks(proj, target, fileGuid);
proj.SetBuildProperty(target, "LD_RUNPATH_SEARCH_PATHS", "$(inherited) @executable_path/Frameworks");
 
8.添加内购和推送,这里需要把正确运行工程的entitlements文件复制到工程里一份,后面让代码自动再复制进iOS工程里使用
proj.AddCapability(target, PBXCapabilityType.InAppPurchase);
// 添加 Capabilities  -  推送
string entitlement = Application.dataPath+ "/Editor/AppleNative/Unity-iPhone.entitlements";
File.Copy(entitlement, path+"/Unity-iPhone.entitlements");
proj.AddCapability(target, PBXCapabilityType.PushNotifications,"Unity-iPhone.entitlements",true);
 
9.修改Info.plist文件
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromString(File.ReadAllText(plistPath));
PlistElementDict infoDict = plist.root;

File.WriteAllText(plistPath, plist.WriteToString());
 
10.修改version和build
infoDict.SetString("CFBundleShortVersionString",version);   //version
infoDict.SetString("CFBundleVersion",build);                 //build
 
11.增加权限描述
// 权限  根据自己的项目 修改后面的提示文案
infoDict.SetString("NSLocationWhenInUseUsageDescription", "为了发现周围的好友,请允许App访问您的地里位置"); //地理位置
infoDict.SetString("NSPhotoLibraryUsageDescription", "为了能选择照片进行上传,请允许App访问您的相册");      //相册
infoDict.SetString("NSMicrophoneUsageDescription", "为了能够录制声音,请允许App访问您的麦克风权限");     //麦克风
 
12..整个文件的替换
string UnityAppControllerMM     = Application.dataPath + "/Editor/AppleNative/UnityAppController.mm"; //获取到我们自己的代码文件路径
string tagUnityAppControllerMM  = path + "/Classes/UnityAppController.mm"; //要替换掉工程里面的文件路径
if (File.Exists(tagUnityAppControllerMM)) //如果有 先删除掉 再复制过去
{
     File.Delete(tagUnityAppControllerMM);
}
File.Copy(UnityAppControllerMM, tagUnityAppControllerMM);