unity自定义节点编辑器 GraphView

这篇还可以

https://blog.csdn.net/dmk17771552304/article/details/121499476

 

using System.Linq;
using UnityEngine;
using UnityEditor.Experimental.GraphView;
 
public class LogNode : ProcessNode
{
    private Port inputString;
 
    public LogNode() : base()
    {
        title = "Log";
 
        inputString = Port.Create<Edge>(Orientation.Horizontal, Direction.Input, Port.Capacity.Single, typeof(string));
        inputContainer.Add(inputString);
    }
 
    public override void Execute()
    {
        var edge = inputString.connections.FirstOrDefault();
        var node = edge.output.node as StringNode;
 
        if (node == null) return;
 
        Debug.Log(node.Text);
    }
}

 

using UnityEditor.Experimental.GraphView;
 
public abstract class ProcessNode : SampleNode
{
    public Port InputPort;
    public Port OutputPort;
 
    public ProcessNode()
    {
        InputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Input, Port.Capacity.Single, typeof(Port));
        InputPort.portName = "In";
        inputContainer.Add(InputPort);
 
        OutputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Output, Port.Capacity.Single, typeof(Port));
        OutputPort.portName = "Out";
        outputContainer.Add(OutputPort);
    }
 
    public abstract void Execute();
}

 

using System.Linq;
using UnityEditor.Experimental.GraphView;
 
public class RootNode : SampleNode
{
    public Port OutputPort;
 
    public RootNode() : base()
    {
        title = "Root";
 
        capabilities -= Capabilities.Deletable;
 
        OutputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Output, Port.Capacity.Single, typeof(Port));
        OutputPort.portName = "Out";
        outputContainer.Add(OutputPort);
    }

    //-------------------------------------
 

    
}

 

using System.Linq;
using UnityEditor;
using UnityEngine.UIElements;

public class SampleGraphEditorWindow : EditorWindow {
    
    
    [MenuItem("Window/Open SampleGraphView")]
    public static void Open()
    {
        GetWindow<SampleGraphEditorWindow>("SampleGraphView");
    }

    void OnEnable()
    {
        var graphView = new SampleGraphView()
        {
            style = { flexGrow = 1 }
        };
        rootVisualElement.Add(graphView);
 
        
        
        rootVisualElement.Add(new Button(graphView.Execute) { text = "Execute" });
    }
}

 

using System.Collections.Generic;
using System.Linq;
using UnityEditor.Experimental.GraphView;
using UnityEngine;
using UnityEngine.UIElements;
 
public class SampleGraphView : GraphView
{
    public RootNode root;
    
    public SampleGraphView()
    {
        SetupZoom(ContentZoomer.DefaultMinScale, ContentZoomer.DefaultMaxScale);
 
        Insert(0, new GridBackground());
        
        //AddElement(new SampleNode());
        this.AddManipulator(new SelectionDragger());
        
        var searchWindowProvider = ScriptableObject.CreateInstance<SampleSearchWindowProvider>();
        searchWindowProvider.Initialize(this);
        
        root = new RootNode();
        AddElement(root);
 
        nodeCreationRequest += context =>
        {
            SearchWindow.Open(new SearchWindowContext(context.screenMousePosition), searchWindowProvider);
        };
    }
 
    public override List<Port> GetCompatiblePorts(Port startAnchor, NodeAdapter nodeAdapter)
    {
        var compatiblePorts = new List<Port>();
        foreach (var port in ports.ToList())
        {
            if (startAnchor.node == port.node ||
                startAnchor.direction == port.direction ||
                startAnchor.portType != port.portType)
            {
                continue;
            }
 
            compatiblePorts.Add(port);
        }
        return compatiblePorts;
    }
    
    public void Execute()
    {
        var rootEdge = root.OutputPort.connections.FirstOrDefault();
        if (rootEdge == null) return;
 
        var currentNode = rootEdge.input.node as ProcessNode;
 
        while (true)
        {
            currentNode.Execute();
 
            var edge = currentNode.OutputPort.connections.FirstOrDefault();
            if (edge == null) break;
 
            currentNode = edge.input.node as ProcessNode;
        }
    }
}

 

using UnityEditor.Experimental.GraphView;
 
public class SampleNode : Node
{
    public SampleNode()
    {
        title = "Sample";
 
        var inputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Input, Port.Capacity.Single, typeof(Port));
        inputContainer.Add(inputPort);
 
        var outputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Output, Port.Capacity.Single, typeof(Port));
        outputContainer.Add(outputPort);
    }
}

 

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor.Experimental.GraphView;
 
public class SampleSearchWindowProvider : ScriptableObject, ISearchWindowProvider
{
    private SampleGraphView graphView;
 
    public void Initialize(SampleGraphView graphView)
    {
        this.graphView = graphView;
    }
 
    List<SearchTreeEntry> ISearchWindowProvider.CreateSearchTree(SearchWindowContext context)
    {
        var entries = new List<SearchTreeEntry>();
        entries.Add(new SearchTreeGroupEntry(new GUIContent("Create Node")));
 
        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            foreach (var type in assembly.GetTypes())
            {
                if (type.IsClass && !type.IsAbstract && (type.IsSubclassOf(typeof(SampleNode)))
                    && type != typeof(RootNode))
                {
                    entries.Add(new SearchTreeEntry(new GUIContent(type.Name)) { level = 1, userData = type });
                }
            }
        }
 
        return entries;
    }
 
    bool ISearchWindowProvider.OnSelectEntry(SearchTreeEntry searchTreeEntry, SearchWindowContext context)
    {
        var type = searchTreeEntry.userData as System.Type;
        var node = Activator.CreateInstance(type) as SampleNode;
        graphView.AddElement(node);
        return true;
    }
}

 

using UnityEngine.UIElements;
using UnityEditor.Experimental.GraphView;
 
public class StringNode : SampleNode
{
    private TextField textField;
    public string Text { get { return textField.value; } }
 
    public StringNode() : base()
    {
        title = "String";
 
        var outputPort = Port.Create<Edge>(Orientation.Horizontal, Direction.Output, Port.Capacity.Multi, typeof(string));
        outputContainer.Add(outputPort);
 
        textField = new TextField();
        mainContainer.Add(textField);
    }
}

 

posted @ 2023-03-16 17:37  三页菌  阅读(545)  评论(0编辑  收藏  举报