今天打包AssetBundle时候发现有资源重名了。调用出错所以写个检查的简单工具在打包之前自动做检查。(感冒写得不好)
/*
* 创建时间:2015-8-10 星华
* 文件帮助类
*/
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System;
public class FileHelper
{
/// <summary>
/// 获取路径下面的所有文件
/// </summary>
/// <param name="Path">文件夹的路径</param>
/// <param name="outDirList">输出的名字列表:</param>
/// <param name="flag">文件标准默认null</param>
/// <param name="cancleSuffix">是否去掉后缀</param>
public static bool GetDirAllFiles(string Path, ref Dictionary<string,string> outDic, string flag = null, bool cancleSuffix = true)
{
if (!Directory.Exists(Path))
{
return false;
}
#if !UNITY_WEBPLAYER //Webplayers do not support any kind of local file IO for security reasons
DirectoryInfo dirInfo = new DirectoryInfo(Path);
FileInfo[] files = dirInfo.GetFiles();
foreach(FileInfo fo in files)
{
string name = fo.Name;
if (!name.Contains(".meta"))
{
if (flag != null)
{
if (name.Contains(flag))
{
if (cancleSuffix == true) name = name.Split('.')[0];
outDic.Add(name, fo.FullName);
}
}
else
{
if (cancleSuffix == true) name = name.Split('.')[0];
outDic.Add(name, fo.FullName);
}
}
}
return true;
#else
return false;
#endif
}
/// <summary>
/// 深度获取文件夹下面所有文件包括子文件夹
/// 如果文件夹下面有完全相同文件就报错 包含后缀
/// </summary>
/// <param name="Path">Path.</param>
/// <param name="outDic">Out dic.</param>
/// <param name="flag">Flag.</param>
/// <param name="cancleSuffix">If set to <c>true</c> cancle suffix.</param>
public static void GetDeepDirAllFilesWithSuffix(string Path, ref Dictionary<string,string> outDic, string flag = null, bool cancleSuffix = true)
{
DirectoryInfo folder = new DirectoryInfo (Path);
FileSystemInfo[] files = folder.GetFileSystemInfos ();
int length = files.Length;
for (int i = 0; i < length; i++) {
if(files[i] is DirectoryInfo)
{
GetDeepDirAllFilesWithSuffix(files[i].FullName,ref outDic,flag,cancleSuffix);
}
else
{
if(!files[i].Name.EndsWith(".meta") && !files[i].Name.EndsWith(".DS_Store") )
{
if(outDic.ContainsKey(files[i].Name))
{
Debug.LogError("all Same Name Asset In folder Name :" + files[i].Name);
}else{
outDic.Add(files[i].Name,files[i].FullName);
}
}
}
}
}
/// <summary>
/// 找出相同名字的文件 包含后缀
/// outDic 同文件,key 是全路径 ,vuale 名字
/// </summary>
public static void GetDeepDirAllFilesWithSuffix(string Path, ref Dictionary<string,string> outDic,ref Dictionary<string,string> tempDic)
{
DirectoryInfo folder = new DirectoryInfo (Path);
FileSystemInfo[] files = folder.GetFileSystemInfos ();
int length = files.Length;
for (int i = 0; i < length; i++) {
if(files[i] is DirectoryInfo)
{
GetDeepDirAllFilesWithSuffix(files[i].FullName,ref outDic,ref tempDic);
}
else
{
if(!files[i].Name.EndsWith(".meta") && !files[i].Name.EndsWith(".DS_Store") )
{
if(tempDic.ContainsKey(files[i].Name))
{
outDic.Add(files[i].FullName,files[i].Name);
}else{
tempDic.Add(files[i].Name,files[i].FullName);
}
}
}
}
}
/// <summary>
/// 找出相同名字的文件,不包含后缀 比如有1.png ,1.tex 就会找出来
/// </summary>
/// <param name="Path">文件夹的路径</param>
/// <param name="outDic">Out dic.</param>
/// <param name="flag">Flag.</param>
/// <param name="cancleSuffix">If set to <c>true</c> cancle suffix.</param>
public static void GetDeepDirAllFilesWithOutSuffix(string Path,ref Dictionary<string,string> sameDic)
{
Dictionary<string,string> outDic = new Dictionary<string,string > ();
Dictionary<string,string> nameDic = new Dictionary<string,string > ();
GetDeepDirAllFilesWithSuffix (Path, ref outDic, null, false);
foreach (KeyValuePair<string,string> temp in outDic) {
string name = temp.Key.Split('.')[0]; //去掉后缀的名字
if(!nameDic.ContainsKey(name))
nameDic.Add(name,temp.Key); //如果不包含在里面就加进去
else
{
sameDic.Add(temp.Key,temp.Value);//添加到相同字典里面
//把上次和它相同的也丢进去
string samekey = nameDic[name];
if(!sameDic.ContainsKey(samekey))
sameDic.Add(samekey,outDic[samekey]);
}
}
}
/// <summary>
/// 动态创建文件夹.
/// </summary>
/// <returns>The folder.</returns>
/// <param name="path">文件创建目录.</param>
/// <param name="FolderName">文件夹名(不带符号).</param>
public static string CreateFolder(string path, string FolderName)
{
string FolderPath = path + FolderName;
if (!Directory.Exists(FolderPath))
{
Directory.CreateDirectory(FolderPath);
}
return FolderPath;
}
/// <summary>
/// 创建文件.
/// </summary>
/// <param name="path">完整文件夹路径.</param>
/// <param name="name">文件的名称.</param>
/// <param name="info">写入的内容.</param>
public static void CreateFile(string path, string name, string info)
{
//文件流信息
StreamWriter sw;
FileInfo t = new FileInfo(path + name);
if (!t.Exists)
{
//如果此文件不存在则创建
sw = t.CreateText();
}
else
{
//如果此文件存在则打开
sw = t.AppendText();
}
//以行的形式写入信息
sw.WriteLine(info);
//关闭流
sw.Close();
//销毁流
sw.Dispose();
}
/// <summary>
/// 校验文件是否存在
/// </summary>
/// <returns>The file.</returns>
/// <param name="path">完整文件夹路径.</param>
/// <param name="name">读取文件的名称.</param>
public static bool FileExists(string path, string filesName)
{
#if UNITY_WEBPLAYER
return false;
#else
return File.Exists(path + "/" + filesName);
#endif
}
/// <summary>
/// 读取文件.
/// </summary>
/// <returns>The file.</returns>
/// <param name="path">完整文件夹路径.</param>
/// <param name="name">读取文件的名称.</param>
public static ArrayList LoadFile(string path, string name)
{
//使用流的形式读取
StreamReader sr = null;
try
{
sr = File.OpenText(path + name);
}
catch (Exception e)
{
Debug.Log(e.Message);
return null;
}
string line;
ArrayList arrlist = new ArrayList();
while ((line = sr.ReadLine()) != null)
{
//一行一行的读取
//将每一行的内容存入数组链表容器中
arrlist.Add(line);
}
//关闭流
sr.Close();
//销毁流
sr.Dispose();
//将数组链表容器返回
return arrlist;
}
}
-------Editor------------------------------------
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
public class OtherTools : Editor {
[MenuItem("OtherTools/检查文件夹下是否有同名文件(不包含后缀)")]
public static void CheckSameAssetName()
{
string path = EditorUtility.OpenFolderPanel("选择检查的文件夹", @"Assets/", "");
checkSameName (path,true);
}
[MenuItem("OtherTools/检查文件夹下是否有同名文件(包含后缀)")]
public static void CheckSameNameFile()
{
string path = EditorUtility.OpenFolderPanel("选择检查的文件夹", @"Assets/", "");
checkSameFilesWithSuffix (path,true);
}
//不包含后缀
public static bool checkSameName(string path,bool isShowFalse)
{
//比如有1.png ,1.tex
Dictionary<string,string> sameDic = new Dictionary<string, string> ();
FileHelper.GetDeepDirAllFilesWithOutSuffix (path,ref sameDic);
if (sameDic.Count > 0) { //有相同名字的资源
string assetNameList = "";
foreach (KeyValuePair<string,string> temp in sameDic) {
int value = temp.Value.IndexOf("Assets");
string lastStr = temp.Value.Substring(value);
assetNameList = assetNameList + "文件名:" +temp.Key +",路径 : " +lastStr+"\n";
}
EditorUtility.DisplayDialog("检查(不包含)后缀文件",assetNameList,"Ok");
return true;
} else {
if(isShowFalse)
EditorUtility.DisplayDialog("检查(不包含)后缀文件","没有相同名字的资源","Ok");
return false;
}
}
//包含后缀
public static bool checkSameFilesWithSuffix(string path,bool isShowFalse)
{
Dictionary<string,string> sameDic = new Dictionary<string, string> ();
Dictionary<string,string> tempDic = new Dictionary<string, string> ();
FileHelper.GetDeepDirAllFilesWithSuffix(path,ref sameDic,ref tempDic);
if (sameDic.Count > 0) { //有相同名字的资源
string assetNameList = "";
foreach (KeyValuePair<string,string> temp in sameDic) {
//Debug.Log("Key : " + temp.Key+",value :" + temp.Value);
int value = temp.Key.IndexOf("Assets");
string lastStr = temp.Key.Substring(value);
assetNameList = assetNameList + "文件名:" +temp.Value +",路径 : " +lastStr+"\n";
}
EditorUtility.DisplayDialog("检查(包含)后缀文件",assetNameList,"Ok");
return true;
} else {
if(isShowFalse)
EditorUtility.DisplayDialog("检查(包含)后缀文件","没有相同名字的资源","Ok");
return false;
}
}
}