Skip to content

Instantly share code, notes, and snippets.

@n0mimono
Last active November 12, 2019 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n0mimono/0522ef2146daf9a9e5e40d6c1569f281 to your computer and use it in GitHub Desktop.
Save n0mimono/0522ef2146daf9a9e5e40d6c1569f281 to your computer and use it in GitHub Desktop.
ゲーム開発中とかによくあるファイル名が連番としてアセットをいい感じに一括リネームしたいときに役立つやつ
using System;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class NumberingAssetRenamer : ScriptableObject
{
[Serializable]
public class Bundle
{
public string path;
public string origin;
public string next;
}
[SerializeField]
List<Bundle> bundles;
[ContextMenu("Rename")]
public void Rename()
{
bundles.ForEach(b => RenameBundle(b));
}
void RenameBundle(Bundle bundle)
{
#if UNITY_EDITOR
// search paths
var rootPath = Path.Combine(Application.dataPath, bundle.path);
var childs = GetFilesAndDirsRecursively(rootPath);
// all paths
var paths = new List<string>()
{
rootPath,
rootPath + ".meta",
};
paths.AddRange(childs);
Func<string, string> getNextPath = p =>
{
var file = Path.GetFileName(p);
var dir = Path.GetDirectoryName(p);
var fileNext = file.Replace(bundle.origin, bundle.next);
return Path.Combine(dir, fileNext);
};
// rename files
foreach (var path in paths)
{
var nextPath = getNextPath(path);
if (File.Exists(path))
{
Debug.Log("move file: " + path + " -> " + nextPath);
File.Move(path, nextPath);
}
}
// rename directories
foreach (var path in paths)
{
var nextPath = getNextPath(path);
if (Directory.Exists(path))
{
Debug.Log("move dir: " + path + " -> " + nextPath);
Directory.Move(path, nextPath);
}
}
// refresh dirty
AssetDatabase.Refresh();
#endif
}
static List<string> GetFilesAndDirsRecursively(string path)
{
var files = Directory.GetFiles(path);
var dirs = Directory.GetDirectories(path);
var filesAndDirs = files.Union(dirs).ToList();
foreach (var dir in Directory.GetDirectories(path))
{
var subs = GetFilesAndDirsRecursively(dir);
filesAndDirs.AddRange(subs);
}
return filesAndDirs;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment