Unity编辑器中递归设置文件夹下图片AssetBundle Tag 可多选

批量设置

    static List<string> selectedPicsPathList = new List<string>();
    /// <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];
                selectedPicsPathList.AddRange(GetFilesRecursively(tarPath));
                //UnityEngine.Debug.Log("******dir ****** " + dirs[j]);
                if (tarPath.EndsWith("meta"))
                    continue;
                else if (tarPath.EndsWith("png") || tarPath.EndsWith("jpg"))
                {
                    if (!selectedPicsPathList.Contains(tarPath))
                        selectedPicsPathList.Add(tarPath);
                }
                else
                {
                    selectedPicsPathList.AddRange(GetFilesRecursively(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];
                //UnityEngine.Debug.Log("******dir ****** " + dirs[j]);
                if (tarPath.EndsWith("meta"))
                    continue;
                else if (tarPath.EndsWith("png") || tarPath.EndsWith("jpg"))
                {
                    if (!selectedPicsPathList.Contains(tarPath))
                        selectedPicsPathList.Add(tarPath);
                }
                else
                {
                    selectedPicsPathList.AddRange(GetFilesRecursively(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 (tarPath.EndsWith("meta") || string.IsNullOrEmpty(tarPath))
                continue;
            else if (tarPath.EndsWith("png") || tarPath.EndsWith("jpg"))
            {
                if (!tar.Contains(tarPath))
                    tar.Add(tarPath);
            }
            else
            {
                tar.AddRange(GetFilesRecursively(tarPath));
            }
        }
        return tar;
    }

猜你喜欢

转载自www.cnblogs.com/leesymbol/p/12034998.html
tag
今日推荐