NodeGraphProcessorMenuItems.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEditor;
  5. using System.IO;
  6. using System.Reflection;
  7. using UnityEditor.ProjectWindowCallback;
  8. namespace GraphProcessor
  9. {
  10. /// <summary>
  11. /// To add the menu items that create node C# script templates files you can inherit from this class and use it's API combined with [MenuItem]
  12. /// See GraphProcessorMenuItems.cs in examples for implementation details
  13. /// </summary>
  14. public class NodeGraphProcessorMenuItems
  15. {
  16. static readonly string nodeBaseName = "Node.cs";
  17. static readonly string nodeViewBaseName = "NodeView.cs";
  18. static string _nodeTemplatePath = null;
  19. static string nodeTemplatePath
  20. {
  21. get
  22. {
  23. if (_nodeTemplatePath == null)
  24. {
  25. var template = Resources.Load<TextAsset>("NodeTemplate.cs");
  26. _nodeTemplatePath = AssetDatabase.GetAssetPath(template);
  27. }
  28. return _nodeTemplatePath;
  29. }
  30. }
  31. static string _nodeViewTemplatePath;
  32. static string nodeViewTemplatePath
  33. {
  34. get
  35. {
  36. if (_nodeViewTemplatePath == null)
  37. {
  38. var template = Resources.Load<TextAsset>("NodeViewTemplate.cs");
  39. _nodeViewTemplatePath = AssetDatabase.GetAssetPath(template);
  40. }
  41. return _nodeViewTemplatePath;
  42. }
  43. }
  44. protected static class MenuItemPosition
  45. {
  46. public const int afterCreateScript = 81;
  47. public const int beforeCreateScript = 79;
  48. }
  49. protected static string GetCurrentProjectWindowPath()
  50. {
  51. var path = "";
  52. var obj = Selection.activeObject;
  53. if (obj == null)
  54. return null;
  55. else
  56. path = AssetDatabase.GetAssetPath(obj.GetInstanceID());
  57. if (path.Length > 0)
  58. {
  59. if (Directory.Exists(path))
  60. return path;
  61. else
  62. return new FileInfo(path).Directory.FullName;
  63. }
  64. return null;
  65. }
  66. protected static void CreateDefaultNodeCSharpScritpt()
  67. {
  68. ProjectWindowUtil.CreateScriptAssetFromTemplateFile(nodeTemplatePath, nodeBaseName);
  69. }
  70. protected static void CreateDefaultNodeViewCSharpScritpt()
  71. {
  72. ProjectWindowUtil.CreateScriptAssetFromTemplateFile(nodeViewTemplatePath, nodeViewBaseName);
  73. }
  74. }
  75. }