うにてぃブログ

主にUnityとC#に関する記事を書いていきます

【Unity】アセットのディレクトリを移動する

アセットを移動する際にドラッグして移動するのが面倒だったので、移動先を指定して移動できるように拡張

f:id:hacchi_man:20200411235131p:plain:w300

using UnityEditor;
using UnityEngine;
 
public static class EditorMoveDirectory
{
    [MenuItem("Assets/Move Directory")]
    private static void MoveAssets()
    {
        var guids = Selection.assetGUIDs;
        if (guids.Length <= 0)
            return;
 
        var path = EditorUtility.OpenFolderPanel("Select Move Directory", "Assets", "");
        if (string.IsNullOrEmpty(path))
            return;
 
        var inProjectPath = path.Replace(Application.dataPath, "Assets");
        foreach (var guid in guids)
        {
            var assetPath = AssetDatabase.GUIDToAssetPath(guid);
            var fileName = System.IO.Path.GetFileName(assetPath);
            AssetDatabase.MoveAsset(assetPath, System.IO.Path.Combine(inProjectPath, fileName));
        }
    }
}