• Unity编辑器中递归设置文件夹下资源的 AssetBundle Tag 可多选


    批量设置

    /// <summary>
        /// 批量设置某路径下的资源
        /// </summary>
        [MenuItem("AssetBundle/Set Asset Bundle Name With Selected Folder (设置某个路径的资源可多选递归)")]
        public static void SetSelectFolderBundleName()
        {
            EditorUtility.ClearProgressBar();
            UnityEngine.Object[] selObj = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Unfiltered);
            int total = selObj.Length;
            int curr = 0;
            selectedPicsPathList.Clear();
            foreach (UnityEngine.Object item in selObj)
            {
                string objPath = AssetDatabase.GetAssetPath(item);
                DirectoryInfo dirInfo = new DirectoryInfo(objPath);
                if (dirInfo == null)
                {
                    UnityEngine.Debug.LogError("******请检查,是否选中了非文件夹对象******");
                    return;
                }
                string[] dirs = System.IO.Directory.GetFileSystemEntries(objPath);
                for (int j = 0; j < dirs.Length; j++)
                {
                    string tarPath = dirs[j];
                    if (System.IO.Directory.Exists(tarPath))
                    {
                        //文件夹
                        selectedPicsPathList.AddRange(GetFilesRecursively(tarPath));
                    }
                    if (System.IO.File.Exists(tarPath))
                    {
                        //文件
                        if (tarPath.EndsWith("meta") || string.IsNullOrEmpty(tarPath))
                            continue;
    
                        else 
                        {
                            if (!selectedPicsPathList.Contains(tarPath))
                                selectedPicsPathList.Add(tarPath);
                        }
                    }
                }
            }
    
            
            for (int i = 0; i < selectedPicsPathList.Count; i++)
            {
                DirectoryInfo dirInfo = new DirectoryInfo(selectedPicsPathList[i]);
                if (dirInfo == null)
                {
                    UnityEngine.Debug.LogError("******请检查,是否选中了非文件夹对象******");
                    return;
                }
                _dirName = dirInfo.Name;
                _dirName = dirInfo.Name;
                string filePath = dirInfo.FullName.Replace('\', '/');
                filePath = filePath.Replace(Application.dataPath, "Assets");
                AssetImporter ai = AssetImporter.GetAtPath(filePath);
                curr++;
                UpdateProgress(curr, total, filePath);
                UnityEngine.Debug.Log(filePath);
                string bundle_name;
                bundle_name = filePath.Replace('/', '-');
                ai.assetBundleName = bundle_name.Split('.')[0] + ".assetbundle";
            }
            AssetDatabase.Refresh();
            UnityEngine.Debug.Log("******批量设置AssetBundle名称成功******");
            EditorUtility.ClearProgressBar();
        }

    批量清空 

    /// <summary>  
        /// 批量清空所选文件夹下资源的AssetBundleName.(可递归)  
        /// </summary>  
        [MenuItem("AssetBundle/Reset Asset Bundle Name With Selected Folders(清空所选目录,可多选递归)")]
        public static void ResetSelectedFoldersFileBundleName()
        {
            EditorUtility.ClearProgressBar();
            UnityEngine.Object[] selObj = Selection.GetFiltered(typeof(UnityEngine.Object), SelectionMode.Unfiltered);
            int total = selObj.Length;
            int curr = 0;
            selectedPicsPathList.Clear();
            foreach (UnityEngine.Object item in selObj)
            {
                string objPath = AssetDatabase.GetAssetPath(item);
                DirectoryInfo dirInfo = new DirectoryInfo(objPath);
                if (dirInfo == null)
                {
                    UnityEngine.Debug.LogError("******请检查,是否选中了非文件夹对象******");
                    return;
                }
                string[] dirs = System.IO.Directory.GetFileSystemEntries(objPath);
                for (int j = 0; j < dirs.Length; j++)
                {
                    string tarPath = dirs[j];
    
                    if (System.IO.Directory.Exists(tarPath))
                    {
                        //文件夹
                        selectedPicsPathList.AddRange(GetFilesRecursively(tarPath));
                    }
                    if (System.IO.File.Exists(tarPath))
                    {
                        //文件
                        if (tarPath.EndsWith("meta") || string.IsNullOrEmpty(tarPath))
                            continue;
                        else
                        {
                            if (!selectedPicsPathList.Contains(tarPath))
                                selectedPicsPathList.Add(tarPath);
                        }
                    }
                }
            }
    
            _dirName = null;
            for (int i = 0; i < selectedPicsPathList.Count; i++)
            {
                string tarPath = selectedPicsPathList[i];
                DirectoryInfo dirInfo = new DirectoryInfo(tarPath);
    
                string filePath = dirInfo.FullName.Replace('\', '/');
                filePath = filePath.Replace(Application.dataPath, "Assets");
                UnityEngine.Debug.Log("filePath=" + filePath);
                curr++;
                UpdateProgress(curr, total, filePath);
                AssetImporter ai = AssetImporter.GetAtPath(filePath);
                ai.assetBundleName = _dirName;
            }
    
            AssetDatabase.Refresh();
            UnityEngine.Debug.Log("******批量清除AssetBundle名称成功******");
            EditorUtility.ClearProgressBar();
            selectedPicsPathList.Clear();
        }

    递归方法

    /// <summary>
        /// 递归
        /// </summary>
        /// <param name="folder"></param>
        /// <returns></returns>
        static List<string> GetFilesRecursively(string folder)
        {
            List<string> tar = new List<string>();
            string[] dirs = System.IO.Directory.GetFileSystemEntries(folder);
      
            for(int j = 0;j<dirs.Length;j++)
            { 
                string tarPath = dirs[j];
                if (System.IO.Directory.Exists(tarPath))
                {
                    //文件夹
                    tar.AddRange(GetFilesRecursively(tarPath));
                }
                if (System.IO.File.Exists(tarPath))
                {
                    //文件
                    if (tarPath.EndsWith("meta") || string.IsNullOrEmpty(tarPath))
                        continue;
                    else 
                    {
                        if (!tar.Contains(tarPath))
                            tar.Add(tarPath);
                    }
                }
            }
            return tar;
        }
  • 相关阅读:
    打包压缩文件命令
    用户与组管理命令
    cut 命令 通过列来提取文本字符
    linux文件拼接命令 paste
    shell 指定范围产生随机数
    shell 脚本随机抽取班级学生
    shell 输出九九乘法表
    shell 判断语句
    linux 排序命令sort
    linux之PATH环境变量
  • 原文地址:https://www.cnblogs.com/leesymbol/p/12034998.html
Copyright © 2020-2023  润新知