using GluonGui.WorkspaceWindow.Views.WorkspaceExplorer;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEngine.SceneManagement;
/// <summary>
/// 打包过程处理
/// ||过程:OnPreprocessBuild ---> OnProcessScene ---> OnPostprocessBuild
/// </summary>
public class BuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport, IProcessSceneWithReport
{
//打包路径(exe同级目录)
private string buildEXEPath;
//文件列表
private List<string> moveFilePath = new List<string>()
{
//Application.dataPath.Replace("Assets", "config.json"),
};
//文件夹路径
private const string moveFolderName = "Config";
private string unityFolderPath = Application.dataPath.Replace("Assets", moveFolderName);
public int callbackOrder { get => 0; }
public void OnPreprocessBuild(BuildReport report)
{
buildEXEPath = GetParentFilePath(report.summary.outputPath);
UnityEngine.Debug.Log($"打包路径:{buildEXEPath}");
DeleteBuildFolder(Path.GetDirectoryName(report.summary.outputPath));
}
public void OnProcessScene(Scene scene, BuildReport report)
{
}
public void OnPostprocessBuild(BuildReport report)
{
MoveFiles();
MoveFolder();
UnityEngine.Debug.Log($"打包成功!!!");
//结束,打开EXE文件夹
Process.Start(buildEXEPath);
}
//移动文件
private void MoveFiles()
{
for (int i = 0; i < moveFilePath.Count; i++)
{
MoveFile(moveFilePath[i]);
}
}
private void MoveFile(string path)
{
if (File.Exists(path))
{
string fileName = Path.GetFileName(path);
string newdestinationPath = Path.Combine(buildEXEPath, fileName);
if (File.Exists(newdestinationPath))
{
File.Delete(newdestinationPath);
}
File.Copy(path, newdestinationPath, true);
}
}
//移动文件夹
private void MoveFolder()
{
string sourceFolder = unityFolderPath;
string targetFolder = Path.Combine(buildEXEPath, moveFolderName);
CopyFolder(targetFolder, sourceFolder);
}
public void CopyFolder(string targetFolder, string sourceFolder)
{
// 如果目标文件夹不存在,则创建它
if (!Directory.Exists(targetFolder))
{
Directory.CreateDirectory(targetFolder);
}
// 复制文件夹中的文件
string[] files = Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string targetPath = Path.Combine(targetFolder, fileName);
File.Copy(file, targetPath, true);
}
// 复制子文件夹
string[] subFolders = Directory.GetDirectories(sourceFolder);
foreach (string subFolder in subFolders)
{
string folderName = Path.GetFileName(subFolder);
string targetPath = Path.Combine(targetFolder, folderName);
CopyFolderRecursive(subFolder, targetPath);
}
}
private void CopyFolderRecursive(string sourceFolder, string targetFolder)
{
// 如果目标文件夹不存在,则创建它
if (!Directory.Exists(targetFolder))
{
Directory.CreateDirectory(targetFolder);
}
// 复制文件夹中的文件
string[] files = Directory.GetFiles(sourceFolder);
foreach (string file in files)
{
string fileName = Path.GetFileName(file);
string targetPath = Path.Combine(targetFolder, fileName);
File.Copy(file, targetPath, true);
}
// 复制子文件夹
string[] subFolders = Directory.GetDirectories(sourceFolder);
foreach (string subFolder in subFolders)
{
string folderName = Path.GetFileName(subFolder);
string targetPath = Path.Combine(targetFolder, folderName);
CopyFolderRecursive(subFolder, targetPath);
}
}
private string GetParentFilePath(string fullPath)
{
return Path.GetDirectoryName(fullPath) + "\\";
}
//清空打包文件夹
private void DeleteBuildFolder(string path)
{
if (Directory.Exists(path))
{
DirectoryInfo dir = new DirectoryInfo(path);
dir.Delete(true); //第二个参数表示是否删除文件夹本身,true表示删除
}
}
}
Unity自动打包(EXE)
于 2023-07-03 11:41:27 首次发布