unity C#でのpath取得関連メモ

project viewで選択中のディレクトリ取得

    static private string GetSelectedDir()
    {
        foreach (Object obj in Selection.GetFiltered(typeof(DefaultAsset), SelectionMode.TopLevel))
        {
            if (obj is DefaultAsset)
            {
                string path = AssetDatabase.GetAssetPath(obj);

                if (AssetDatabase.IsValidFolder(path))
                {
                    return path;
                }
            }
        }

        return "";
    }

カレントディレクトリの取得。unity editorで実行すると、Projectが入ってるディレクトリの絶対パスが入っているようだ。

System.Environment.CurrentDirectory
var a = GetSelectedDir(); // Assets/xxx/xxx みたいな Assetsからの相対パス

var b = System.IO.Path.GetFullPath(a); // 絶対パス変換

 

ディレクトリを再帰的にコピー

https://docs.microsoft.com/ja-jp/dotnet/standard/io/how-to-copy-directories