在后处理中启用Unity3D xCode项目的功能

时间:2017-06-01 15:13:09

标签: ios xcode unity3d post-processing

我有一个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中手动设置这两种功能的情况下添加这两种功能?

3 个答案:

答案 0 :(得分:3)

问题在于无效的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时遇到了麻烦。据我所知,它需要一些授权文件,我不知道在哪里可以找到。如果您知道如何提供帮助,我将不胜感激。

答案 1 :(得分:1)

这是可以实现的新方法:

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();
        }
    }
}

答案 2 :(得分:0)

您可以使用ProjectCapabilityManageriCloud存储标记中添加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