CustomContextMenuGraphView.cs 988 B

12345678910111213141516171819202122232425262728293031323334
  1. using UnityEngine.UIElements;
  2. using UnityEditor.Experimental.GraphView;
  3. using UnityEngine;
  4. using GraphProcessor;
  5. using System;
  6. using UnityEditor;
  7. public class CustomContextMenuGraphView : BaseGraphView
  8. {
  9. public CustomContextMenuGraphView(EditorWindow window) : base(window) {}
  10. public override void BuildContextualMenu(ContextualMenuPopulateEvent evt)
  11. {
  12. evt.menu.AppendSeparator();
  13. foreach (var nodeMenuItem in NodeProvider.GetNodeMenuEntries())
  14. {
  15. var mousePos = (evt.currentTarget as VisualElement).ChangeCoordinatesTo(contentViewContainer, evt.localMousePosition);
  16. Vector2 nodePosition = mousePos;
  17. evt.menu.AppendAction("Create/" + nodeMenuItem.path,
  18. (e) => CreateNodeOfType(nodeMenuItem.type, nodePosition),
  19. DropdownMenuAction.AlwaysEnabled
  20. );
  21. }
  22. base.BuildContextualMenu(evt);
  23. }
  24. void CreateNodeOfType(Type type, Vector2 position)
  25. {
  26. RegisterCompleteObjectUndo("Added " + type + " node");
  27. AddNode(BaseNode.CreateFromType(type, position));
  28. }
  29. }