80 lines
2.5 KiB
C#
80 lines
2.5 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;
|
||
string dataFolderPath = Path.Combine(projectPath, "../Data");
|
||
string buildOutputPath = GetValidBuildPath(report);
|
||
|
||
if (Directory.Exists(dataFolderPath))
|
||
{
|
||
// 目标目录强制小写
|
||
string targetDataPath = Path.Combine(buildOutputPath, "Data");
|
||
CopyDirectoryWithLowerCaseNames(dataFolderPath, targetDataPath);
|
||
Debug.Log($"数据目录已复制到: {targetDataPath}");
|
||
}
|
||
else
|
||
{
|
||
Debug.LogWarning("未找到数据目录: " + dataFolderPath);
|
||
}
|
||
}
|
||
|
||
private string GetValidBuildPath(BuildReport report)
|
||
{
|
||
string path = report.summary.outputPath;
|
||
if (path.Contains(".exe"))
|
||
{
|
||
return Path.GetDirectoryName(path);
|
||
}
|
||
return path;
|
||
}
|
||
|
||
private void CopyDirectoryWithLowerCaseNames(string sourceDir, string targetDir)
|
||
{
|
||
// 创建小写目标目录
|
||
//var lowerTargetDir = ConvertToLowerPath(targetDir);
|
||
Directory.CreateDirectory(targetDir);
|
||
|
||
// 复制文件(带小写转换)
|
||
foreach (var file in Directory.GetFiles(sourceDir))
|
||
{
|
||
string fileName = Path.GetFileName(file);
|
||
// 暂时废弃小写转换
|
||
//string lowerName = ConvertToLowerPath(fileName);
|
||
File.Copy(file, Path.Combine(targetDir, fileName), true);
|
||
}
|
||
|
||
// 递归处理子目录(带小写转换)
|
||
foreach (var dir in Directory.GetDirectories(sourceDir))
|
||
{
|
||
string dirName = Path.GetFileName(dir);
|
||
// 暂时废弃小写转换
|
||
//string lowerDirName = ConvertToLowerPath(dirName);
|
||
CopyDirectoryWithLowerCaseNames(dir, Path.Combine(targetDir, dirName));
|
||
}
|
||
}
|
||
|
||
// 中英文混合路径转小写
|
||
private string ConvertToLowerPath(string input)
|
||
{
|
||
char[] chars = input.ToCharArray();
|
||
for (int i = 0; i < chars.Length; i++)
|
||
{
|
||
// 只处理ASCII字母字符(中文等Unicode字符保持不变)
|
||
if (chars[i] >= 'A' && chars[i] <= 'Z')
|
||
{
|
||
chars[i] = (char)(chars[i] | 0x20); // 快速转小写
|
||
}
|
||
}
|
||
return new string(chars);
|
||
}
|
||
} |