buildAsset分析(二)——BuildCommon

本文介绍了一个名为BuildCommon的工具类,该类主要用于Unity项目的构建流程。它包含了多个静态方法,如获取文件夹路径、文件名、文件后缀等实用功能,并提供了递归遍历文件夹、检查资源依赖关系等更为复杂的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值