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

izkcnapc  于 2023-11-22  发布在  其他
关注(0)|答案(4)|浏览(185)

我有一个Unity3D iOS项目,在那里我使用Game Center和In-App Purchases(通过第三方插件),但是当我将Unity3D项目构建到xCode中时,在功能部分Game Center和In-App Purchases被禁用。我需要在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中手动设置它们?

bxgwgixi

bxgwgixi1#

这里有一个新的方法,这是可以实现的:

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

字符串

hpxqektj

hpxqektj2#

所以问题是无效的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为真.据我所知,它需要一些权利文件,我不知道在哪里可以找到.如果你知道如何帮助,我会很感激一些意见.

tnkciper

tnkciper3#

您可以使用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更名为namespace,您可以从这里获取xcode API https://bitbucket.org/Unity-Technologies/xcodeapi/src

sshcrbum

sshcrbum4#

截至2023年11月21日,这里正确标记的答案已被弃用。Unity像往常一样破坏了自己的API。例如,以下内容将适用于Game Center(GameKit框架):

[PostProcessBuild]
    public static void ProcessForiOS(BuildTarget buildTarget, string path)
    {
        if (buildTarget == BuildTarget.iOS)
        {
            // Add Game Center capability. Required since Unity and Apple ****** everything up with Xcode 15.
            string projectPath = PBXProject.GetPBXProjectPath(path);
            PBXProject project = new PBXProject();
            project.ReadFromString(File.ReadAllText(projectPath));

            string targetGuid = project.GetUnityMainTargetGuid();
            project.AddFrameworkToProject(targetGuid, "GameKit.framework", false);
            File.WriteAllText(projectPath, project.WriteToString());
}
}

字符串

相关问题