UnityRuntimeNodeEditor
1.0.0

几乎每个以Unity制作的节点编辑器都使用Unity编辑器来制作。
我的目标是与Unity UI一起运行时。
Unity版本2021.3.3F1

RGB颜色显示示例

只需扩展NodeEditor即可。
public class ExampleNodeEditor : NodeEditor
{
public override void StartEditor ( NodeGraph graph )
{
base . StartEditor ( graph ) ;
// make your custom initialization here
}
}使用API创建图形,图将扩展到holder对象。 (没有预制涉及)
public class ApplicationStartup : MonoBehaviour
{
public RectTransform editorHolder ;
public ExampleNodeEditor editor ; // asigned in unity from hierarchy
private void Start ( )
{
var graph = editor . CreateGraph < NodeGraph > ( editorHolder ) ;
// var graph = editor.CreateGraph<NodeGraph>(editorHolder, bgColor, connColor);
editor . StartEditor ( graph ) ;
}
}您可能还需要使用自己的自定义图形和预制。
public class ApplicationStartup : MonoBehaviour
{
public RectTransform editorHolder ;
public ExampleNodeEditor editor ;
private void Start ( )
{
editor . StartEditor ( graph ) ;
}
}图形操作是基于事件的。

您将在示例文件夹中找到一个完整的示例。让我们演练。
听编辑的事件
public class ExampleNodeEditor : NodeEditor
{
private string _savePath ;
public override void StartEditor ( NodeGraph graph )
{
base . StartEditor ( graph ) ;
_savePath = Application . dataPath + "/Example/Resources/graph.json" ;
Events . OnGraphPointerClickEvent += OnGraphPointerClick ;
Events . OnGraphPointerDragEvent += OnGraphPointerDrag ;
Events . OnNodePointerClickEvent += OnNodePointerClick ;
Events . OnConnectionPointerClickEvent += OnNodeConnectionPointerClick ;
}
} protected override void OnGraphPointerClick ( PointerEventData eventData )
{
switch ( eventData . button )
{
case PointerEventData . InputButton . Right :
{
var ctx = new ContextMenuBuilder ( )
. Add ( "nodes/float" , CreateFloatNode )
. Add ( "nodes/math op" , CreateMatOpNode )
. Add ( "graph/load" , ( ) => LoadGraph ( _savePath ) )
. Add ( "graph/save" , ( ) => SaveGraph ( _savePath ) )
. Build ( ) ;
SetContextMenu ( ctx ) ;
DisplayContextMenu ( ) ;
}
break ;
case PointerEventData . InputButton . Left : CloseContextMenu ( ) ; break ;
}
} protected override void OnNodePointerClick ( Node node , PointerEventData eventData )
{
if ( eventData . button == PointerEventData . InputButton . Right )
{
var ctx = new ContextMenuBuilder ( )
. Add ( "duplicate" , ( ) => DuplicateNode ( node ) )
. Add ( "clear connections" , ( ) => ClearConnections ( node ) )
. Add ( "delete" , ( ) => DeleteNode ( node ) )
. Build ( ) ;
SetContextMenu ( ctx ) ;
DisplayContextMenu ( ) ;
}
} protected override void OnNodeConnectionPointerClick ( string connId , PointerEventData eventData )
{
if ( eventData . button == PointerEventData . InputButton . Right )
{
var ctx = new ContextMenuBuilder ( )
. Add ( "clear connection" , ( ) => DisconnectConnection ( connId ) )
. Build ( ) ;
SetContextMenu ( ctx ) ;
DisplayContextMenu ( ) ;
}
}据说是为了创建一个新的节点:
public class MyAwesomeNode : Node
{
public TMP_InputField valueField ; // added from editor
public SocketOutput outputSocket ; // added from editor
public SocketInput inputSocket ; // added from editor
public override void Setup ( )
{
Register ( outputSocket ) ;
Register ( inputSocket ) ;
SetHeader ( "float" ) ;
}
public override void OnSerialize ( Serializer serializer )
{
// save values on graph save
serializer . Add ( "floatValue" , valueField . text ) ;
// it would be good idea to use JsonUtility for complex data
}
public override void OnDeserialize ( Serializer serializer )
{
// load values on graph load
var value = serializer . Get ( "floatValue" ) ;
valueField . SetTextWithoutNotify ( value ) ;
}
}要从编辑器创建一个节点,请从Resources文件夹传递其路径。
// context item actions
private void CreateMyNode ( )
{
graph . Create ( "Prefabs/Nodes/MyAwesomeNode" ) ; // your prefab path in resources
}查看ExampleScene中的完整启动以获取更多详细信息。
该项目正在积极发展。随时为任何建议或反馈提出问题。
麻省理工学院
版权(C)2022 CEM UGUR KARACAM