Unity 创建文件夹怎么能像他自带的创建文件夹那样可以自己填写名字

正常咱们官网查到的Unity API
public static string CreateFolder (string parentFolder, string newFolderName);
但是这样的话就只能创建固定的命名了。
所以我查了一下官网的Create Folder是怎么实现的

// Create a folder
        [ShortcutManagement.ShortcutAttribute("Project Browser/Create/Folder", typeof(ProjectBrowser), KeyCode.N, ShortcutManagement.ShortcutModifiers.Shift | ShortcutManagement.ShortcutModifiers.Action)]
        public static void CreateFolder()
        {
    
    
            StartNameEditingIfProjectWindowExists(0, ScriptableObject.CreateInstance<DoCreateFolder>(), "New Folder", EditorGUIUtility.IconContent(EditorResources.emptyFolderIconName).image as Texture2D, null);
        }

附上地址:UnityCsReference/ProjectWindowUtil.cs at master · Unity-Technologies/UnityCsReference (github.com)

她是用了一个StartNameEditingIfProjectWindowExists的方法

我查了一下这个方法
public static void StartNameEditingIfProjectWindowExists(int instanceID, EndNameEditAction endAction, string pathName, Texture2D icon, string resourceFile)
附上地址:Class ProjectWindowUtil (stephenhodgson.github.io)

EndNameEditAction有一个Action,官网是这么写的

internal class DoCreateFolder : EndNameEditAction
        {
    
    
            public override void Action(int instanceId, string pathName, string resourceFile)
            {
    
    
                string guid = AssetDatabase.CreateFolder(Path.GetDirectoryName(pathName), Path.GetFileName(pathName));
                Object o = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(Object));
                ProjectWindowUtil.ShowCreatedAsset(o);
            }
        }

因为我只需要在最外层命名,里面的结构都是写死的,所以我重写了这个方法来实现我需要再里面嵌套其它文件夹。

using System.IO;
using UnityEditor;
using UnityEditor.Experimental;
using UnityEditor.ProjectWindowCallback;
using UnityEngine;

public class CreateArtResFolder : Editor
{
    
    
    [MenuItem("Assets/Create/Folder.ArtRes", priority = 15)]
    static void Init()
    {
    
    
        CreateArtFolder();
        Debug.Log("已创建文件夹");
    }

    static void CreateArtFolder()
    {
    
    
        ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, CreateInstance<DoCreateFolder>(), "New Folder", EditorGUIUtility.IconContent(EditorResources.folderIconName).image as Texture2D, null);
    }
}

internal class DoCreateFolder : EndNameEditAction
{
    
    
    public override void Action(int instanceId, string pathName, string resourceFile)
    {
    
    
        string guid = AssetDatabase.CreateFolder(Path.GetDirectoryName(pathName), Path.GetFileName(pathName));
        AssetDatabase.CreateFolder(pathName, "Materials");
        Object o = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(Object));
        ProjectWindowUtil.ShowCreatedAsset(o);
    }
}

在底下DoCreateFolder类里面写内层的文件夹结构
增加一个AssetDatabase.CreateFolder(pathName, "Materials");
pathName可以直接拿到的当前创建好后的文件夹完整内部路径,所以可以按照这个思路往里面添加其它文件夹。

附上 Unity 官方源码地址:Unity-Technologies/UnityCsReference: Unity C# reference source code. (github.com)

猜你喜欢

转载自blog.csdn.net/u012685176/article/details/126721189