Unity自动打包(EXE)

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表示删除
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值