diff --git a/Examples/Nodes/AddNode.cs b/Examples/Nodes/AddNode.cs
index 6b02b82..fc6de57 100644
--- a/Examples/Nodes/AddNode.cs
+++ b/Examples/Nodes/AddNode.cs
@@ -3,7 +3,7 @@
[System.Serializable]
public class AddNode : Node {
- public string thisIsAddNode = "Add";
+ public int someValue;
protected override void Init() {
inputs = new NodePort[2];
diff --git a/Examples/Nodes/BaseNode.cs b/Examples/Nodes/BaseNode.cs
index 04b0319..6ee4621 100644
--- a/Examples/Nodes/BaseNode.cs
+++ b/Examples/Nodes/BaseNode.cs
@@ -3,7 +3,7 @@
[System.Serializable]
public class BaseNode : Node {
- public string thisIsBaseNode = "ASDF";
+ public bool concat;
protected override void Init() {
inputs = new NodePort[2];
diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs
index 1668c2a..1034746 100644
--- a/Scripts/Editor/NodeEditorAction.cs
+++ b/Scripts/Editor/NodeEditorAction.cs
@@ -92,7 +92,7 @@ public partial class NodeEditorWindow {
if (IsDraggingPort) {
//If connection is valid, save it
if (draggedOutputTarget != null) {
- draggedOutput.Connect(draggedOutputTarget);
+ if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
}
//Release dragged connection
draggedOutput = null;
@@ -129,6 +129,7 @@ public partial class NodeEditorWindow {
/// Draw a connection as we are dragging it
public void DrawDraggedConnection() {
if (IsDraggingPort) {
+ if (!_portConnectionPoints.ContainsKey(draggedOutput)) return;
Vector2 from = _portConnectionPoints[draggedOutput];
Vector2 to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget] : WindowToGridPosition(Event.current.mousePosition);
DrawConnection(from, to);
diff --git a/Scripts/Editor/NodeEditorWindow.cs b/Scripts/Editor/NodeEditorWindow.cs
index bad2e31..13eaaa2 100644
--- a/Scripts/Editor/NodeEditorWindow.cs
+++ b/Scripts/Editor/NodeEditorWindow.cs
@@ -31,9 +31,11 @@ public partial class NodeEditorWindow : EditorWindow {
public void Save() {
saved = graph.Serialize();
+ Debug.Log(saved);
}
public void Load() {
+ Debug.Log(saved);
_graph = NodeGraph.Deserialize(saved);
}
@@ -41,12 +43,12 @@ public partial class NodeEditorWindow : EditorWindow {
GUI.DragWindow();
}
- public byte[] ProtoSerialize(T value) {
+ /*public byte[] ProtoSerialize(T value) {
using (var ms = new MemoryStream()) {
ProtoBuf.Serializer.Serialize(ms, value);
return ms.ToArray();
}
- }
+ }*/
public Vector2 WindowToGridPosition(Vector2 windowPosition) {
return (windowPosition - (position.size * 0.5f) - (panOffset / zoom)) * zoom;
diff --git a/Scripts/Node.cs b/Scripts/Node.cs
index 7af4c2b..5696e2d 100644
--- a/Scripts/Node.cs
+++ b/Scripts/Node.cs
@@ -7,12 +7,13 @@ using System;
[Serializable]
public abstract class Node {
+ [NonSerialized] public NodeGraph graph;
public string NodeType { get { return nodeType; } }
[SerializeField] private string nodeType;
- public Rect position = new Rect(0,0,200,200);
- protected NodePort[] inputs = new NodePort[0];
- protected NodePort[] outputs = new NodePort[0];
+ [SerializeField] public Rect position = new Rect(0,0,200,200);
+ [SerializeField] protected NodePort[] inputs = new NodePort[0];
+ [SerializeField] protected NodePort[] outputs = new NodePort[0];
public int InputCount { get { return inputs.Length; } }
public int OutputCount { get { return outputs.Length; } }
@@ -54,6 +55,13 @@ public abstract class Node {
return new NodePort(name, type, this, NodePort.IO.Output);
}
+ public void FinalizeDeserialization() {
+ for (int i = 0; i < outputs.Length; i++) {
+ outputs[i].FinalizeDeserialization();
+ }
+ }
+
+
public void ClearConnections() {
for (int i = 0; i < inputs.Length; i++) {
inputs[i].ClearConnections();
diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs
index 64a8ec7..073df97 100644
--- a/Scripts/NodeGraph.cs
+++ b/Scripts/NodeGraph.cs
@@ -12,30 +12,29 @@ public class NodeGraph {
/// Serialized nodes.
[SerializeField] public string[] s_nodes;
- public List strings = new List() { "ASDF", "3523" };
public T AddNode() where T : Node {
T node = default(T);
- nodes.Add(node);
- return node;
+ return AddNode(node) as T;
}
public Node AddNode(Type type) {
Node node = (Node)Activator.CreateInstance(type);
- if (node == null) {
- Debug.LogError("Node could node be instanced");
- return null;
- }
- nodes.Add(node);
- return node;
+ return AddNode(node);
}
public Node AddNode(string type) {
+ Debug.Log(type);
Node node = (Node)Activator.CreateInstance(null,type).Unwrap();
+ return AddNode(node);
+ }
+
+ public Node AddNode(Node node) {
if (node == null) {
Debug.LogError("Node could node be instanced");
return null;
}
nodes.Add(node);
+ node.graph = this;
return node;
}
@@ -76,8 +75,13 @@ public class NodeGraph {
for (int i = 0; i < nodeGraph.s_nodes.Length; i++) {
NodeTyper tempNode = new NodeTyper();
JsonUtility.FromJsonOverwrite(nodeGraph.s_nodes[i],tempNode);
- Node node = nodeGraph.AddNode(tempNode.nodeType);
- JsonUtility.FromJsonOverwrite(nodeGraph.s_nodes[i], node);
+ //Node node = nodeGraph.AddNode(tempNode.nodeType);
+ Type type = Type.GetType(tempNode.nodeType);
+ Node node = JsonUtility.FromJson(nodeGraph.s_nodes[i], type) as Node;
+ nodeGraph.AddNode(node);
+ }
+ for (int i = 0; i < nodeGraph.nodes.Count; i++) {
+ nodeGraph.nodes[i].FinalizeDeserialization();
}
return nodeGraph;
}
diff --git a/Scripts/NodePort.cs b/Scripts/NodePort.cs
index b4c562d..0ac5359 100644
--- a/Scripts/NodePort.cs
+++ b/Scripts/NodePort.cs
@@ -3,7 +3,8 @@ using System.Collections.Generic;
using UnityEngine;
using System;
-public class NodePort {
+[Serializable]
+public class NodePort :ISerializationCallbackReceiver{
public enum IO { Input, Output}
public int ConnectionCount { get { return connections.Count; } }
@@ -23,13 +24,15 @@ public class NodePort {
public string name { get { return _name; } set { _name = value; } }
public bool enabled { get { return _enabled; } set { _enabled = value; } }
- private Type _type;
- private List connections = new List();
+ [NonSerialized] private List connections = new List();
+ [SerializeField] private Type _type;
[SerializeField] private string _name;
[SerializeField] private bool _enabled = true;
[SerializeField] private IO _direction;
+ [SerializeField] private PortID[] connectionIDs;
+
public NodePort(string name, Type type, Node node, IO direction) {
_name = name;
_type = type;
@@ -64,4 +67,38 @@ public class NodePort {
}
connections.Clear();
}
+
+ public void OnBeforeSerialize() {
+ if (direction == IO.Output) {
+ connectionIDs = new PortID[connections.Count];
+ for (int i = 0; i < connections.Count; i++) {
+ connectionIDs[i] = new PortID();
+ connectionIDs[i].nodeID = node.graph.nodes.IndexOf(connections[i].node);
+ connectionIDs[i].portID = connections[i].node.GetInputId(connections[i]);
+ }
+ }
+ }
+
+ public void OnAfterDeserialize() {
+
+ }
+
+ public void FinalizeDeserialization() {
+ //Reconnect
+ if (direction == IO.Output) {
+ connections = new List();
+ for (int i = 0; i < connectionIDs.Length; i++) {
+ Node node = this.node.graph.nodes[connectionIDs[i].nodeID];
+ NodePort port = node.GetInput(connectionIDs[i].portID);
+ Connect(port);
+ }
+ }
+ }
+
+
+ [Serializable]
+ private class PortID {
+ public int nodeID;
+ public int portID;
+ }
}