Unity学习笔记--在Editor下实现一个有下拉菜单的文件路径记忆功能(保姆级教程)

前言

最近需要在Unity编辑器下实现一个面板,有一个下拉菜单,其他人可以往这个下拉菜单里面添加一些文件夹路径,保证下次进来的时候能够直接通过这个面板获取到本地已经添加的文件夹路径。

需求分析

  1. 实现一个下拉菜单,能够选取本地已经添加的文件夹路径
  2. 提供一个添加文件夹路径的按钮,点击之后能够选取本地文件夹路径,添加到当前已有的文件夹路径列表中
  3. 提供文件写入功能,能够把当前内存中的文件夹路径写入硬盘,通俗点来说就是需要把变量内容保存到.txt文件中。

知识前提

一、下拉菜单的实现

首先关于下拉菜单的实现,查找Unity手册,发现有一个叫:EditorGUILayout.Popup的方法可以使用

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class SaveFileHistory : EditorWindow
{
    
    
    // 下拉列表的选项
    private string[] options = new string[] {
    
     "Option 1", "Option 2", "Option 3" };

    // 当前选中的选项
    private int selectedOption = 0;

    [MenuItem("Window/My Editor Window")]
    public static void ShowWindow()
    {
    
    
        EditorWindow.GetWindow(typeof(SaveFileHistory));
    }

    void OnGUI()
    {
    
    
        // 显示下拉列表
        selectedOption = EditorGUILayout.Popup("My Dropdown Menu", selectedOption, options);
    }
}

在这里插入图片描述

在这个示例代码中,首先定义了一个字符串数组 options,其中包含了下拉列表中的选项。然后,定义了一个整数变量 selectedOption,用于保存当前选中的选项。在 OnGUI() 方法中,使用 EditorGUILayout.Popup() 方法来显示下拉列表。EditorGUILayout.Popup() 方法的第一个参数是用于显示下拉列表的标签名称,第二个参数是当前选中的选项,第三个参数是下拉列表的选项。

当用户选择了一个新的选项时,selectedOption 变量会被更新,可以在代码中使用它来执行相应的操作。

需要注意的是,这个示例代码是一个继承自 EditorWindow 的自定义编辑器窗口。你可以在 Unity 编辑器中选择 “Window/My Editor Window” 菜单来打开这个窗口。在这个窗口中,会显示一个下拉菜单,用户可以选择一个选项。这个示例演示了如何在自定义编辑器窗口中创建一个简单的下拉菜单,但是它的效果比较简单,可能无法满足复杂的需求。如果需要更复杂的下拉菜单效果,可以考虑使用其他的 Unity 编辑器扩展技术。

二、选取对应文件夹路径

要选取,那么肯定就需要让用户选取本地的路径,我们可以使用EditorUtility.OpenFolderPanel
新建一个脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class OpenFolderPanelTest : EditorWindow
{
    
    
    // 下拉列表的选项
    private string[] options = new string[] {
    
     "Option 1", "Option 2", "Option 3" };

    // 当前选中的选项
    private int selectedOption = 0;

    [MenuItem("Window/OpenFolderPanelTest")]
    public static void ShowWindow()
    {
    
    
        EditorWindow.GetWindow(typeof(OpenFolderPanelTest));
    }

    void OnGUI()
    {
    
    
        GUILayout.BeginHorizontal();

        if (GUILayout.Button("浏览", GUILayout.Width(100f)))
        {
    
    
            string temp_path = EditorUtility.OpenFolderPanel("添加常用文件夹路径",  "Assets/Resources", "");
            if (!temp_path.Equals(""))
            {
    
    
                Debug.Log(temp_path);
            }
            else
            {
    
    
                Debug.LogWarning("未选择文件夹");
            }
        }

        GUILayout.EndHorizontal();
    }
}

在这里插入图片描述

三、写入到.txt文件

这个就没什么好说的了,简单讲讲思路
首先定义一个List,用户点击浏览之后,选中文件夹路径,那么List.Add路径就好了
然后利用File IO保存到.txt文件中就好了

完整代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class SaveFileHistory : EditorWindow
{
    
    
    private const string history_content_file_path = "Assets/Resources/save_file_history.txt";
    private string target_folder_path = "";
    private int selected_folder_index = 0;
    private string[] history_file_path_array = null;

    [MenuItem("Window/My Editor Window")]
    public static void ShowWindow()
    {
    
    
        EditorWindow.GetWindow(typeof(SaveFileHistory));
    }

    private void OnGUI()
    {
    
    
        GUILayout.BeginHorizontal();
        {
    
    
            string[] show_history_file_path_array = GetShowHistoryFilePathArray();
            selected_folder_index = EditorGUILayout.Popup("", selected_folder_index, show_history_file_path_array);

            if (history_file_path_array.Length != 0)
            {
    
    
                target_folder_path = history_file_path_array[selected_folder_index];
            }

            if (GUILayout.Button("浏览", GUILayout.Width(100f)))
            {
    
    
                string temp_path = EditorUtility.OpenFolderPanel("添加常用文件夹路径", "", "");
                if (!temp_path.Equals(""))
                {
    
    
                    target_folder_path = temp_path;
                    AppendSavePrefabHistoryPath(target_folder_path);
                    // 更新history_file_path_array
                    history_file_path_array = GetHistoryFilePathArray();
                }
                else
                {
    
    
                    Debug.LogWarning("未选择文件夹");
                }
            }
        }
        GUILayout.EndHorizontal();



        GUILayout.BeginHorizontal();
        {
    
    
            if (GUILayout.Button("打印当前选中的文件夹路径", GUILayout.Width(200f)))
            {
    
    
                Debug.Log(target_folder_path);
            }
        }
        GUILayout.EndHorizontal();


    }

    private void AppendSavePrefabHistoryPath(string append_history_file_path)
    {
    
    
        history_file_path_array = GetHistoryFilePathArray();
        List<string> history_file_path_list = new List<string>();

        //当前选中的放到第一个
        history_file_path_list.Add(append_history_file_path);
        //原来的文件路径放后
        foreach (string history_file_path in history_file_path_array)
        {
    
    
            //重复的不添加
            if (!history_file_path.Equals(append_history_file_path))
            {
    
    
                history_file_path_list.Add(history_file_path);
            }
        }

        //先清空
        File.WriteAllText(history_content_file_path, "");
        //再添加
        File.AppendAllLines(history_content_file_path, history_file_path_list);
    }

    /// <summary>
    /// `EditorGUILayout.Popup()` 方法默认使用空格作为下拉菜单中选项之间的分隔符。
    /// 这意味着,如果你将文件路径作为选项添加到下拉菜单中,路径中的斜杠 `/` 将被解释为分隔符,从而导致菜单显示异常。
    /// 为了解决这个问题,你可以将文件路径中的斜杠 `/` 替换为其他字符,比如 `-` 或者 `|`,然后在菜单的回调函数中将这些字符再替换回斜杠 `/`。
    /// </summary>
    /// <returns></returns>
    private string[] GetShowHistoryFilePathArray()
    {
    
    
        history_file_path_array = GetHistoryFilePathArray();
        string[] show_history_file_path_array = new string[history_file_path_array.Length];

        System.Array.Copy(history_file_path_array, show_history_file_path_array, history_file_path_array.Length);

        for (int i = 0; i < show_history_file_path_array.Length; i++)
        {
    
    
            show_history_file_path_array[i] = show_history_file_path_array[i].Replace('/', '\\');
        }
        return show_history_file_path_array;
    }

    private string[] GetHistoryFilePathArray()
    {
    
    
        if (!File.Exists(history_content_file_path))
        {
    
    
            File.WriteAllText(history_content_file_path, "");
        }
        return File.ReadAllLines(history_content_file_path);
    }
}

在这里插入图片描述
验证文件记忆功能,第二次进入,还能显示之前选中的文件夹路径
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_52855744/article/details/129177145
今日推荐