Unity Editor【Open File Panel】- Open the file selection window and record the file path

As shown in the figure, write such a function in the Unity Editor editor environment: click the "Browse" button to open a window, select the file and record the path of the folder: 

API used: OpenFilePanel method in EditorUtility class:

// 摘要:
//     Displays the "open file" dialog and returns the selected path name.
// 参数:
//   title:
//
//   directory:
//
//   extension:
public static string OpenFilePanel(string title, string directory, string extension);

The first parameter title: indicates the title of the opened window:

The second parameter directory: the default folder path to open the window, for example, if Application.dataPath is passed in, the path when the window is opened is the Assets folder of the project project:

The third parameter extension: the suffix of the file, that is, the type of file we want to select, for example, to select a .png type image:

Test code:

using UnityEngine;
using UnityEditor;

public class Example : EditorWindow
{
    [MenuItem("SKFramework/Example")]
    private static void Open()
    {
        GetWindow<Example>().Show();
    }

    private string path;

    private void OnGUI()
    {
        //水平布局
        GUILayout.BeginHorizontal();
        {
            GUILayout.Label("路径", GUILayout.Width(50f));
            path = GUILayout.TextField(path);
            if (GUILayout.Button("浏览", GUILayout.Width(50f)))
            {
                path = EditorUtility.OpenFilePanel("窗口标题", Application.dataPath, "png");
            }
        }
        GUILayout.EndHorizontal();
    }
}

   Welcome to the public account "Contemporary Wild Programmer"

Guess you like

Origin blog.csdn.net/qq_42139931/article/details/123206376