NodeGenWindow.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using System;
  2. using System.Reflection;
  3. using UnityEditor;
  4. using UnityEngine;
  5. using XGame.Framework.Nodes;
  6. namespace XGame.Editor.Nodes
  7. {
  8. public class NodeGenWindow : EditorWindow
  9. {
  10. [MenuItem("Tools/Node/Node代码生成窗口")]
  11. public static void ShowWindow()
  12. {
  13. NodeGenWindow window = EditorWindow.GetWindowWithRect<NodeGenWindow>(new Rect(0, 0, 400, 300), false, "Node代码生成窗口");
  14. window.Show();
  15. }
  16. GUIStyle _titleStyle;
  17. GUIStyle _btnStyle;
  18. private Type _nodeGroupType;
  19. private Enum _selectedGroup;
  20. private string _nodeName;
  21. private string _componentName;
  22. private void OnEnable()
  23. {
  24. _nodeGroupType = FindNodeGroupType();
  25. _titleStyle = new GUIStyle("Label");
  26. _titleStyle.fontSize = 25;
  27. _titleStyle.alignment = TextAnchor.MiddleCenter;
  28. _titleStyle.fontStyle = FontStyle.BoldAndItalic;
  29. _btnStyle = new GUIStyle("Button");
  30. _btnStyle.fontSize = 25;
  31. _btnStyle.alignment = TextAnchor.MiddleCenter;
  32. _btnStyle.fontStyle = FontStyle.BoldAndItalic;
  33. }
  34. private void OnDisable()
  35. {
  36. _nodeGroupType = null;
  37. _titleStyle = null;
  38. _btnStyle = null;
  39. }
  40. private void OnGUI()
  41. {
  42. GUILayout.Label(new GUIContent("Node代码生成器"), _titleStyle, GUILayout.Height(50));
  43. DrawNodeGroup();
  44. DrawNodeName();
  45. DrawGenerateBtn();
  46. }
  47. private Type FindNodeGroupType()
  48. {
  49. try
  50. {
  51. var assemblies = AppDomain.CurrentDomain.GetAssemblies();
  52. foreach (var assembly in assemblies)
  53. {
  54. var assemblyName = assembly.GetName().Name;
  55. if (assemblyName.StartsWith("Business", StringComparison.OrdinalIgnoreCase) == false ||
  56. assemblyName.Contains("Editor", StringComparison.OrdinalIgnoreCase))
  57. {
  58. continue;
  59. }
  60. var types = assembly.GetTypes();
  61. foreach (var type in types)
  62. {
  63. if (type.IsEnum && type.GetCustomAttribute<NodeGroupAttribute>() != null)
  64. {
  65. return type;
  66. }
  67. }
  68. }
  69. }
  70. catch (Exception e)
  71. {
  72. Debug.LogException(e);
  73. }
  74. return null;
  75. }
  76. private void DrawNodeGroup()
  77. {
  78. if (_nodeGroupType != null)
  79. {
  80. var names = Enum.GetNames(_nodeGroupType);
  81. if (_selectedGroup == null)
  82. _selectedGroup = Enum.Parse(_nodeGroupType, names[0]) as Enum;
  83. _selectedGroup = EditorGUILayout.EnumPopup("选择分组类型:", _selectedGroup);
  84. }
  85. else
  86. {
  87. EditorGUILayout.HelpBox("没有找到NodeGroup的枚举,请先定义枚举并使用特性:NodeGroupAttribute", MessageType.Error);
  88. }
  89. }
  90. private void DrawNodeName()
  91. {
  92. _nodeName = EditorGUILayout.TextField("请输入Node名字:", _nodeName);
  93. _componentName = EditorGUILayout.TextField("请输入Component名字:", _componentName);
  94. }
  95. private void DrawGenerateBtn()
  96. {
  97. GUILayout.FlexibleSpace();
  98. GUILayout.BeginVertical();
  99. GUIStyle style = new GUIStyle("Button")
  100. {
  101. alignment = TextAnchor.MiddleCenter,
  102. fontStyle = FontStyle.BoldAndItalic,
  103. fontSize = 15,
  104. };
  105. GUI.color = Color.green;
  106. if (GUILayout.Button("Generator", style, GUILayout.Height(50)))
  107. {
  108. if (_nodeGroupType == null || _selectedGroup == null)
  109. {
  110. EditorUtility.DisplayDialog("代码生成错误", "NodeGroup不能为空!", "确认");
  111. return;
  112. }
  113. var isSuccess = (new NodeCodeGen()).Gen(_nodeName, _componentName, $"{_nodeGroupType.Name}.{_selectedGroup}");
  114. if (isSuccess)
  115. {
  116. Close();
  117. }
  118. //new NodeCodeGen().CodeGen(GetCodeGenType(), inputStr, saveObj, behaviour, mapVO, saveMapVO, objcetName);
  119. }
  120. GUI.color = Color.white;
  121. GUILayout.EndVertical();
  122. }
  123. }
  124. }