ShaderGroup.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. using System;
  2. using System.Linq;
  3. using UnityEditor.Experimental.GraphView;
  4. using UnityEditor.Graphing;
  5. using UnityEngine;
  6. using UnityEngine.UIElements;
  7. using ContextualMenuManipulator = UnityEngine.UIElements.ContextualMenuManipulator;
  8. using ContextualMenuPopulateEvent = UnityEngine.UIElements.ContextualMenuPopulateEvent;
  9. using VisualElementExtensions = UnityEngine.UIElements.VisualElementExtensions;
  10. namespace UnityEditor.ShaderGraph
  11. {
  12. sealed class ShaderGroup : Group
  13. {
  14. GraphData m_Graph;
  15. public new GroupData userData
  16. {
  17. get => (GroupData)base.userData;
  18. set => base.userData = value;
  19. }
  20. public ShaderGroup(GraphData graph)
  21. {
  22. m_Graph = graph;
  23. VisualElementExtensions.AddManipulator(this, new ContextualMenuManipulator(BuildContextualMenu));
  24. }
  25. public void BuildContextualMenu(ContextualMenuPopulateEvent evt)
  26. {
  27. if (evt.target is ShaderGroup)
  28. {
  29. evt.menu.AppendAction("Delete Group and Contents", RemoveNodesInsideGroup, DropdownMenuAction.AlwaysEnabled);
  30. }
  31. }
  32. void RemoveNodesInsideGroup(DropdownMenuAction action)
  33. {
  34. m_Graph.owner.RegisterCompleteObjectUndo("Delete Group and Contents");
  35. var groupItems = m_Graph.GetItemsInGroup(userData);
  36. m_Graph.RemoveElements(groupItems.OfType<AbstractMaterialNode>().ToArray(), new IEdge[] {}, new [] {userData}, groupItems.OfType<StickyNoteData>().ToArray());
  37. }
  38. }
  39. }