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
}
实现打包之后将目标文件夹(包含文件夹下所有内容)/目标文件进行移动以及重命名