在有的时候会有需求去批量修改文件名,下面上代码
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
}
}