|
正常咱们官网查到的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>(), &#34;New Folder&#34;, EditorGUIUtility.IconContent(EditorResources.emptyFolderIconName).image as Texture2D, null);
}
附上地址:UnityCsReference/ProjectWindowUtil.cs at master · Unity-Technologies/UnityCsReference
她是用了一个StartNameEditingIfProjectWindowExists的方法
我查了一下这个方法
public static void StartNameEditingIfProjectWindowExists(int instanceID, EndNameEditAction endAction, string pathName, Texture2D icon, string resourceFile)
附上地址:Site not found · GitHub Pages)](Class ProjectWindowUtil
他`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(&#34;Assets/Create/Folder.ArtRes&#34;, priority = 15)]
static void Init()
{
CreateArtFolder();
Debug.Log(&#34;已创建文件夹&#34;);
}
static void CreateArtFolder()
{
ProjectWindowUtil.StartNameEditingIfProjectWindowExists(0, CreateInstance<DoCreateFolder>(), &#34;New Folder&#34;, 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, &#34;Materials&#34;);
Object o = AssetDatabase.LoadAssetAtPath(AssetDatabase.GUIDToAssetPath(guid), typeof(Object));
ProjectWindowUtil.ShowCreatedAsset(o);
}
}
在底下DoCreateFolder类里面写内层的文件夹结构
增加一个AssetDatabase.CreateFolder(pathName, &#34;Materials&#34;);
pathName可以直接拿到的当前创建好后的文件夹完整内部路径,所以可以按照这个思路往里面添加其它文件夹。
附上 Unity 官方源码地址:GitHub - Unity-Technologies/UnityCsReference: Unity C# reference source code. |
|