Unity 打包后将指定文件/文件夹移动到目标目录

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using UnityEngine;

public class OutputAfterCopy
{
#if UNITY_EDITOR
    const int
        FOLDER = -1,
        FILE = -2;
    [UnityEditor.Callbacks.PostProcessBuildAttribute(1)]
    public static void AfterBuild(UnityEditor.BuildTarget build, string pathToBuilderProject)
    {
        Action<string, string> MoveFolterFunc = null;
        MoveFolterFunc=(endpath,targetpath) =>
        {
            if (!File.Exists(endpath))
            {
                Directory.CreateDirectory(endpath);
            }
            string[] files = Directory.GetFileSystemEntries(targetpath);

            List<string> paths_2 = new List<string>();
            for (int j = 0; j < files.Length; j++)
            {
                try
                {

                    if (File.Exists(files[j]))
                    {
                        FileInfo file = new FileInfo(files[j]);
                        string[] temp= files[j].Split(new char[2] { '/' ,'\\'}, StringSplitOptions.RemoveEmptyEntries);
                        string path_4 = endpath + "/" + temp[temp.Length - 1];
                        if (!File.Exists(path_4))
                        {
                            file.MoveTo(path_4);
                        }
                        else
                        {
                            File.Delete(files[j]);
                        }
                      
                    }
                    else
                        paths_2.Add(files[j]);
                }
                catch (System.Exception e)
                {
                    Debug.LogError("目标文件夹移动失败");
                }
            }
            if (paths_2.Count != 0)
            {
                for (int i = 0; i < paths_2.Count; i++)
                {
                    string[] temp = paths_2[i].Split(new char[2] { '/','\\' },StringSplitOptions.RemoveEmptyEntries);
                    MoveFolterFunc.Invoke(endpath + "/" + temp[temp.Length - 1], paths_2[i]);
                }
            }

            Directory.Delete(targetpath);
        };

        pathToBuilderProject = pathToBuilderProject.Substring(0, pathToBuilderProject.Length - 4);
        //获取拷贝路径 = 打包输出路径 + 包名(打包文件夹名) + 下级路径
        string[] paths = pathToBuilderProject.Split('/');
        pathToBuilderProject = "";
        for (int i = 0; i < paths.Length - 1; i++)
        {
            pathToBuilderProject = pathToBuilderProject + paths[i] + "/";
        }


        KVPair<int, string>[] moveTarget = {//所需要移动的文件或文件夹的路径
            new KVPair<int, string>(FOLDER,pathToBuilderProject + paths[paths.Length - 1] + "_Data/StreamingAssets/Debug"),
 };
        string[] moveEnd = {//目标移动的最终位置
           pathToBuilderProject + "MonoBleedingEdge/",
 };
        Dictionary<int, string> ReName = new Dictionary<int, string>(){//移动的目标是否需要改名,如果需要记录其在moveTarget的索引以及要改的名字
            { 0,"excel"},
        };


        for (int i = 0; i < moveTarget.Length; i++)
        {
            switch (moveTarget[i].Key)
            {
                case FOLDER:
                    if (Directory.Exists(moveTarget[i].Value))
                    {
                        DirectoryInfo directory = new DirectoryInfo(moveTarget[i].Value);
                        string endpath = null;
                        if (ReName.ContainsKey(i))
                        {
                            endpath = moveEnd[i] + ReName[i];
                        }
                        else
                        {
                            string[] temp = moveTarget[i].Value.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
                            endpath = moveEnd[i] + temp[temp.Length - 1];
                        }
                        MoveFolterFunc.Invoke(endpath, moveTarget[i].Value);
                    }
                    else
                    {
                        Debug.LogError("不存在目标文件夹");
                    }
                    break;
                case FILE:
                    if (File.Exists(moveTarget[i].Value))
                    {
                        string[] temp2 = moveTarget[i].Value.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
                        try
                        {
                            File.Move(moveTarget[i].Value, moveEnd[i] + temp2[temp2.Length - 1]);
                        }
                        catch (System.Exception e)
                        {
                            Debug.LogError("目标文件移动失败");
                        }
                        try
                        {
                            if (ReName.ContainsKey(i))
                            {
                                string temp4 = moveEnd[i] + ReName[i];
                                string temp3 = moveEnd[i] + temp2[temp2.Length - 1];
                                FileInfo file = new FileInfo(temp3);
                                file.MoveTo(temp4);
                            }
                        }
                        catch (System.Exception e)
                        {
                            Debug.LogError("目标文件重命名失败");
                        }
                    }
                    else
                    {
                        Debug.LogError("不存在目标文件");
                    }
                    break;
            }
        }
    }
#endif
}

 实现打包之后将目标文件夹(包含文件夹下所有内容)/目标文件进行移动以及重命名

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值