Skip to content

Commit 17a078c

Browse files
committed
Added create node form object feature
1 parent d704ed1 commit 17a078c

File tree

6 files changed

+130
-12
lines changed

6 files changed

+130
-12
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using UnityEngine;
4+
using GraphProcessor;
5+
using System.Linq;
6+
7+
[System.Serializable, NodeMenuItem("Custom/Game Object")]
8+
public class GameObjectNode : BaseNode, ICreateNodeFrom<GameObject>
9+
{
10+
[Output(name = "Out"), SerializeField]
11+
public GameObject output;
12+
13+
public override string name => "Game Object";
14+
15+
public bool InitializeNodeFromObject(GameObject value)
16+
{
17+
output = value;
18+
return true;
19+
}
20+
}

Assets/Examples/DefaultNodes/Nodes/GameObjectNode.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Assets/com.alelievr.NodeGraphProcessor/Editor/Views/BaseGraphView.cs

Lines changed: 63 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Linq;
99
using System;
1010
using UnityEditor.SceneManagement;
11+
using System.Reflection;
1112

1213
using Status = UnityEngine.UIElements.DropdownMenuAction.Status;
1314
using Object = UnityEngine.Object;
@@ -123,6 +124,8 @@ public class BaseGraphView : GraphView, IDisposable
123124

124125
public SerializedObject serializedGraph { get; private set; }
125126

127+
Dictionary<Type, Type> nodeTypePerCreateAssetType = new Dictionary<Type, Type>();
128+
126129
public BaseGraphView(EditorWindow window)
127130
{
128131
serializeGraphElements = SerializeGraphElementsCallback;
@@ -588,25 +591,52 @@ void DragPerformedCallback(DragPerformEvent e)
588591
var mousePos = (e.currentTarget as VisualElement).ChangeCoordinatesTo(contentViewContainer, e.localMousePosition);
589592
var dragData = DragAndDrop.GetGenericData("DragSelection") as List< ISelectable >;
590593

591-
if (dragData == null)
592-
return;
594+
// Drag and Drop for elements inside the graph
595+
if (dragData != null)
596+
{
597+
var exposedParameterFieldViews = dragData.OfType<ExposedParameterFieldView>();
598+
if (exposedParameterFieldViews.Any())
599+
{
600+
foreach (var paramFieldView in exposedParameterFieldViews)
601+
{
602+
RegisterCompleteObjectUndo("Create Parameter Node");
603+
var paramNode = BaseNode.CreateFromType< ParameterNode >(mousePos);
604+
paramNode.parameterGUID = paramFieldView.parameter.guid;
605+
AddNode(paramNode);
606+
}
607+
}
608+
}
593609

594-
var exposedParameterFieldViews = dragData.OfType<ExposedParameterFieldView>();
595-
if (exposedParameterFieldViews.Any())
610+
// External objects drag and drop
611+
if (DragAndDrop.objectReferences.Length > 0)
596612
{
597-
foreach (var paramFieldView in exposedParameterFieldViews)
613+
RegisterCompleteObjectUndo("Create Node From Object(s)");
614+
foreach (var obj in DragAndDrop.objectReferences)
598615
{
599-
RegisterCompleteObjectUndo("Create Parameter Node");
600-
var paramNode = BaseNode.CreateFromType< ParameterNode >(mousePos);
601-
paramNode.parameterGUID = paramFieldView.parameter.guid;
602-
AddNode(paramNode);
616+
var objectType = obj.GetType();
617+
618+
foreach (var kp in nodeTypePerCreateAssetType)
619+
{
620+
if (kp.Key.IsAssignableFrom(objectType))
621+
{
622+
var node = BaseNode.CreateFromType(kp.Value, mousePos);
623+
var initializeFunction = kp.Value.GetMethod(nameof(ICreateNodeFrom<Object>.InitializeNodeFromObject), BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
624+
if ((bool)initializeFunction.Invoke(node, new []{obj}))
625+
{
626+
AddNode(node);
627+
}
628+
else
629+
break;
630+
}
631+
}
603632
}
604633
}
605634
}
606635

607636
void DragUpdatedCallback(DragUpdatedEvent e)
608637
{
609638
var dragData = DragAndDrop.GetGenericData("DragSelection") as List<ISelectable>;
639+
var dragObjects = DragAndDrop.objectReferences;
610640
bool dragging = false;
611641

612642
if (dragData != null)
@@ -618,10 +648,11 @@ void DragUpdatedCallback(DragUpdatedEvent e)
618648
}
619649
}
620650

651+
if (dragObjects.Length > 0)
652+
dragging = true;
653+
621654
if (dragging)
622-
{
623655
DragAndDrop.visualMode = DragAndDropVisualMode.Generic;
624-
}
625656

626657
UpdateNodeInspectorSelection();
627658
}
@@ -717,6 +748,20 @@ public void Initialize(BaseGraph graph)
717748
InitializeView();
718749

719750
NodeProvider.LoadGraph(graph);
751+
752+
// Register the nodes that can be created from assets
753+
foreach (var nodeInfo in NodeProvider.GetNodeMenuEntries(graph))
754+
{
755+
var interfaces = nodeInfo.type.GetInterfaces();
756+
foreach (var i in interfaces)
757+
{
758+
if (i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICreateNodeFrom<>))
759+
{
760+
var genericArgument = i.GetGenericArguments()[0];
761+
nodeTypePerCreateAssetType[genericArgument] = nodeInfo.type;
762+
}
763+
}
764+
}
720765
}
721766

722767
public void ClearGraphElements()
@@ -888,6 +933,13 @@ public BaseNodeView AddNodeView(BaseNode node)
888933
return baseNodeView;
889934
}
890935

936+
public void RemoveNode(BaseNode node)
937+
{
938+
var view = nodeViewsPerNode[node];
939+
RemoveNodeView(view);
940+
graph.RemoveNode(node);
941+
}
942+
891943
public void RemoveNodeView(BaseNodeView nodeView)
892944
{
893945
RemoveElement(nodeView);

Assets/com.alelievr.NodeGraphProcessor/Editor/Views/ExposedParameterView.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,12 @@ void OnDragUpdatedEvent(DragUpdatedEvent evt)
141141
{
142142
DragAndDrop.visualMode = DragAndDropVisualMode.Move;
143143
int newIndex = GetInsertIndexFromMousePosition(evt.mousePosition);
144+
var graphSelectionDragData = DragAndDrop.GetGenericData("DragSelection");
144145

145-
foreach (var obj in DragAndDrop.GetGenericData("DragSelection") as List<ISelectable>)
146+
if (graphSelectionDragData == null)
147+
return;
148+
149+
foreach (var obj in graphSelectionDragData as List<ISelectable>)
146150
{
147151
if (obj is ExposedParameterFieldView view)
148152
{
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using UnityEngine;
2+
using System;
3+
using Object = UnityEngine.Object;
4+
5+
namespace GraphProcessor
6+
{
7+
/// <summary>
8+
/// Implement this interface on a BaseNode, it allows you to automatically spawn a node if an asset of type T is dropped in the graphview area
9+
/// </summary>
10+
/// <typeparam name="T">The type object your node will be created from, it must be a subclass of UnityEngine.Object</typeparam>
11+
public interface ICreateNodeFrom<T> where T : Object
12+
{
13+
/// <summary>
14+
/// This function is called just after creating the node from an object and allows you to initialize the node with the object data.
15+
/// </summary>
16+
/// <param name="value">Object value</param>
17+
/// <returns>True if the initialization happened correctly. False otherwise, returning false will discard your node.</returns>
18+
bool InitializeNodeFromObject(T value);
19+
}
20+
}

Assets/com.alelievr.NodeGraphProcessor/Runtime/Elements/ICreateNodeFromObject.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)