FM/Assets/Scripts/Editor/GenAssetEditor.cs

73 lines
2.4 KiB
C#
Raw Normal View History

2025-04-26 21:05:13 +08:00
using System;
using System.IO;
using System.Text;
using Sirenix.OdinInspector;
using UnityEditor;
using UnityEngine;
namespace HK.Editor
{
2025-07-10 23:16:27 +08:00
public class GenAssetEditor : UnityEditor.Editor
2025-04-26 21:05:13 +08:00
{
2025-07-02 10:24:01 +08:00
static string csName = "AssetConst";
static string folderPath = "Assets/Scripts/Runtime/Generate";
static string resPath = "Assets/Res";
static string template = "Assets/Resources/Template/AssetsConstTemplate.txt";
2025-04-26 21:05:13 +08:00
2025-07-02 10:24:01 +08:00
[MenuItem("Tool/GenAssetsConst")]
static void Gen()
2025-04-26 21:05:13 +08:00
{
string path = $"{folderPath}/{csName}.cs";
StringBuilder sb = new StringBuilder();
Bridging(ref sb, resPath);
var readAllText = File.ReadAllText(template);
2025-07-02 10:24:01 +08:00
var content = readAllText.Replace("#CONSTCONTENT#", sb.ToString()).Replace("\\", "/")
.Replace("#CLASSNAME#", csName);
2025-04-26 21:05:13 +08:00
if (!File.Exists(path))
{
File.Create(path).Dispose();
}
File.WriteAllText(path, content);
2025-07-02 10:24:01 +08:00
2025-04-26 21:05:13 +08:00
Debug.Log("生成完成!");
}
static void Bridging(ref StringBuilder sb, string path)
{
DirectoryInfo info = new DirectoryInfo(path);
var fileInfos = info.GetFiles();
var directoryInfos = info.GetDirectories();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.Name.Contains(".meta"))
continue;
string filePath = fileInfo.FullName.Substring(Application.dataPath.Length - 6);
var name = filePath.Replace("\\", "_")
.Replace(".", "_")
2025-07-02 10:24:01 +08:00
.Replace("-", "_")
2025-08-20 11:14:21 +08:00
.Replace(" ", "")
.Replace("(", "")
.Replace(")", "")
.Replace("", "")
.Replace("", "")
.Replace("{", "")
.Replace("}", "")
.Replace("【", "")
.Replace("】", "")
.Replace("[", "")
.Replace("]", "")
2025-04-26 21:05:13 +08:00
.Replace("+", String.Empty);
sb.AppendLine($"public const string {name} = @\"{filePath}\";");
}
foreach (var directoryInfo in directoryInfos)
{
Bridging(ref sb, directoryInfo.FullName);
}
}
}
}