NLDClient-yudde/ProjectNLD/Assets/Editor/ProjectBuilder/ProjectBuilderWindow.cs

93 lines
3.3 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
namespace ProjectBuilder
{
public class ProjectBuilderWindow : EditorWindow
{
[MenuItem("Tools/文件与路径/项目构建器")]
public static void ShowWindow()
{
var window = GetWindow<ProjectBuilderWindow>();
window.titleContent = new GUIContent("项目构建器");
window._data = new DataCustomProj();
window.minSize = new Vector2(400, 400);
window.Show();
}
private DataCustomProj _data;
private void OnGUI()
{
EditorGUILayout.LabelField("构建项目", EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("项目路径", GUILayout.Width(100));
_data.projPath = EditorGUILayout.TextField(_data.projPath);
if (GUILayout.Button("选择路径", GUILayout.Width(100)))
{
_data.projPath = EditorUtility.OpenFolderPanel("选择项目路径", "", "");
}
EditorGUILayout.EndHorizontal();
if (!string.IsNullOrEmpty(_data.projPath))
{
// 显示最终文件夹名字
_ShowProjFolderInfo();
}
if (GUILayout.Button("构建项目"))
{
_BuildProject();
}
}
private void _ShowProjFolderInfo()
{
// 显示最后三层文件夹的名字
string folderName = _data.projPath;
string[] pathParts = _data.projPath.Split(new char[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
if (pathParts.Length >= 3)
{
folderName = string.Join("/", pathParts[^3], pathParts[^2], pathParts[^1]);
}
else if (pathParts.Length > 0)
{
folderName = string.Join("/", pathParts);
}
EditorGUILayout.LabelField("最后三层文件夹", folderName);
}
private void _BuildProject()
{
// Assets/Code/Scripts/Gameplay
var srcPath = Application.dataPath + "/Code/Scripts/Gameplay";
var targetPath = _data.projPath + "/Gameplay";
// 复制所有cs文件到目标路径, 并保持文件夹结构
if (!System.IO.Directory.Exists(targetPath))
{
System.IO.Directory.CreateDirectory(targetPath);
}
var files = System.IO.Directory.GetFiles(srcPath, "*.cs", System.IO.SearchOption.AllDirectories);
foreach (var file in files)
{
var relativePath = System.IO.Path.GetRelativePath(srcPath, file);
var targetFile = System.IO.Path.Combine(targetPath, relativePath);
var targetDir = System.IO.Path.GetDirectoryName(targetFile);
if (!System.IO.Directory.Exists(targetDir))
{
System.IO.Directory.CreateDirectory(targetDir);
}
System.IO.File.Copy(file, targetFile, true);
}
// 弹出提示框
EditorUtility.DisplayDialog("提示", "项目构建完成!", "确定");
}
}
}