Skip to content

Instantly share code, notes, and snippets.

@eriseven
Created December 17, 2021 03:33
Show Gist options
  • Save eriseven/26b5d88536a9b7e21186fc12abefb714 to your computer and use it in GitHub Desktop.
Save eriseven/26b5d88536a9b7e21186fc12abefb714 to your computer and use it in GitHub Desktop.
[Unity, iOS] Adding iOS Capability via Script

Adding iOS Capability via script

Overview

iOS' capability setting can be modified by Unity Editor script.

I'm using Jenkins build machine. And sometimes the xcode project's capability settings modified. So, I need to add below script to add capabilities automatically.

The following script let you add capabilities for what you want to use. See also apple developer document to see what kind of capability you can use.

using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;

public class XCodePostProcessBuild
{
  [PostProcessBuild]
  public static void OnPostProcessBuild(BuildTarget buildTarget, string pathToBuiltProject)
  {
    if (buildTarget != BuildTarget.iOS) return;
    
    string projPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
    ProjectCapabilityManager projCapability = new ProjectCapabilityManager(projPath, "Unity-iPhone/(entitlement file)", "Unity-iPhone");
    
    projCapability.AddBackgroundModes(BackgroundModes.RemoteNotifications);
    projCapability.AddPushNotifications(false);
    projCapability.AddiCloud(true, true, null);
    
    projCapability.WriteToFile();
  }
}

The entitlement file is located in [your xcode project built]/Unity-iPhone/, and the file's name ends with .entitlements.

PostProcessBuild attribute let a method to get a notification just after building the player. It has an option to provide an order index in the callback, start from 0.

References

Stack Overflow
Apple Develoer Documents

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment