Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex-Rachel committed Jun 27, 2024
2 parents fcb3416 + 3df18c0 commit dc7ab7f
Show file tree
Hide file tree
Showing 5 changed files with 234 additions and 1 deletion.
2 changes: 1 addition & 1 deletion UnityProject/Assets/TEngine/Editor/UI/ScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private static void Generate(bool includeListener, bool isUniTask = false)
}
}

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)
Expand Down
73 changes: 73 additions & 0 deletions UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs
Original file line number Diff line number Diff line change
@@ -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;
}

}
}

11 changes: 11 additions & 0 deletions UnityProject/Assets/TEngine/Editor/UI/TEUIHelper.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

138 changes: 138 additions & 0 deletions UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}

11 changes: 11 additions & 0 deletions UnityProject/Assets/TEngine/Editor/UI/TEUIHelperWindiw.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit dc7ab7f

Please sign in to comment.