[英]From Unity to iOS, how to perfectly automate frameworks, settings and plist?
在 Unity3D 中構建到 iOS Xcode 項目時,
如何完美地自動化所有三個
解決方案必須只有最現代的 2019 語法和變體,因為這些年來 Unity 中的這一點略有變化。
文件名,BuildPostProcessor.cs
把它放在文件夾 Assets/Editor/ 中。 (如果它不存在,只需將文件夾“Editor/”准確地放在那里。)
這展示了如何完成所有三個框架、設置和 plist。
// filename BuildPostProcessor.cs
// put it in a folder Assets/Editor/
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
public class BuildPostProcessor {
[PostProcessBuild]
public static void ChangeXcodePlist(BuildTarget buildTarget, string path) {
if (buildTarget == BuildTarget.iOS) {
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistPath);
PlistElementDict rootDict = plist.root;
Debug.Log(">> Automation, plist ... <<");
// example of changing a value:
// rootDict.SetString("CFBundleVersion", "6.6.6");
// example of adding a boolean key...
// < key > ITSAppUsesNonExemptEncryption </ key > < false />
rootDict.SetBoolean("ITSAppUsesNonExemptEncryption", false);
File.WriteAllText(plistPath, plist.WriteToString());
}
}
[PostProcessBuildAttribute(1)]
public static void OnPostProcessBuild(BuildTarget target, string path) {
if (target == BuildTarget.iOS) {
PBXProject project = new PBXProject();
string sPath = PBXProject.GetPBXProjectPath(path);
project.ReadFromFile(sPath);
string tn = PBXProject.GetUnityTargetName();
string g = project.TargetGuidByName(tn);
ModifyFrameworksSettings(project, g);
// modify frameworks and settings as desired
File.WriteAllText(sPath, project.WriteToString());
}
}
static void ModifyFrameworksSettings(PBXProject project, string g) {
// add hella frameworks
Debug.Log(">> Automation, Frameworks... <<");
project.AddFrameworkToProject(g, "blah.framework", false);
project.AddFrameworkToProject(g, "libz.tbd", false);
// go insane with build settings
Debug.Log(">> Automation, Settings... <<");
project.AddBuildProperty(g,
"LIBRARY_SEARCH_PATHS",
"../blahblah/lib");
project.AddBuildProperty(g,
"OTHER_LDFLAGS",
"-lsblah -lbz2");
// note that, due to some Apple shoddyness, you usually need to turn this off
// to allow the project to ARCHIVE correctly (ie, when sending to testflight):
project.AddBuildProperty(g,
"ENABLE_BITCODE",
"false");
}
}
這樣就可以了。
注意 - 拼圖的最后一部分是跨文件復制(可能是數據文件或文本文件)。 實際上,最好只使用“StreamingAssets/”方法,在本 QA 中有完整的解釋。
Unity 2019.3 的固定代碼:
// filename BuildPostProcessor.cs
// put it in a folder Assets/Editor/
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
public class BuildPostProcessor
{
[PostProcessBuild]
public static void ChangeXcodePlist(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS)
{
string plistPath = path + "/Info.plist";
PlistDocument plist = new PlistDocument();
plist.ReadFromFile(plistPath);
PlistElementDict rootDict = plist.root;
Debug.Log(">> Automation, plist ... <<");
// example of changing a value:
// rootDict.SetString("CFBundleVersion", "6.6.6");
// example of adding a boolean key...
// < key > ITSAppUsesNonExemptEncryption </ key > < false />
rootDict.SetBoolean("ITSAppUsesNonExemptEncryption", false);
File.WriteAllText(plistPath, plist.WriteToString());
}
}
[PostProcessBuildAttribute(1)]
public static void OnPostProcessBuild(BuildTarget target, string path)
{
if (target == BuildTarget.iOS)
{
PBXProject project = new PBXProject();
string sPath = PBXProject.GetPBXProjectPath(path);
project.ReadFromFile(sPath);
string g = project.GetUnityFrameworkTargetGuid();
ModifyFrameworksSettings(project, g);
// modify frameworks and settings as desired
File.WriteAllText(sPath, project.WriteToString());
}
}
static void ModifyFrameworksSettings(PBXProject project, string g)
{
// add hella frameworks
Debug.Log(">> Automation, Frameworks... <<");
project.AddFrameworkToProject(g, "blah.framework", false);
project.AddFrameworkToProject(g, "libz.tbd", false);
// go insane with build settings
Debug.Log(">> Automation, Settings... <<");
project.AddBuildProperty(g,
"LIBRARY_SEARCH_PATHS",
"../blahblah/lib");
project.AddBuildProperty(g,
"OTHER_LDFLAGS",
"-lsblah -lbz2");
// note that, due to some Apple shoddyness, you usually need to turn this off
// to allow the project to ARCHIVE correctly (ie, when sending to testflight):
project.AddBuildProperty(g,
"ENABLE_BITCODE",
"false");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.