Unity 框架

UICreateTool.cs 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /****************************************************
  2. 文件:UICreateTool.cs
  3. 作者:陶长春
  4. 邮箱:376248129@qq.com
  5. 日期:2025年2月5日 3:03:33
  6. UnityVersion: 2021.3.13f1
  7. 功能:UI创建工具
  8. *****************************************************/
  9. using System.Collections.Generic;
  10. using System.IO;
  11. using System.Text;
  12. using UnityEditor;
  13. using UnityEngine;
  14. namespace TFramework
  15. {
  16. public static class UICreateTool
  17. {
  18. [MenuItem("GameObject/UI/扩展/面板")]
  19. public static void CreateUI()
  20. {
  21. CreateUIWin.OpenWin();
  22. }
  23. }
  24. public class CreateUIWin:EditorWindow
  25. {
  26. static CreateUIWin win;
  27. public static void OpenWin()
  28. {
  29. win = GetWindow<CreateUIWin>();
  30. win.maxSize = new Vector2(300, 80);
  31. win.minSize = new Vector2(300, 80);
  32. win.titleContent.text = "创建UI";
  33. }
  34. string _panleName;
  35. string _scriptName;
  36. string _scriptSavePath;
  37. string PanleName
  38. {
  39. get => _panleName;
  40. set
  41. {
  42. _panleName = value;
  43. _scriptName = _panleName;
  44. }
  45. }
  46. private void OnGUI()
  47. {
  48. using(new EditorGUILayout.HorizontalScope())
  49. {
  50. GUILayout.Label("面板名称:",GUILayout.ExpandWidth(false));
  51. PanleName = EditorGUILayout.TextField(PanleName);
  52. }
  53. using(new EditorGUILayout.HorizontalScope())
  54. {
  55. GUILayout.Label("脚本名称:", GUILayout.ExpandWidth(false));
  56. _scriptName = EditorGUILayout.TextField(_scriptName);
  57. }
  58. using (new EditorGUILayout.HorizontalScope())
  59. {
  60. GUILayout.Label("脚本保存路径:", GUILayout.ExpandWidth(false));
  61. _scriptSavePath = EditorGUILayout.TextField(_scriptSavePath);
  62. if(GUILayout.Button(EditorGUIUtility.IconContent("d_Folder On Icon").image,GUILayout.Width(30),GUILayout.Height(20)))
  63. {
  64. _scriptSavePath = EditorUtility.OpenFolderPanel("保存路径", Application.dataPath, "Assets");
  65. }
  66. }
  67. GUI.enabled = !string.IsNullOrEmpty(_panleName) && !string.IsNullOrEmpty(_scriptName) && !string.IsNullOrEmpty(_scriptSavePath);
  68. GUI.color = Color.green;
  69. if(GUILayout.Button("创建"))
  70. {
  71. if (CreateUI())
  72. {
  73. Selection.activeGameObject.name = _panleName;
  74. CreateScreipt();
  75. }
  76. }
  77. GUI.color = Color.white;
  78. GUI.enabled = true;
  79. }
  80. private bool CreateUI()
  81. {
  82. return EditorApplication.ExecuteMenuItem("GameObject/UI/Panel");
  83. }
  84. private void CreateScreipt()
  85. {
  86. string baseStr = AssetDatabase.LoadAssetAtPath<TextAsset>(EditorConfigFile.UIBaseScriptTemplatePath).text;
  87. string designerStr= AssetDatabase.LoadAssetAtPath<TextAsset>(EditorConfigFile.UIDesignerScriptTemplatePath).text;
  88. baseStr = baseStr.Replace("#ClassName", _scriptName);
  89. designerStr = designerStr.Replace("#ClassName", _scriptName);
  90. string baseFullPath = _scriptSavePath+$"/{_scriptName}.cs";
  91. string designerFullPath = _scriptSavePath + $"/{_scriptName}.Designer.cs";
  92. string basePath = baseFullPath.Remove(0, baseFullPath.IndexOf(Application.dataPath) + Application.dataPath.Length - 6);
  93. StreamWriter baseWriter = new StreamWriter(baseFullPath, false, UTF8Encoding.UTF8);
  94. baseWriter.Write(baseStr);
  95. baseWriter.Close();
  96. StreamWriter writer = new StreamWriter(designerFullPath, false, UTF8Encoding.UTF8);
  97. writer.Write(designerStr);
  98. writer.Close();
  99. AssetDatabase.ImportAsset(basePath);
  100. MonoScript script = AssetDatabase.LoadAssetAtPath<MonoScript>(basePath);
  101. AssetDatabase.OpenAsset(script);
  102. AssetDatabase.Refresh();
  103. Close();
  104. }
  105. }
  106. }