Skip to content

Instantly share code, notes, and snippets.

@samizzo
Last active January 19, 2018 08:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samizzo/21a5f206f88ca975d337dc9f38d799f0 to your computer and use it in GitHub Desktop.
Save samizzo/21a5f206f88ca975d337dc9f38d799f0 to your computer and use it in GitHub Desktop.
using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System;
using System.IO;
public static class CopyFilesBuildPostProcessor
{
private const string PathToCopy = "PathToFiles";
[PostProcessBuild]
public static void OnPostProcessBuild(BuildTarget target, string pathToBuiltProject)
{
// Copy all files from the Unity Assets/{PathToCopy} directory to a directory
// called {PathToCopy} under the build's output directory.
var sourceFullPath = Path.Combine(Application.dataPath, PathToCopy);
var destinationFullPath = Path.Combine(pathToBuiltProject, PathToCopy);
CopyFiles(sourceFullPath, destinationFullPath);
}
private static void CopyFiles(string source, string destination)
{
var files = Directory.GetFiles(source);
foreach (var sourceFile in files)
{
// Get just the filename for the file, so we can determine whether the file
// exists in the destination.
var filename = Path.GetFileName(sourceFile);
var destinationFile = Path.Combine(destination, filename);
var exists = File.Exists(destinationFile);
var doCopy = !exists; // If the file doesn't exist, we definitely need to copy it!
if (exists)
{
// If the file exists at the destination and the source has been modified more
// recently than the destination file, we will copy the source file again.
var sourceTimestamp = File.GetLastWriteTime(sourceFile);
var destinationTimestamp = File.GetLastWriteTime(destinationFile);
doCopy = sourceTimestamp > destinationTimestamp;
}
if (doCopy)
{
try
{
File.Copy(sourceFile, destinationFile);
}
catch (Exception e)
{
Debug.Log("Failed to copy file: " + e.Message);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment