IndieGame/client/Assets/Ether/Scripts/Module/UI/Editor/FrameCodeGenerate.cs

173 lines
6.0 KiB
C#
Raw Normal View History

2024-10-11 10:12:15 +08:00
/********************************************************************
: FrameCodeGenerate.cs
:
: 1982614048@qq.com
: 2024/03/29 17:28:19
:
: 2024/04/04 16:34:30
:
*********************************************************************/
using System;
using System.IO;
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
using System.Linq;
2024-10-24 23:44:04 +08:00
using UnityEditor.PackageManager.UI;
2024-10-11 10:12:15 +08:00
namespace Ether
{
public class FrameCodeGenerate
{
2024-10-28 03:42:42 +08:00
private static string templeteBaseFileName = Application.dataPath + $"/Ether/Scripts/Module/UI/Editor/FrameBaseTemplete.txt";
2024-10-11 10:12:15 +08:00
2024-10-24 23:44:04 +08:00
public static string GenerateFrameBaseCode(string prefabPath, string baseDirectoryPath, Dictionary<string, string> componentTypeDic)
2024-10-11 10:12:15 +08:00
{
2024-10-24 23:44:04 +08:00
// 加载预制体
GameObject prefab = AssetDatabase.LoadAssetAtPath<GameObject>(prefabPath);
2024-10-11 10:12:15 +08:00
2024-10-24 23:44:04 +08:00
if (prefab && PrefabUtility.IsPartOfAnyPrefab(prefab))
2024-10-11 10:12:15 +08:00
{
2024-10-24 23:44:04 +08:00
Transform[] childs = prefab.GetComponentsInChildren<Transform>(true);
2024-10-11 10:12:15 +08:00
2024-10-24 23:44:04 +08:00
// 子物体路径 子物体名称 组件列表
Dictionary<string, (string, List<string>)> componentDic = new Dictionary<string, (string, List<string>)>();
2024-10-11 10:12:15 +08:00
foreach (Transform child in childs)
{
2024-10-24 23:44:04 +08:00
if (child.name == prefab.name)
2024-10-11 10:12:15 +08:00
{
continue;
}
2024-10-24 23:44:04 +08:00
(string, List<string>) childComponent = CheckClildName(child, componentTypeDic);
2024-10-11 10:12:15 +08:00
string childPath = GetChildPath(child);
if (!componentDic.ContainsKey(childPath))
{
componentDic.Add(childPath, childComponent);
}
2024-10-24 23:44:04 +08:00
}
GenerateFrameBase(prefabPath, baseDirectoryPath, componentTypeDic, componentDic);
2024-10-11 10:12:15 +08:00
}
2024-10-24 23:44:04 +08:00
return null;
2024-10-11 10:12:15 +08:00
}
/// <summary>
/// 检查子物体的名字,获取对应的组件
/// </summary>
/// <param name="child"></param>
/// <returns></returns>
2024-10-24 23:44:04 +08:00
private static (string, List<string>) CheckClildName(Transform child, Dictionary<string, string> componentTypeDic)
2024-10-11 10:12:15 +08:00
{
string input = child.name;
string pattern = @"<(.*?)>";
2024-10-24 23:44:04 +08:00
List<string> childTypeList = new List<string>();
2024-10-11 10:12:15 +08:00
string childName = input;
MatchCollection matches = Regex.Matches(input, pattern);
foreach (Match match in matches)
{
childName = childName.Replace(match.Groups[0].Value, "");
string content = match.Groups[1].Value;
2024-10-24 23:44:04 +08:00
if (componentTypeDic.ContainsKey(content))
2024-10-11 10:12:15 +08:00
{
2024-10-24 23:44:04 +08:00
childTypeList.Add(content);
2024-10-11 10:12:15 +08:00
}
}
return (childName, childTypeList);
}
2024-10-24 23:44:04 +08:00
2024-10-11 10:12:15 +08:00
/// <summary>
/// 获取子物体的路径
/// </summary>
/// <param name="child"></param>
/// <returns></returns>
private static string GetChildPath(Transform child)
{
string path = child.name;
Transform parent = child.parent;
while (parent != null)
{
path = parent.name + "/" + path;
parent = parent.parent;
}
//去除本身的路径
int slashIndex = path.IndexOf("/");
if (slashIndex >= 0)
{
path = path.Substring(slashIndex + 1);
}
return path;
}
2024-10-24 23:44:04 +08:00
private static void GenerateFrameBase(string prefabPath, string baseDirectoryPath, Dictionary<string, string> componentTypeDic, Dictionary<string, (string, List<string>)> componentDic)
2024-10-11 10:12:15 +08:00
{
2024-10-24 23:44:04 +08:00
string relativePrefabPath = PathTools.GetRelativePathToResources(prefabPath);
2024-10-11 10:12:15 +08:00
string define = "";
string code = "";
string name = "";
foreach (var component in componentDic)
{
name = component.Value.Item1;
foreach (var type in component.Value.Item2)
{
string tempType;
2024-10-24 23:44:04 +08:00
if (componentTypeDic.ContainsKey(type))
2024-10-11 10:12:15 +08:00
{
2024-10-24 23:44:04 +08:00
tempType = componentTypeDic[type];
2024-10-11 10:12:15 +08:00
}
else
{
tempType = type.ToString();
}
define += $"protected {tempType} _{type}{name};\n\t\t";
code += GetCodeByComponentType(type, tempType, name, component.Key);
}
}
2024-10-24 23:44:04 +08:00
string tempFileStr = FileTools.ReadFile(templeteBaseFileName);
2024-10-11 10:12:15 +08:00
2024-10-24 23:44:04 +08:00
if (!string.IsNullOrEmpty(tempFileStr))
{
tempFileStr = tempFileStr.Replace("$FrameBaseName$", $"{Path.GetFileNameWithoutExtension(prefabPath)}Base");
tempFileStr = tempFileStr.Replace("$PrefabPath$", relativePrefabPath);
tempFileStr = tempFileStr.Replace("$PropertyList$", define);
tempFileStr = tempFileStr.Replace("$PropertyInitList$", code);
string basePath = $"{Application.dataPath}/{baseDirectoryPath}/{Path.GetFileNameWithoutExtension(prefabPath)}Base.cs";
if (File.Exists(basePath)) File.Delete(basePath);
FileTools.WriteFile(basePath, tempFileStr);
}
2024-10-11 10:12:15 +08:00
}
2024-10-24 23:44:04 +08:00
private static string GetCodeByComponentType(string type, string componentName, string name, string path)
2024-10-11 10:12:15 +08:00
{
string code = "";
2024-10-24 23:44:04 +08:00
if (type == "Go")
{
code = $"_{type}{name} = GetChild(\"{path}\").gameObject;\n\t\t\t";
}
else
2024-10-11 10:12:15 +08:00
{
2024-10-24 23:44:04 +08:00
code = $"_{type}{name} = GetComponent<{componentName}>(\"{path}\");\n\t\t\t";
2024-10-11 10:12:15 +08:00
}
return code;
}
2024-10-24 23:44:04 +08:00
2024-10-11 10:12:15 +08:00
}
}