以下是一个Unity编辑器扩展脚本,用于根据输入路径自动生成文件夹结构:
对惹,这里有一个游戏开发交流小组,希望大家可以点击进来一起交流一下开发经验呀!
using UnityEditor;
using UnityEngine;
using System.IO;
using System;
public class FolderGeneratorWindow : EditorWindow { private string folderPath = "Assets/"; [MenuItem("Tools/Create Folder Structure")] static void Init() { var window = GetWindow<FolderGeneratorWindow>(); window.titleContent = new GUIContent("Folder Generator"); window.minSize = new Vector2(350, 100); window.Show(); } void OnGUI() { GUILayout.Space(10); GUILayout.Label("Folder Path Creator", EditorStyles.boldLabel); GUILayout.Space(5); EditorGUI.BeginChangeCheck(); folderPath = EditorGUILayout.TextField("Path:", folderPath); GUILayout.Space(15); if (GUILayout.Button("Generate Folders", GUILayout.Height(30))) { if (ValidatePath(folderPath)) { GenerateFolderStructure(folderPath); } } } private bool ValidatePath(string path) { if (!path.StartsWith("Assets/")) { ShowErrorDialog("Invalid path format! Path must start with 'Assets/'"); return false; } if (path.IndexOfAny(Path.GetInvalidPathChars()) != -1) { ShowErrorDialog("Path contains invalid characters!"); return false; } return true; } private void GenerateFolderStructure(string fullPath) { try { string[] folders = fullPath.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries); if (folders.Length < 2) { ShowErrorDialog("Please specify at least one subfolder"); return; } string currentPath = folders[0]; // Should be "Assets" for (int i = 1; i < folders.Length; i++) { string newFolder = folders[i]; string parentPath = currentPath; string newPath = Path.Combine(parentPath, newFolder); if (!AssetDatabase.IsValidFolder(newPath)) { string guid = AssetDatabase.CreateFolder(parentPath, newFolder); if (string.IsNullOrEmpty(guid)) throw new Exception($"Failed to create folder: {newPath}"); Debug.Log($"Created folder: {newPath}"); } currentPath = newPath; } AssetDatabase.Refresh(); EditorUtility.DisplayDialog("Success", "Folder structure created successfully!", "OK"); } catch (Exception e) { ShowErrorDialog($"Error: {e.Message}"); } } private void ShowErrorDialog(string message) { EditorUtility.DisplayDialog("Error", message, "OK"); } }
使用方法:

功能说明:
特性:
注意事项:
对于更复杂的文件管理需求,可以考虑扩展以下功能:
免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删