Skip to content

Instantly share code, notes, and snippets.

@danielbierwirth
Last active March 30, 2022 07:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danielbierwirth/74573388e8ab7e7f60c5be9a5d17baa5 to your computer and use it in GitHub Desktop.
Save danielbierwirth/74573388e8ab7e7f60c5be9a5d17baa5 to your computer and use it in GitHub Desktop.
#unity3d post-process class (c#) that copies a directory including content and subdirectories from source to target location as unity build post-process. place this script inside Editor folder somewhere within Assets directory
/// <summary>
/// PostProcessorFileCopy class contains functions to copy source folder and subfolders/files (recursive)
/// to build directory as post process after build. Place this class inside Editor folder.
/// </summary>
public class PostProcessorFileCopy {
[PostProcessBuild]
public static void OnPostprocessBuild(BuildTarget target, string pathToBuildfile) {
// Path to source directory.
string sourceDir = Application.dataPath + "/../"+ _SOURCE_DIRECTORY_2_COPY;
// Path to target directory.
DirectoryInfo buildPath = Directory.GetParent(pathToBuildfile);
string targetDir = buildPath + "/"+ _TARGET_DIRECTORY;
// Check if directory exists at given location.
if (Directory.Exists(sourceDir)) {
if (!Directory.Exists(targetDir))
{
// Create the folder since it doesn't exist yet
Directory.CreateDirectory(targetDir);
}
// Copy source folder to target location
// including subdirectories
DirectoryCopy(sourceDir, targetDir, true);
}
}
/// <summary>
/// Function copies files and subdirectories from source to target.
/// Subdirectories and their content will be copied (recursive) by
/// setting the copySubDirs of the DirectoryCopy method to true.
/// </summary>
/// <param name="sourceDirName">Path to source directory.</param>
/// <param name="destDirName">Path to target directory.</param>
/// <param name="copySubDirs">Set true to recursively copy subdir.</param>
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment