项目记录08-- 文件检查(打包失败)

今天打包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 Pathref Dictionary<string,stringoutDicstring flag = nullbool 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 == truename = name.Split('.')[0];
                        outDic.Add(namefo.FullName);
                    }
                }
                else
                {
                    if (cancleSuffix == truename = name.Split('.')[0];
                    outDic.Add(namefo.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 Pathref Dictionary<string,stringoutDicstring flag = nullbool cancleSuffix = true)
    {
        DirectoryInfo folder = new DirectoryInfo (Path);
        FileSystemInfo[] files = folder.GetFileSystemInfos ();
        int length = files.Length;
        for (int i = 0i < lengthi++) {
            if(files[iis 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 Pathref Dictionary<string,stringoutDic,ref Dictionary<string,stringtempDic)
    {

        DirectoryInfo folder = new DirectoryInfo (Path);
        FileSystemInfo[] files = folder.GetFileSystemInfos ();
        int length = files.Length;
        for (int i = 0i < lengthi++) {
            if(files[iis 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,stringsameDic)
    {
        Dictionary<string,stringoutDic = new Dictionary<string,string > (); 
        Dictionary<string,stringnameDic = new Dictionary<string,string > (); 
        GetDeepDirAllFilesWithSuffix (Pathref outDicnullfalse);
        foreach (KeyValuePair<string,stringtemp 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 pathstring 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 pathstring namestring 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 pathstring 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 pathstring 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,stringsameDic = new Dictionary<stringstring> ();
        FileHelper.GetDeepDirAllFilesWithOutSuffix (path,ref sameDic);
        if (sameDic.Count > 0) { //有相同名字的资源
            string assetNameList = "";
            foreach (KeyValuePair<string,stringtemp 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,stringsameDic = new Dictionary<stringstring> ();
        Dictionary<string,stringtempDic = new Dictionary<stringstring> ();
        FileHelper.GetDeepDirAllFilesWithSuffix(path,ref sameDic,ref tempDic);
        if (sameDic.Count > 0) { //有相同名字的资源
            string assetNameList = "";
            foreach (KeyValuePair<string,stringtemp 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;
        }
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值