Skip to content

Instantly share code, notes, and snippets.

@mattbajorek
Created April 13, 2020 00:12
Show Gist options
  • Save mattbajorek/2f6b513dab04f628b86e996d46d9fd92 to your computer and use it in GitHub Desktop.
Save mattbajorek/2f6b513dab04f628b86e996d46d9fd92 to your computer and use it in GitHub Desktop.
Copy edited Objective C folders and files back to Unity
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using System.IO;
using System.Linq;
public class BuildPostProcessor
{
[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget target, string path)
{
if (target == BuildTarget.iOS)
{
// Get project into C#
var projectPath = PBXProject.GetPBXProjectPath(path);
var project = new PBXProject();
project.ReadFromFile(projectPath);
// Get targetGUID
var targetName = PBXProject.GetUnityTargetName();
var targetGUID = project.TargetGuidByName(targetName);
...
// Add Shell Script to copy folders and files after running successfully
var shellScriptName = "Copy edited Objective C folders and files back to Unity";
var shellPath = "/bin/sh";
var shellScript = $"cp -r \"$PROJECT_DIR/Libraries/Plugins/iOS\" {Directory.GetCurrentDirectory()}/Assets/Plugins";
var allBuildPhasesGUIDS = project.GetAllBuildPhasesForTarget(targetGUID);
var foundShellScript = allBuildPhasesGUIDS.Where(buildPhasesGUID => project.GetBuildPhaseName(buildPhasesGUID) == shellScriptName).FirstOrDefault();
if (foundShellScript == null)
{
project.AddShellScriptBuildPhase(targetGUID, shellScriptName, shellPath, shellScript);
Debug.Log($"Added custom shell script: {shellScriptName}");
}
...
// Overwrite
project.WriteToFile(projectPath);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment