在后处理中启用 Unity3D xCode 项目的功能
Enable capabilities for Unity3D xCode Project in postprocess
我有一个 Unity3D iOS 项目,我在其中使用游戏中心和应用内购买(通过第三方插件),但是当我将 Unity3D 项目构建到 xCode 时,在功能游戏中心和应用内购买部分被禁用。我需要在 PostProcessBuild 方法中启用它们。我尝试通过以下代码使用 xCodeApi:
string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string target = proj.TargetGuidByName("Unity-iPhone");
proj.AddCapability (target, PBXCapabilityType.GameCenter);
proj.AddCapability (target, PBXCapabilityType.InAppPurchase);
File.WriteAllText(projPath, proj.ToString());
但此后xCode无法打开创建的项目(直接崩溃)。如何在 xCode 中添加这两个功能而不手动设置它们?
所以问题出在无效的 projPath 上,我没有启用 iCloud。此代码有效:
[PostProcessBuild(999)]
public static void AddCapabilities(BuildTarget buildTarget, string pathToBuiltProject)
{
if (buildTarget == BuildTarget.iOS) {
string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject proj = new PBXProject ();
proj.ReadFromString (File.ReadAllText (projPath));
string target = proj.TargetGuidByName ("Unity-iPhone");
proj.AddCapability (target, PBXCapabilityType.iCloud);
proj.AddCapability (target, PBXCapabilityType.GameCenter);
proj.AddCapability (target, PBXCapabilityType.InAppPurchase);
File.WriteAllText (projPath, proj.WriteToString ());
}
}
目前我只有在将 iCloud 标志键值存储和 CloudKit 设置为 true 时遇到问题。据我所知,它需要一些授权文件,我不知道在哪里可以找到。如果您知道如何提供帮助,我将不胜感激。
您可以使用 ProjectCapabilityManager
添加 iCloud
具有 key-value
存储标志的功能
using XcodeUnityCapability = UnityEditor.iOS.XcodeUnity.ProjectCapabilityManager;
public class IosPostProcessBuild : MonoBehaviour
{
[PostProcessBuild]
private static void PostBuildActions(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS)
{
string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
XcodeUnityCapability projCapability = new XcodeUnityCapability(projPath, "Unity-iPhone/mmk.entitlements", "Unity-iPhone");
projCapability.AddGameCenter();
string[] empty = null;
projCapability.AddiCloud(true, false, empty);
projCapability.WriteToFile();
}
}
}
XcodeUnity
是重命名的命名空间,你可以从这里 xcode api https://bitbucket.org/Unity-Technologies/xcodeapi/src
这是可以实现的新方法:
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.iOS.Xcode;
public class CapabilityPostprocessBuild : IPostprocessBuildWithReport
{
public int callbackOrder => 999;
public void OnPostprocessBuild(BuildReport report)
{
OnPostprocessBuild(report.summary.platform, report.summary.outputPath);
}
public void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS)
{
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromFile(projPath);
ProjectCapabilityManager manager = new ProjectCapabilityManager(
projPath,
"Entitlements.entitlements",
targetGuid: proj.GetUnityMainTargetGuid()
);
manager.AddiCloud(true, false, null);
manager.WriteToFile();
}
}
}
我有一个 Unity3D iOS 项目,我在其中使用游戏中心和应用内购买(通过第三方插件),但是当我将 Unity3D 项目构建到 xCode 时,在功能游戏中心和应用内购买部分被禁用。我需要在 PostProcessBuild 方法中启用它们。我尝试通过以下代码使用 xCodeApi:
string projPath = PBXProject.GetPBXProjectPath(pathToBuiltProject);
PBXProject proj = new PBXProject();
proj.ReadFromString(File.ReadAllText(projPath));
string target = proj.TargetGuidByName("Unity-iPhone");
proj.AddCapability (target, PBXCapabilityType.GameCenter);
proj.AddCapability (target, PBXCapabilityType.InAppPurchase);
File.WriteAllText(projPath, proj.ToString());
但此后xCode无法打开创建的项目(直接崩溃)。如何在 xCode 中添加这两个功能而不手动设置它们?
所以问题出在无效的 projPath 上,我没有启用 iCloud。此代码有效:
[PostProcessBuild(999)]
public static void AddCapabilities(BuildTarget buildTarget, string pathToBuiltProject)
{
if (buildTarget == BuildTarget.iOS) {
string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject proj = new PBXProject ();
proj.ReadFromString (File.ReadAllText (projPath));
string target = proj.TargetGuidByName ("Unity-iPhone");
proj.AddCapability (target, PBXCapabilityType.iCloud);
proj.AddCapability (target, PBXCapabilityType.GameCenter);
proj.AddCapability (target, PBXCapabilityType.InAppPurchase);
File.WriteAllText (projPath, proj.WriteToString ());
}
}
目前我只有在将 iCloud 标志键值存储和 CloudKit 设置为 true 时遇到问题。据我所知,它需要一些授权文件,我不知道在哪里可以找到。如果您知道如何提供帮助,我将不胜感激。
您可以使用 ProjectCapabilityManager
添加 iCloud
具有 key-value
存储标志的功能
using XcodeUnityCapability = UnityEditor.iOS.XcodeUnity.ProjectCapabilityManager;
public class IosPostProcessBuild : MonoBehaviour
{
[PostProcessBuild]
private static void PostBuildActions(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS)
{
string projPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
XcodeUnityCapability projCapability = new XcodeUnityCapability(projPath, "Unity-iPhone/mmk.entitlements", "Unity-iPhone");
projCapability.AddGameCenter();
string[] empty = null;
projCapability.AddiCloud(true, false, empty);
projCapability.WriteToFile();
}
}
}
XcodeUnity
是重命名的命名空间,你可以从这里 xcode api https://bitbucket.org/Unity-Technologies/xcodeapi/src
这是可以实现的新方法:
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEditor.iOS.Xcode;
public class CapabilityPostprocessBuild : IPostprocessBuildWithReport
{
public int callbackOrder => 999;
public void OnPostprocessBuild(BuildReport report)
{
OnPostprocessBuild(report.summary.platform, report.summary.outputPath);
}
public void OnPostprocessBuild(BuildTarget buildTarget, string path)
{
if (buildTarget == BuildTarget.iOS)
{
string projPath = PBXProject.GetPBXProjectPath(path);
PBXProject proj = new PBXProject();
proj.ReadFromFile(projPath);
ProjectCapabilityManager manager = new ProjectCapabilityManager(
projPath,
"Entitlements.entitlements",
targetGuid: proj.GetUnityMainTargetGuid()
);
manager.AddiCloud(true, false, null);
manager.WriteToFile();
}
}
}