Search code examples
iosxcodeunity-game-enginepost-processing

Enable capabilities for Unity3D xCode Project in postprocess


I have a Unity3D iOS project, where I use Game Center and In-App Purchases(via third-party plugins), but when I build Unity3D project into xCode, in the Capabilities section Game Center and In-App Purchases are disabled. I need to enable them in a PostProcessBuild method. I tried using xCodeApi via this code:

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

But after this xCode is unable to open the created project(it just crashes immidietly). How do I add these two capabilities without setting them manually in xCode?


Solution

  • So the problem was with invalid projPath and that I did not enable iCloud. This code works:

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

    For now I am having trouble only with setting iCloud flags key-value storage and CloudKit to true. As far as I've read, it needs some entitlement file, which I do not know where to find. If you know how to help, I would appreciate some comments.