NodeCodeGen.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using UnityEditor;
  3. using UnityEngine;
  4. namespace XGame.Editor.Nodes
  5. {
  6. public class NodeCodeGen : CodeGenerator
  7. {
  8. public bool Gen(string nodeName, string componentName, string nodeGroupName)
  9. {
  10. if (string.IsNullOrEmpty(nodeName))
  11. {
  12. EditorUtility.DisplayDialog("代码生成错误", "Node名字不能为空!", "确认");
  13. return false;
  14. }
  15. if (string.IsNullOrEmpty(componentName))
  16. {
  17. EditorUtility.DisplayDialog("代码生成错误", "Component名字不能为空!", "确认");
  18. return false;
  19. }
  20. if (string.IsNullOrEmpty(nodeGroupName))
  21. {
  22. EditorUtility.DisplayDialog("代码生成错误", "NodeGroup不能为空!", "确认");
  23. return false;
  24. }
  25. try
  26. {
  27. InitPath("Nodes");
  28. if (!GenNode(nodeName, componentName, nodeGroupName))
  29. {
  30. return false;
  31. }
  32. if (!GenCommonCode(nodeName, componentName, componentName + "Component", "NodeComponentTemplate"))
  33. {
  34. return false;
  35. }
  36. AssetDatabase.SaveAssets();
  37. AssetDatabase.Refresh();
  38. }
  39. catch (Exception ex)
  40. {
  41. Debug.LogException(ex);
  42. return false;
  43. }
  44. return true;
  45. }
  46. private bool GenNode(string nodeName, string componentName, string nodeGroupName)
  47. {
  48. return GenCommonCode(nodeName, componentName, nodeName + "Node", "NodeTemplate", codeText =>
  49. {
  50. return codeText.Replace("<#NodeGroupName>", nodeGroupName);
  51. });
  52. }
  53. }
  54. }