/**************************************************** 文件:UICreateTool.cs 作者:陶长春 邮箱:376248129@qq.com 日期:2025年2月5日 3:03:33 UnityVersion: 2021.3.13f1 功能:UI创建工具 *****************************************************/ using System.Collections.Generic; using System.IO; using System.Text; using UnityEditor; using UnityEngine; namespace TFramework { public static class UICreateTool { [MenuItem("GameObject/UI/扩展/面板")] public static void CreateUI() { CreateUIWin.OpenWin(); } } public class CreateUIWin:EditorWindow { static CreateUIWin win; public static void OpenWin() { win = GetWindow(); win.maxSize = new Vector2(300, 80); win.minSize = new Vector2(300, 80); win.titleContent.text = "创建UI"; } string _panleName; string _scriptName; string _scriptSavePath; string PanleName { get => _panleName; set { _panleName = value; _scriptName = _panleName; } } private void OnGUI() { using(new EditorGUILayout.HorizontalScope()) { GUILayout.Label("面板名称:",GUILayout.ExpandWidth(false)); PanleName = EditorGUILayout.TextField(PanleName); } using(new EditorGUILayout.HorizontalScope()) { GUILayout.Label("脚本名称:", GUILayout.ExpandWidth(false)); _scriptName = EditorGUILayout.TextField(_scriptName); } using (new EditorGUILayout.HorizontalScope()) { GUILayout.Label("脚本保存路径:", GUILayout.ExpandWidth(false)); _scriptSavePath = EditorGUILayout.TextField(_scriptSavePath); if(GUILayout.Button(EditorGUIUtility.IconContent("d_Folder On Icon").image,GUILayout.Width(30),GUILayout.Height(20))) { _scriptSavePath = EditorUtility.OpenFolderPanel("保存路径", Application.dataPath, "Assets"); } } GUI.enabled = !string.IsNullOrEmpty(_panleName) && !string.IsNullOrEmpty(_scriptName) && !string.IsNullOrEmpty(_scriptSavePath); GUI.color = Color.green; if(GUILayout.Button("创建")) { if (CreateUI()) { Selection.activeGameObject.name = _panleName; CreateScreipt(); } } GUI.color = Color.white; GUI.enabled = true; } private bool CreateUI() { return EditorApplication.ExecuteMenuItem("GameObject/UI/Panel"); } private void CreateScreipt() { string baseStr = AssetDatabase.LoadAssetAtPath(EditorConfigFile.UIBaseScriptTemplatePath).text; string designerStr= AssetDatabase.LoadAssetAtPath(EditorConfigFile.UIDesignerScriptTemplatePath).text; baseStr = baseStr.Replace("#ClassName", _scriptName); designerStr = designerStr.Replace("#ClassName", _scriptName); string baseFullPath = _scriptSavePath+$"/{_scriptName}.cs"; string designerFullPath = _scriptSavePath + $"/{_scriptName}.Designer.cs"; string basePath = baseFullPath.Remove(0, baseFullPath.IndexOf(Application.dataPath) + Application.dataPath.Length - 6); StreamWriter baseWriter = new StreamWriter(baseFullPath, false, UTF8Encoding.UTF8); baseWriter.Write(baseStr); baseWriter.Close(); StreamWriter writer = new StreamWriter(designerFullPath, false, UTF8Encoding.UTF8); writer.Write(designerStr); writer.Close(); AssetDatabase.ImportAsset(basePath); MonoScript script = AssetDatabase.LoadAssetAtPath(basePath); AssetDatabase.OpenAsset(script); AssetDatabase.Refresh(); Close(); } } }