using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using UnityEditor;
using UnityEngine;
public class ScriptsGenerateMD : EditorWindow
{
static StringBuilder sb = new StringBuilder();
//层级序号
static int first = 0;
[MenuItem("Tools/DoGenerate")]
static void WindowInit()
{
//创建窗口
Rect myrect = new Rect(0, 0, 300, 350);
ScriptsGenerateMD window = (ScriptsGenerateMD)EditorWindow.GetWindowWithRect(typeof(ScriptsGenerateMD), myrect, true, "选择文件夹生成ReadMe文件");
window.Show();
}
private void OnGUI()
{
EditorGUI.LabelField(new Rect(5, 10, 180, 30), "作用:", EditorStyles.boldLabel);
EditorGUI.LabelField(new Rect(25, 35, 275, 30), "可以遍历文件夹名称,生成ReadMe.md文件");
EditorGUI.LabelField(new Rect(25, 75, 275, 30), "请选择Assets文件夹(可多选)");
if (GUI.Button(new Rect(50,150,200,35),"选中文件夹进行生成"))
{
Object[] selectedAssets = Selection.GetFiltered<Object>(SelectionMode.Assets);
foreach (Object asset in selectedAssets)
{
string assetPath = AssetDatabase.GetAssetPath(asset);
bool isFolder = AssetDatabase.IsValidFolder(assetPath);
if (isFolder)
{
// 处理资产路径
ReadAssets(assetPath);
}
else
{
Debug.LogError("请选择文件夹目录");
}
}
}
}
private void OnInspectorUpdate()
{
this.Repaint();
}
static void ReadAssets(string assetPath)
{
Debug.Log("目录:" + assetPath);
sb.Clear();
// 添加标题
sb.Append("# 目录说明文档\n\n");
// 添加列表
sb.Append("## 目录信息\n");
sb.Append("```\n");
//取地址的最后一个名称作为头名称
string[] resultSplit = assetPath.Split('/');
sb.Append("├" + resultSplit[resultSplit.Length - 1] + "\n");
// 从根目录开始遍历
ReadFolder(assetPath, first);
sb.Append("```\n");
string userPath = assetPath.Replace("Assets","");
// 路径拼写
string path = Path.Combine(Application.dataPath + userPath, "ReadMe.md");
//Debug.Log(path);
// 保存为.md文件
System.IO.File.WriteAllText(path, sb.ToString());
AssetDatabase.Refresh();
}
static void ReadFolder(string folderPath,int index)
{
index++;
// 获取当前文件夹下的所有子文件夹
string[] subfolders = AssetDatabase.GetSubFolders(folderPath);
// 遍历子文件夹
foreach (string subfolder in subfolders)
{
//Debug.Log( $"Folder: {subfolder} ;层级: {index}");
string combin = "";
for (global::System.Int32 i = first; i < index; i++)
{
if(i> first)
combin += "│ ";
else
combin += " ";
}
combin += "├";
//上个文件夹的替换
string newSubFloder = subfolder.Replace(folderPath + "/", "");
combin += newSubFloder + "--(此处填写说明信息)";
sb.Append(combin + "\n");
ReadFolder(subfolder, index); // 递归处理子文件夹
}
}
}