うにてぃブログ

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

【Unity】Unity で使えるフォルダ・ファイル選択パネル

folder には フルパスでもいいし、Assets/ から始まるパスでも良い

EditorUtility.OpenFolderPanel

public static void OpenFolderPanel(string title, string folder, string defaultName);

フォルダを選択するための Windowを開き、選択されたフォルダのフルパスを取得できる
キャンセルを押した場合 string.Empty が取得できる
defaultName は何を入力しても挙動が変わらなかったので、何に利用するのか不明

f:id:hacchi_man:20200116231527p:plain:w400

using System.IO;
using UnityEditor;
using UnityEngine;
 
public class SamplePanelsWindow : EditorWindow
{
    [MenuItem("Sample/Load Scripts To Folder")]
    static void OpenFolderPanelTest()
    {
        var path = EditorUtility.OpenFolderPanel("Load Scripts",  Application.dataPath, string.Empty);
        if (string.IsNullOrEmpty(path))
            return;
        
        foreach (var file in Directory.GetFiles(path))
            if (file.EndsWith(".cs"))
                Debug.Log(file);
    }
}

EditorUtility.OpenFilePanel

public static void OpenFilePanel(string title, string directory, string extension);

ファイルを選択するための Windowを開き、選択されたファイルのフルパスを取得できる
キャンセルを押した場合 string.Empty が取得できる
extension を指定することで特定の拡張子のみ選択可能にできる

f:id:hacchi_man:20200116232859p:plain:w400

using UnityEditor;
using UnityEngine;
 
public class SamplePanelsWindow : EditorWindow
{
    [MenuItem("Sample/Select Asset")]
    static void OpenFilePanel()
    {
        var path = EditorUtility.OpenFilePanel("Select Asset",  Application.dataPath, "cs");
        if (string.IsNullOrEmpty(path))
            return;
        
        Debug.Log(path);
    }
}

EditorUtility.OpenFilePanelWithFilters

public static void OpenFilePanelWithFilters(string title, string directory, string[] filters);

OpenFilePanel に複数のフィルター機能を追加したもので、複数種類の拡張子を指定できる
指定するときは "フィルターの説明", "拡張子"

using UnityEditor;
using UnityEngine;
 
public class SamplePanelsWindow : EditorWindow
{
    [MenuItem("Sample/Select Asset With Filters")]
    static void OpenFilePanelWithFilters()
    {
        var path = EditorUtility.OpenFilePanelWithFilters("Select Asset",  Application.dataPath, new[]{
            "Image files", "png,jpg",
            "Script files", "cs",
        });
        
        if (string.IsNullOrEmpty(path))
            return;
        
        Debug.Log(path);
    }
}

EditorUtility.SaveFolderPanel

public static extern string SaveFolderPanel(string title, string folder, string defaultName);
EditorUtility.OpenFolderPanel と同じ処理をしている

EditorUtility.SaveFilePanel

public static extern string SaveFilePanel(string title, string directory, string defaultName, string extension);

デフォルトファイル名と拡張子を指定して保存パネルを表示する
ファイル名は変更可能だが、拡張子が一致しない場合文末に拡張子を追加してフルパスを取得する

png を指定していて sampleFile.jpg で保存した場合 sampleFile.jpg.png となる

f:id:hacchi_man:20200117001459p:plain:w400

using UnityEditor;
using UnityEngine;

public class SamplePanelsWindow : EditorWindow
{
    [MenuItem("Sample/Save Asset")]
    static void SaveFilePanel()
    {
        var path = EditorUtility.SaveFilePanel("Save Asset", Application.dataPath, "SaveAssetName", "png");
        if (string.IsNullOrEmpty(path))
            return;
        
        var pngData = EditorGUIUtility.whiteTexture.EncodeToPNG();
        if (pngData != null)
            System.IO.File.WriteAllBytes(path, pngData);
    }
}

EditorUtility.SaveFilePanelInProject

public static string SaveFilePanelInProject(string title, string defaultName, string extension, string message)
public static string SaveFilePanelInProject(string title, string defaultName, string extension, string message, string path)

SaveFilePanel とは異なり Assets/ から始まるパスを取得できる
またmessageを上部に表示できる

f:id:hacchi_man:20200117003704p:plain:w400

using UnityEditor;
using UnityEngine;

public class SamplePanelsWindow : EditorWindow
{
    [MenuItem("Sample/Save Asset in Project")]
    static void SaveFilePanel()
    {
        var path = EditorUtility.SaveFilePanelInProject(
            "Save Asset",
            "SaveAssetName",
            "png",
            "Please enter a file name to save the texture to");
        
        if (string.IsNullOrEmpty(path))
            return;
        
        var pngData = EditorGUIUtility.whiteTexture.EncodeToPNG();
        if (pngData != null)
            System.IO.File.WriteAllBytes(path, pngData);
    }
}

参考リンク

EditorUtility-OpenFolderPanel - Unity スクリプトリファレンス
EditorUtility-OpenFilePanel - Unity スクリプトリファレンス
EditorUtility-OpenFilePanelWithFilters - Unity スクリプトリファレンス
EditorUtility-SaveFilePanel - Unity スクリプトリファレンス
EditorUtility-SaveFolderPanel - Unity スクリプトリファレンス
EditorUtility-SaveFilePanelInProject - Unity スクリプトリファレンス