LoginSignup
2
1

More than 5 years have passed since last update.

【Unityエディター拡張】 ワンクリックでファイルを1つ上のディレクトリへ移動させる

Posted at

ファイルを1つ上のディレクトリへ移動させるのがめんどくさかったので作りました。

ファイル選択 → 右クリック → 「親ディレクトリへ移動」を選択
image

1つ上のディレクトリへファイルが移動します。
image

ソースコード

AssetHelper.cs
using UnityEngine;
using UnityEditor;

public class AssetHelper
{
    [MenuItem("Assets/親ディレクトリへ移動", true)]
    static public bool ChkSelectAsset()
    {
        //ファイルパス
        string path = AssetDatabase.GetAssetPath(Selection.activeObject);

        //ファイル拡張子
        string extension = System.IO.Path.GetExtension(path);

        return extension != "";
    }

    [MenuItem("Assets/親ディレクトリへ移動")]
    static public void MoveAssetToParent()
    {
        Object obj = Selection.activeObject;

        //ファイルパス
        string path = AssetDatabase.GetAssetPath(obj);

        //移動先ディレクトリ
        string dir = GetParentDirectory(path, 2);

        MoveAssetsToDirectory(Selection.objects, dir);
    }
    static string GetParentDirectory(string filepath, int n = 1)
    {
        string dir = filepath;
        for (int i = 0; i < n; i++)
        {
            dir = System.IO.Directory.GetParent(dir).FullName;
        }
        return ConvertSystemPathToUnityPath(dir);
    }

    public static string ConvertSystemPathToUnityPath(string path)
    {
        int index = path.IndexOf("Assets");
        if (index > 0)
        {
            path = path.Remove(0, index);
        }
        return path;
    }

    static private void MoveAssetsToDirectory(Object[] objects, string dir)
    {
        foreach (Object o in Selection.objects)
        {
            string path = AssetDatabase.GetAssetPath(o);
            string extension = System.IO.Path.GetExtension(path);
            AssetDatabase.MoveAsset(path, dir + "/" + o.name + extension);
        }
    }
}
2
1
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1