Are you sick and tired of forgetting to copy that data file to the build directory after each and every standalone build in unity? Sure you could incorporate it into the project but you don’t want that, you just wish that PostProcessBuildPlayer script would work on windows. As of Unity 3.5.2 life just got better…
A long awaited function in Unity was a working post process build player script for standalone players on windows as it was already working on mac’s. As seen in the latest Release Notes and stated in the BuildPipeline a few things have changed.. Apparently the PostProscessBuildplayer script is not supported on windows, which is a bummer for windows users on the other hand it’s easily solved with a new and improved UnityEditor.Callbacks which is both usable on Windows and Mac, So here goes…
P.S. No this is not a custom build window script, it actually triggers from the existing Unity BuildWindow.
using System.IO;
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
public static class PostBuildTrigger
{
private static DirectoryInfo targetdir;
private static string buildname;
private static string buildDataDir;
private static DirectoryInfo projectParent;
// Name of folder in project directory containing files for build
private static string srcName = "CopyToBuild";
private static int filecount;
private static int dircount;
/// Processbuild Function
[PostProcessBuild] // <- this is where the magic happens
public static void OnPostProcessBuild(BuildTarget target, string path)
{
Debug.Log("Post Processing Build");
// Get Required Paths
projectParent = Directory.GetParent(Application.dataPath);
buildname = Path.GetFileNameWithoutExtension(path);
targetdir = Directory.GetParent(path);
char divider = Path.DirectorySeparatorChar;
string dataMarker = "_Data"; // Specifically for Windows Standalone build
buildDataDir = targetdir.FullName + divider + buildname + dataMarker + divider;
// Do Certain actions on your files (Copy, remove or email them to NASA your decision)
filecount = 0;
dircount = 0;
CopyAll(new DirectoryInfo(projectParent.ToString() + divider + srcName), new DirectoryInfo(buildDataDir));
Debug.Log("Copied: " + filecount + " file" +((filecount!=1)?"s":"")+ ", " + dircount + " folder" +((dircount!=1)?"s":""));
}
/// <summary>
/// Recursive Copy Directory Method
/// </summary>
public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
dircount++;
Directory.CreateDirectory(target.FullName);
}
// Copy each file into it’s new directory.
foreach (FileInfo fi in source.GetFiles())
{
filecount++;
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
dircount++;
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
CopyAll(diSourceSubDir, nextTargetSubDir);
}
}
}
Your options say you want to add another method that runs before your current one then you can add a priority to the attribute like this:
[PostProcessBuild(0)] // <- this is where the magic happens
public static void OnPostProcessBuildFirst(BuildTarget target, string path)
{
Debug.Log("I get Executed First");
}
NB: -10 is a higher priority than 100, the default priority is 1
You can use the method to do all kinds of things on your computer after a build is complete just a couple ideas:
- After a large build send an email with a link to the file saying the build is complete.
- Play a sound notifiying you build is complete
- Run a test on your build
- And lots more…
Anyway now you have the tools go make
4 Comments
Trackbacks/Pingbacks
- Helping along the Unity build pipeline for iOS and Android (Part 1) « Big Bad Robots - [...] to write python/perl scripts. Documentation is lacking at this time but you can see an example here. Hopefully Unity ...



very good!
cool
thanks.
How do the post process scripts get executed if you’re compiling from the command line? (i.e. via a Jenkins build) I’m trying to get that working, but they don’t seem to be getting executed.
To me it sounds like the command line build command doesn’t call the PostProcess Callbacks i could be mistaken havent tested it myself yet.. probably a bug. I’ll give it a go when I have the time
You can always post a bug report towards the Unity folk i think it’s a valid bug.