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