Skip to content

Instantly share code, notes, and snippets.

@LosoncziTamas
Last active November 17, 2022 10:58
Show Gist options
  • Select an option

  • Save LosoncziTamas/c5bbaac6f8ad2292c2cc705f255a7eb2 to your computer and use it in GitHub Desktop.

Select an option

Save LosoncziTamas/c5bbaac6f8ad2292c2cc705f255a7eb2 to your computer and use it in GitHub Desktop.
Unity build script to exclude content of StreamingAssets from builds.
using System;
using System.IO;
using System.Text;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
namespace Editor
{
public class StreamingAssetsExcluder : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
private static string GetTempFolderPath => Path.Combine(Application.persistentDataPath, "Temp");
public int callbackOrder { get; }
public void OnPreprocessBuild(BuildReport report)
{
PurgeDirectory(GetTempFolderPath);
MoveDirectoryContent(Application.streamingAssetsPath, GetTempFolderPath);
}
// TODO: add recovery in case of build failure
public void OnPostprocessBuild(BuildReport report)
{
MoveDirectoryContent(GetTempFolderPath, Application.streamingAssetsPath);
}
private static void PurgeDirectory(string directoryPath)
{
if (Directory.Exists(directoryPath))
{
Directory.Delete(directoryPath, recursive: true);
}
Directory.CreateDirectory(directoryPath);
}
private static void MoveDirectoryContent(string sourceDirPath, string destinationDirPath)
{
if (!Directory.Exists(destinationDirPath))
{
return;
}
var files = Directory.GetFiles(sourceDirPath);
var directories = Directory.GetDirectories(sourceDirPath);
var movedContentLog = new StringBuilder($"[{nameof(StreamingAssetsExcluder)}]");
foreach (var filePath in files)
{
if (TryMoveFile(filePath, destinationDirPath))
{
movedContentLog.Append($"\n File: {filePath} moved to {destinationDirPath}.");
}
}
foreach (var directoryPath in directories)
{
if (TryMoveDirectory(directoryPath, destinationDirPath))
{
movedContentLog.Append($"\n Directory: {directoryPath} moved to {destinationDirPath}.");
}
}
Debug.Log(movedContentLog);
}
private static bool TryMoveFile(string filePath, string destinationDirPath)
{
var fileInfo = new FileInfo(filePath);
var isHiddenFile = fileInfo.Name.StartsWith(".");
if (isHiddenFile)
{
return false;
}
var destFileName = Path.Combine(destinationDirPath, fileInfo.Name);
return TryOverwriteFileMove(filePath, destFileName);
}
private static bool TryMoveDirectory(string directory, string destinationDirPath)
{
var dirInfo = new DirectoryInfo(directory);
var destDirName = Path.Combine(destinationDirPath, dirInfo.Name);
try
{
Directory.Move(directory, destDirName);
}
catch (Exception e)
{
Debug.LogError(e);
return false;
}
return true;
}
private static bool TryOverwriteFileMove(string source, string destination)
{
try
{
File.Copy(source, destination, true);
File.Delete(source);
}
catch (Exception e)
{
Debug.LogError(e);
return false;
}
return true;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment