Created
November 17, 2018 20:46
-
-
Save vmchar/7a565711b6d58fd5bd60869f62d5e3d2 to your computer and use it in GitHub Desktop.
Unity3D iOS post build process to replace app delegate source
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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