VirtualFramework/Assets/Scripts/Editor/VirtualFPostProcess.cs
2025-01-03 13:42:53 +08:00

67 lines
2.1 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
public class VirtualFPostProcess : IPostprocessBuildWithReport
{
// 定义回调优先级(越低越先执行)
public int callbackOrder => 0;
// 在打包完成后执行
public void OnPostprocessBuild(BuildReport report)
{
// 获取项目根目录路径
string projectPath = Application.dataPath;
// 获取Data文件夹的路径
string dataFolderPath = Path.Combine(projectPath, "../Data");
// 获取打包后的输出目录
string buildOutputPath = report.summary.outputPath;
if (buildOutputPath.Contains(".exe"))
{
var paths = buildOutputPath.Split('/');
buildOutputPath = buildOutputPath.Replace(paths[paths.Length - 1], "");
}
// 检查Data文件夹是否存在
if (Directory.Exists(dataFolderPath))
{
// 将Data文件夹复制到打包后的目标目录
string targetDataPath = Path.Combine(buildOutputPath, "Data");
CopyDirectory(dataFolderPath, targetDataPath);
Debug.Log($"Data folder copied to build output directory: {targetDataPath}");
}
else
{
Debug.LogWarning("Data folder not found at: " + dataFolderPath);
}
}
// 递归复制文件夹的辅助方法,覆盖已有文件和文件夹
private void CopyDirectory(string sourceDir, string destinationDir)
{
// 确保目标目录存在
if (!Directory.Exists(destinationDir))
{
Directory.CreateDirectory(destinationDir);
}
// 遍历源目录中的文件
foreach (string file in Directory.GetFiles(sourceDir))
{
string destFile = Path.Combine(destinationDir, Path.GetFileName(file));
File.Copy(file, destFile, true); // 覆盖已有文件
}
// 遍历源目录中的子文件夹
foreach (string subDir in Directory.GetDirectories(sourceDir))
{
string destSubDir = Path.Combine(destinationDir, Path.GetFileName(subDir));
CopyDirectory(subDir, destSubDir); // 递归复制子文件夹
}
}
}