Skip to content

Instantly share code, notes, and snippets.

@sanukin39
Last active October 11, 2023 16:04
Show Gist options
  • Save sanukin39/997d8364d16c5c27dae75a3bc1f1f045 to your computer and use it in GitHub Desktop.
Save sanukin39/997d8364d16c5c27dae75a3bc1f1f045 to your computer and use it in GitHub Desktop.
Unity XcodeAPI Settings Sample
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.iOS.Xcode;
using UnityEditor.Callbacks;
using System.Collections;
public class XcodeSettingsPostProcesser
{
[PostProcessBuildAttribute (0)]
public static void OnPostprocessBuild (BuildTarget buildTarget, string pathToBuiltProject)
{
// Stop processing if targe is NOT iOS
if (buildTarget != BuildTarget.iOS)
return;
// Initialize PbxProject
var projectPath = pathToBuiltProject + "/Unity-iPhone.xcodeproj/project.pbxproj";
PBXProject pbxProject = new PBXProject ();
pbxProject.ReadFromFile (projectPath);
string targetGuid = pbxProject.TargetGuidByName ("Unity-iPhone");
// Sample of adding build property
pbxProject.AddBuildProperty(targetGuid, "OTHER_LDFLAGS", "-all_load");
// Sample of setting build property
pbxProject.SetBuildProperty(targetGuid, "ENABLE_BITCODE", "NO");
// Sample of update build property
pbxProject.UpdateBuildProperty(targetGuid, "OTHER_LDFLAGS", new string[]{"-ObjC"}, new string[]{"-weak_framework"});
// Sample of adding REQUIRED framwrok
pbxProject.AddFrameworkToProject(targetGuid, "Security.framework", false);
// Sample of adding OPTIONAL framework
pbxProject.AddFrameworkToProject(targetGuid, "SafariServices.framework", true);
// Sample of setting compile flags
var guid = pbxProject.FindFileGuidByProjectPath("Classes/UI/Keyboard.mm");
var flags = pbxProject.GetCompileFlagsForFile(targetGuid, guid);
flags.Add("-fno-objc-arc");
pbxProject.SetCompileFlagsForFile(targetGuid, guid, flags);
// Apply settings
File.WriteAllText (projectPath, pbxProject.WriteToString ());
// Samlpe of editing Info.plist
var plistPath = Path.Combine (pathToBuiltProject, "Info.plist");
var plist = new PlistDocument ();
plist.ReadFromFile (plistPath);
// Add string setting
plist.root.SetString ("hogehogeId", "dummyid");
// Add URL Scheme
var array = plist.root.CreateArray ("CFBundleURLTypes");
var urlDict = array.AddDict ();
urlDict.SetString ("CFBundleURLName", "hogehogeName");
var urlInnerArray = urlDict.CreateArray ("CFBundleURLSchemes");
urlInnerArray.AddString ("hogehogeValue");
// Apply editing settings to Info.plist
plist.WriteToFile (plistPath);
}
}
@virusbee
Copy link

great!

@phuctri9898
Copy link

That's useful. Thanks

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