This commit is contained in:
ALEXTANG
2024-06-27 21:02:55 +08:00
5 changed files with 234 additions and 1 deletions

View File

@@ -94,7 +94,7 @@ namespace TEngine.Editor.UI
}
}
private static void Ergodic(Transform root, Transform transform, ref StringBuilder strVar, ref StringBuilder strBind, ref StringBuilder strOnCreate,
public static void Ergodic(Transform root, Transform transform, ref StringBuilder strVar, ref StringBuilder strBind, ref StringBuilder strOnCreate,
ref StringBuilder strCallback, bool isUniTask)
{
for (int i = 0; i < transform.childCount; ++i)

View File

@@ -0,0 +1,73 @@
using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
#region
//作者:Saber
#endregion
namespace TEngine.Editor.UI
{
public class TEUIHelper
{
public static string Generate(bool includeListener, bool isUniTask = false, Transform root = null, string nameSpace = "",string className="")
{
if (root != null)
{
StringBuilder strVar = new StringBuilder();
StringBuilder strBind = new StringBuilder();
StringBuilder strOnCreate = new StringBuilder();
StringBuilder strCallback = new StringBuilder();
ScriptGenerator.Ergodic(root, root, ref strVar, ref strBind, ref strOnCreate, ref strCallback, isUniTask);
//object[] args = new object[] { root, root, strVar, strBind, strOnCreate, strCallback, isUniTask };
//typeof(TEngine.Editor.UI.ScriptGenerator).GetMethod("Ergodic",System.Reflection.BindingFlags.NonPublic| System.Reflection.BindingFlags.Static).Invoke(null,args);
//strVar = args[2] as StringBuilder;
//strBind = args[3] as StringBuilder;
//strOnCreate = args[4] as StringBuilder;
//strCallback = args[5] as StringBuilder;
StringBuilder strFile = new StringBuilder();
if (includeListener)
{
#if ENABLE_TEXTMESHPRO
strFile.Append("using TMPro;\n");
#endif
if (isUniTask)
{
strFile.Append("using Cysharp.Threading.Tasks;\n");
}
}
strFile.Append("using UnityEngine;\n");
strFile.Append("using UnityEngine.UI;\n");
strFile.Append("using TEngine;\n\n");
nameSpace = string.IsNullOrEmpty(nameSpace) ? SettingsUtils.GetUINameSpace() : nameSpace;
strFile.Append($"namespace {nameSpace}\n");
strFile.Append("{\n");
//strFile.Append("\t[Window(UILayer.UI)]\n");
strFile.Append("\tpartial class " + className + "\n");
strFile.Append("\t{\n");
// 脚本工具生成的代码
strFile.Append("\t\t#region 脚本工具生成的代码\n");
strFile.Append(strVar);
strFile.Append("\t\tprotected override void ScriptGenerator()\n");
strFile.Append("\t\t{\n");
strFile.Append(strBind);
strFile.Append(strOnCreate);
strFile.Append("\t\t}\n");
strFile.Append("\t\t#endregion");
strFile.Append("\n\t}");
strFile.Append("\n}");
return strFile.ToString();
}
return string.Empty;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 783105f2b5405eb45bb29a4f5131dc3e
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,138 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Sirenix.OdinInspector;
using Sirenix.OdinInspector.Editor;
using System.IO;
using UnityEditor;
using UnityEditor.SceneManagement;
#region
//作者:Saber
#endregion
namespace TEngine.Editor.UI
{
public class TEUIToolWindow : OdinEditorWindow
{
public enum Type
{
normal,
unitask,
listener,
all
}
[ReadOnly, LabelText("选中物体")]
public Transform root;
[LabelText("生成类型")]
public Type type;
[LabelText("命名空间")]
public string nameSpace = "";
[LabelText("类名")]
public string className;
[LabelText("生成位置"), FolderPath]
public string savePath= "Assets/Scripts/UIScriptsAuto";
[ReadOnly, LabelText("生成的脚本"),HorizontalGroup(GroupID ="BS")]
public TextAsset buildScript;
protected override void OnDisable()
{
base.OnDisable();
Save();
}
void Save()
{
EditorPrefs.SetString($"Last_UIScriptsAutoNameSpace", nameSpace);
EditorPrefs.SetInt($"Last_UIScriptsAutoType", (int)type);
EditorPrefs.SetString($"Last_UIScriptsAutoSavePath", savePath);
}
void Load()
{
nameSpace = EditorPrefs.GetString($"Last_UIScriptsAutoNameSpace", nameSpace);
type = (Type)EditorPrefs.GetInt($"Last_UIScriptsAutoType", (int)type);
var newSavePath = EditorPrefs.GetString($"Last_UIScriptsAutoSavePath", savePath);
if (string.IsNullOrEmpty(newSavePath)==false)
{
savePath = newSavePath;
OnSavePathChange();
}
}
void OnSavePathChange()
{
var originPath = $"{savePath}/{className}_Auto.cs";
var path = Path.GetFullPath(originPath);
if (File.Exists(path))
buildScript = AssetDatabase.LoadAssetAtPath<TextAsset>(originPath);
}
[Button(SdfIconType.Trash, Name = ""), HorizontalGroup(GroupID = "BS", Width = 50)]
void Delete()
{
AssetDatabase.DeleteAsset($"{savePath}/{className}.cs");
buildScript = null;
}
[Sirenix.OdinInspector.PropertySpace(30)]
[Button(Icon = SdfIconType.Gem, Name = "生成"), HorizontalGroup(GroupID = "AA" /*, Width = 150*/,MinWidth =200,MarginLeft =0.3f,MarginRight =0.3f)]
public void Build()
{
var str = TEUIHelper.Generate(type == Type.listener || type == Type.all, type == Type.unitask || type == Type.all, root, nameSpace, className);
if (string.IsNullOrEmpty(str))
{
Debug.LogError("出错啦请检查是否选中root为空" + root == null);
return;
}
var originPath = $"{savePath}/{className}_Auto.cs";
var path = Path.GetFullPath(originPath);
MakeSure(path);
File.WriteAllText(path, str);
AssetDatabase.Refresh();
buildScript= AssetDatabase.LoadAssetAtPath<TextAsset>(originPath);
Debug.Log($"保存完毕:Cs->{className}.cs ,位置:{path}");
}
public void OnInit(Transform root)
{
this.root = root;
this.className = this.root.name;
Load();
}
[MenuItem("GameObject/ScriptGenerator/BuildWindow", priority = 40)]
static void OpenWindow()
{
var root = Selection.activeTransform;
if (root != null && (PrefabUtility.IsPartOfAnyPrefab(root) || (PrefabStageUtility.GetCurrentPrefabStage() != null && PrefabStageUtility.GetCurrentPrefabStage().IsPartOfPrefabContents(root.gameObject))))
{
var window = GetWindow<TEUIToolWindow>();
// 设置窗口大小
window.minSize = new Vector2(600, 250);
window.OnInit(root);
}
else
Debug.LogError("仅支持预制体,请选中预制体");
}
static void OpenTopWindow()
{
var window = EditorWindow.GetWindow<TEUIToolWindow>();
//Debug.Log("windiw" + window.name + "_aaa");
window.position = new Rect(0, 0, 600, 800);
}
public static void MakeSure(string path)
{
path = path.Replace("\\", "/");
var extion = Path.GetExtension(path);
var dire = System.IO.Path.GetDirectoryName(path);
if (System.IO.Directory.Exists(dire) == false)
System.IO.Directory.CreateDirectory(dire);
if (string.IsNullOrEmpty(extion) == false)
{
if (System.IO.File.Exists(path) == false)
System.IO.File.Create(path).Dispose();
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f1012810e422c9249a0e302985d02b45
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: