Unity Editor在多个不同文件夹下创建相同的子文件夹

如题,批量操作减少重复的操作。

public class MyTools : EditorWindow
{
    static Object[] objs;
    static MyTools window;
    static bool isCreateFolder = false;
    static string folderName;
 
    [MenuItem("Assets/批量创建子文件夹")]
    public static void CreateFolder()
    {
        window = (MyTools)GetWindow(typeof(MyTools));
        window.titleContent.text = "批量创建子文件夹";
        window.position = new Rect(PlayerSettings.defaultScreenWidth / 2, PlayerSettings.defaultScreenHeight / 2, 400, 120);
        window.Show();
        isCreateFolder = true;
    }
 
    private void OnGUI()
    {
        if (isCreateFolder)
        {
            GUILayout.BeginVertical();
            GUILayout.BeginHorizontal();
            GUILayout.Label("文件夹名称:", GUILayout.MinWidth(15));
            folderName = EditorGUILayout.TextField(folderName, GUILayout.ExpandWidth(true));
            GUILayout.EndHorizontal();
 
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("确认"))
            {
                if (!string.IsNullOrEmpty(folderName))
                    CreateAssignFolder();
                else
                    Debug.LogError("请输入文件夹名称!");
            }
            GUILayout.Space(20);
            if (GUILayout.Button("取消"))
            {
                isCreateFolder = false;
                window.Close();
            }
            GUILayout.EndHorizontal();
            GUILayout.EndVertical();
        }
    }
 
    private void CreateAssignFolder()
    {
        Object[] arr = Selection.GetFiltered(typeof(Object), SelectionMode.TopLevel);
        string rootPath = "";
        StringBuilder subPath;
        for (int i = 0; i < arr.Length; i++)
        {
            subPath = new StringBuilder();
            rootPath = Application.dataPath.Substring(0, Application.dataPath.LastIndexOf('/')) + "/" + AssetDatabase.GetAssetPath(arr[i]);
            subPath.Append(rootPath).Append("/").Append(folderName).Append("/");
            if (!Directory.Exists(subPath.ToString()))
            {
                Directory.CreateDirectory(subPath.ToString());
            }
            else
            {
                Debug.LogError(subPath.ToString() + " exist!");
            }
        }
        AssetDatabase.Refresh();
        isCreateFolder = false;
        window.Close();
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值