Skip to content

Instantly share code, notes, and snippets.

@DomDomHaas
Last active August 29, 2015 14:11
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 DomDomHaas/4bd78108b8f6889fd240 to your computer and use it in GitHub Desktop.
Save DomDomHaas/4bd78108b8f6889fd240 to your computer and use it in GitHub Desktop.
PostBuildHook for Unity
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor.Callbacks;
using UnityEditor;
using System.IO;
public static class PostBuildHook
{
private static int filecount;
private static int dircount;
[PostProcessBuild]
// add the JSONPersistor.filePath to the build!
public static void OnPostProcessBuild (BuildTarget target, string path)
{
//Debug.Log ("OnPostProcessBuild target " + target + " path " + path);
// Get Required Paths
//DirectoryInfo projectParent = Directory.GetParent (Application.dataPath);
DirectoryInfo projectParent = new DirectoryInfo (Application.dataPath);
string buildname = Path.GetFileNameWithoutExtension (path);
DirectoryInfo targetdir = Directory.GetParent (path);
char divider = Path.DirectorySeparatorChar;
string dataMarker = "";
#if UNITY_STANDALONE_WIN
dataMarker = "_Data";
#endif
string buildDataDir = targetdir.FullName + divider + buildname + dataMarker + "YOUR_FOLDER_NAME";
string unitySourceFolder = projectParent.ToString () + "YOUR_FOLDER_NAME";
//Debug.Log ("copy all from " + unitySourceFolder + " to " + buildDataDir.ToString ());
filecount = 0;
dircount = 0;
CopyAll (new DirectoryInfo (unitySourceFolder), new DirectoryInfo (buildDataDir));
//Debug.Log ("Copied: " + filecount + " file" + ((filecount != 1) ? "s" : "") + ", " + dircount + " folder" + ((dircount != 1) ? "s" : ""));
}
//Your options: say you want to add another method that runs before your previous 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");
}
/// <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);
}
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment