Skip to content

Instantly share code, notes, and snippets.

@AndyJB-Unity
Last active April 4, 2022 10:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndyJB-Unity/bdd5e0d80084a171e45cab0aabfb88f5 to your computer and use it in GitHub Desktop.
Save AndyJB-Unity/bdd5e0d80084a171e45cab0aabfb88f5 to your computer and use it in GitHub Desktop.
Sample Unity Editor Build Post-Process Step to handle Burst generated code on OSX
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEditor.Build;
using UnityEditor.Build.Content;
using UnityEditor.Build.Reporting;
using UnityEditor.iOS.Xcode;
using UnityEngine;
public static class PBXProjExt
{
public static string GetGuidForOSXTarget( this PBXProject proj )
{
return proj.TargetGuidByName(Application.productName);
}
public static void SetSignOnCopyFlag(this PBXProject proj, string targetguid, string fileguid )
{
var method = proj.GetType().GetMethod("BuildFilesGetForSourceFile", BindingFlags.NonPublic | BindingFlags.Instance );
var filedata = method.Invoke( proj,new object[] {targetguid, fileguid});
var filedataType = filedata.GetType();
var field = filedata.GetType().GetField("codeSignOnCopy");
if (field != null)
{
field.SetValue( filedata, true );
}
}
public static string GetPhaseByName(this PBXProject proj, string phaseName )
{
var phases = proj.GetAllBuildPhasesForTarget(proj.GetGuidForOSXTarget());
foreach (var phaseguid in phases)
{
var name = proj.GetBuildPhaseName(phaseguid);
if (name.Equals(phaseName))
return phaseguid;
}
return null;
}
public static string GetCopyPluginsPhase(this PBXProject proj)
{
return GetPhaseByName(proj, @"CopyPlugIns" );
}
}
class MacBurst : IPostprocessBuildWithReport
{
public int callbackOrder { get { return 0; } }
string GetProjectName( string projectPath )
{
return Path.GetFileName(projectPath);
}
string GetProductName()
{
return Application.productName;
}
public void OnPostprocessBuild(BuildReport report)
{
if (report.summary.platform == BuildTarget.StandaloneOSX)
{
string projectName = GetProjectName(report.summary.outputPath);
string projectPath = Path.Combine( report.summary.outputPath, $"{projectName}.xcodeproj/project.pbxproj" );
PBXProject project = new PBXProject();
if (File.Exists(projectPath))
{
var rawproj = File.ReadAllText(projectPath);
project.ReadFromString( rawproj );
string macBurstBundlePath = Path.Combine(report.summary.outputPath,
$"{projectName}/Contents/Plugins/lib_burst_generated.bundle");
if (File.Exists(macBurstBundlePath))
{
var burstfile = project.AddFile(macBurstBundlePath, $"{GetProductName()}/PlugIns/lib_burst_generated.bundle", PBXSourceTree.Source );
project.AddFileToBuildSection( project.GetGuidForOSXTarget(), project.GetCopyPluginsPhase(), burstfile );
project.SetSignOnCopyFlag(project.GetGuidForOSXTarget(),burstfile);
}
var fixedproj = project.WriteToString();
File.WriteAllText( projectPath,fixedproj );
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment