Unity 批量修改資源文件名

在有的時候會有需求去批量修改文件名,下面上代碼

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
/// <summary>
/// 批量修改文件名
/// </summary>
public class ChangeFileName : MonoBehaviour
{
    /// <summary>
    /// _w 單一的快捷鍵 W
    /// #w shift + w
    /// %w ctrl + w
    /// &w alt + w
    /// <summary>
    [MenuItem("插件/批量修改文件名")]
    public static void SetFilesName()
    {
#if UNITY_EDITOR
        //根據需求自己去修改需要修改的資源目錄
        string path = Application.dataPath + "/Resources/SpriteNew/Animal";
        // 判斷給定的目錄是否存在  
        if (!Directory.Exists(path))
        {
            Debug.Log("目錄不存在: " + path);
            return;
        }
        // 返回當前按下目錄下的文件列表  
        DirectoryInfo di = new DirectoryInfo(path);
        var files = di.GetFiles();
        var count = 0;
        // 遍歷這個目錄  
        foreach (var f in files)
        {
            //繞過mate文件
            if (f.Extension == ".meta")
            {
                f.Delete();
            }
            else
            {
                //"animal_" + count++.ToString()就是 將要修改成的文件名 animal_xx 同理根據需求自己改
                f.MoveTo(Path.Combine(path, "animal_" + count++.ToString() + f.Extension));
            }
        }
        Debug.Log("成功修改");
#endif        
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章