Skip to content

Instantly share code, notes, and snippets.

@vmchar
Created November 17, 2018 20:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vmchar/7a565711b6d58fd5bd60869f62d5e3d2 to your computer and use it in GitHub Desktop.
Save vmchar/7a565711b6d58fd5bd60869f62d5e3d2 to your computer and use it in GitHub Desktop.
Unity3D iOS post build process to replace app delegate source
using UnityEditor;
using UnityEditor.Callbacks;
using UnityEditor.iOS.Xcode;
using UnityEngine;
namespace QuickActionsiOS
{
public class ReplaceDelegatePostProcess : ScriptableObject
{
/// <summary>
/// Put your custom delegate file here via inspector
/// </summary>
public DefaultAsset NewDelegateFile;
private const string DefaultDelegateName = "UnityAppController.mm";
[PostProcessBuild(999)]
public static void OnPostProcess(BuildTarget buildTarget, string buildPath)
{
if(buildTarget != BuildTarget.iOS) return;
//creating instance of scriptable object to get file path
var instance = ScriptableObject.CreateInstance<ReplaceDelegatePostProcess>();
var delegateFile = instance.NewDelegateFile;
DestroyImmediate(instance);
if(delegateFile == null) return;
//get path to xCode project and project itself
var projectPath = PBXProject.GetPBXProjectPath(buildPath);
var xCodeProject = new PBXProject();
xCodeProject.ReadFromFile(projectPath);
//get paths to new and old delegate
var newDelegatePath = AssetDatabase.GetAssetPath(delegateFile);
var delegatePath = buildPath + "/Classes/";
var oldDelegatePath = delegatePath + DefaultDelegateName;
//remove old delegate, add new one with default unity name
FileUtil.DeleteFileOrDirectory(oldDelegatePath);
FileUtil.CopyFileOrDirectory(newDelegatePath, delegatePath + DefaultDelegateName);
//save changes to xCode project
xCodeProject.WriteToFile(projectPath);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment