buildAsset分析(二)——BuildCommon

来源:互联网 发布:大数据营销医药公司 编辑:程序博客网 时间:2024/05/17 08:20
BuildCommon.cs这个文件主要是用于build的工具类,里面放着要用到的工具函数。
using System;using System.IO;using UnityEngine;using UnityEditor;using System.Collections;using System.Collections.Generic;using Object = UnityEngine.Object;    public class BuildCommon    {        //我测试过,实际上这个函数是get了文件的不包含目录的文件名,        //而不是folder...          //但是我决定还是不修改它        public static string getFolder(string path)        {            path = path.Replace("\\", "/");            int index = path.LastIndexOf("/");            if (-1 == index)                throw new Exception("can not find /!!!");            return path.Substring(index + 1, path.Length - index - 1);        }        //get文件的文件名(包括目录,不包括后缀)        public static string getFileName(string fileName)        {            int index = fileName.IndexOf(".");            if (-1 == index)                throw new Exception("can not find .!!!");            return fileName.Substring(0, index);        }        //suffix值决定返回的文件名是否包括后缀(包括目录)        public static string getFileName(string filePath,bool suffix)        {            if (!suffix)            {                string path = filePath.Replace("\\", "/");                int index = path.LastIndexOf("/");                if (-1 == index)                    throw new Exception("can not find .!!!");                int index2 = path.LastIndexOf(".");                if (-1 == index2)                    throw new Exception("can not find /!!!");                return path.Substring(index + 1, index2 - index - 1);            }            else            {                //这里代码实际上就和getFolder一模一样                string path = filePath.Replace("\\", "/");                int index = path.LastIndexOf("/");                if (-1 == index)                    throw new Exception("can not find /!!!");                return path.Substring(index + 1, path.Length - index - 1);            }        }        //获取文件的后缀名        public static string getFileSuffix(string filePath)        {            int index = filePath.LastIndexOf(".");            if (-1 == index)                throw new Exception("can not find Suffix!!! the filePath is : " + filePath);            return filePath.Substring(index + 1, filePath.Length - index - 1);        }        //递归目录下的所有文件(除svn,meta,dll文件外),加入到list(里面有AssetUnit)中去        public static void getFiles(string path, ref Dictionary<string, AssetUnit> list)        {            string[] dirs = Directory.GetDirectories(path);            foreach (string dir in dirs)            {                if (getFolder(dir) == ".svn")                    continue;                getFiles(dir, ref list);            }            string[] files = Directory.GetFiles(path);            foreach (string file in files)            {                string suffix = getFileSuffix(file);                if (suffix == "meta" || suffix == "dll")                    continue;                string realFile = file.Replace("\\", "/");                realFile = "Assets" + realFile.Replace(Application.dataPath,"");                list.Add(realFile, new AssetUnit(realFile));            }        }        //递归目录下的所有文件(除svn,meta,dll文件外),加入到list中去        public static void GetFiles(string path, List<string> list, bool recursion)        {            if (recursion)            {                string[] dirs = Directory.GetDirectories(path);                foreach (string dir in dirs)                {                    if (getFolder(dir) == ".svn")                        continue;                    GetFiles(dir, list, recursion);                }            }            string[] files = Directory.GetFiles(path);            foreach (string file in files)            {                string suffix = getFileSuffix(file);                if (suffix == "meta")                    continue;                string realFile = file.Replace("\\", "/");                realFile = realFile.Replace(Application.streamingAssetsPath + "/", "");                list.Add(realFile);            }        }        //检查path下是否有speicalName文件。有的话就加入allPath里面。recursion必须为true才行        public static void getFloder(string path, string specialName, bool recursion, List<string> allPath)        {            if (recursion)            {                string[] dirs = Directory.GetDirectories(path);                foreach (string dir in dirs)                {                    if (getFolder(dir) == specialName)                        allPath.Add(dir);                    getFloder(dir, specialName, recursion, allPath);                }            }        }    //这里所谓的AssetLevel其实就是指资源的依赖数量    //有1个依赖,则level为1,    //有两个依赖,则level为2.        public static int getAssetLevel(string filePath)        {            string[] depencys = AssetDatabase.GetDependencies(new string[] { filePath });            List<string> deps = new List<string>();            foreach (string file in depencys)            {                //排除关联脚本                string suffix = BuildCommon.getFileSuffix(file);                //if (suffix == "dll" || suffix == "cs")                if (suffix == "dll")                    continue;                deps.Add(file);            }            if (deps.Count == 1)                return 1;            int maxLevel = 0;            foreach (string file in deps)            {                if (file == filePath)                    continue;                int level = getAssetLevel(file);                maxLevel = maxLevel > level + 1 ? maxLevel : level + 1;            }            return maxLevel;        }        //检查目录,没有就创建        public static void CheckFolder(string path)        {            if (!Directory.Exists(path))                Directory.CreateDirectory(path);        }        //获取文件的path,把文件名剔除掉以后的。        public static string getPath(string filePath)        {            string path = filePath.Replace("\\", "/");            int index = path.LastIndexOf("/");            if (-1 == index)                throw new Exception("can not find /!!!");            return path.Substring(0, index);        }        //判断asset是否是sourceAsset的依赖        public static bool isDependenced(string asset,string sourceAsset)        {            string[] deps = AssetDatabase.GetDependencies(new string[] { sourceAsset });            bool isDep = false;            foreach (string path in deps)            {                if (path == sourceAsset)                    continue;                if (path == asset)                    return true;                isDep = isDependenced(asset,path);            }            return isDep;        }        //检查asset是否只有一个直接依赖.        public static bool isSingleDependenced(AssetUnit asset)        {            if (asset.mDirectUpperDependences.Count > 1)                return false;            else if (asset.mDirectUpperDependences.Count == 1)                return isSingleDependenced(asset.mDirectUpperDependences[0]);            else                return true;        }
原创粉丝点击