1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-21 01:36:03 +08:00

Serialization now works. Node.cs is now a ScriptableObject.

This commit is contained in:
Thor Brigsted 2017-10-07 01:54:10 +02:00
parent 91ccab60db
commit 698f02d716
6 changed files with 81 additions and 29 deletions

View File

@ -1,6 +1,6 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: 2e27fbb85ccd5994e932fd8a6d34e4b3 guid: ce9064d2750a8344784c005788637f15
timeCreated: 1506790922 timeCreated: 1507333474
licenseType: Free licenseType: Free
NativeFormatImporter: NativeFormatImporter:
mainObjectFileID: 11400000 mainObjectFileID: 11400000

View File

@ -93,6 +93,8 @@ public partial class NodeEditorWindow {
//If connection is valid, save it //If connection is valid, save it
if (draggedOutputTarget != null) { if (draggedOutputTarget != null) {
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget); if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
EditorUtility.SetDirty(graph);
AssetDatabase.SaveAssets();
} }
//Release dragged connection //Release dragged connection
draggedOutput = null; draggedOutput = null;

View File

@ -5,23 +5,24 @@ using System;
/// <summary> Base class for all nodes </summary> /// <summary> Base class for all nodes </summary>
[Serializable] [Serializable]
public abstract class Node { public abstract class Node : ScriptableObject {
/// <summary> Name of the node </summary> /// <summary> Name of the node </summary>
[SerializeField] public string name = ""; [NonSerialized] public NodeGraph graph;
[SerializeField] public NodeGraph graph;
[SerializeField] public Rect rect = new Rect(0,0,200,200); [SerializeField] public Rect rect = new Rect(0,0,200,200);
/// <summary> Input <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary> /// <summary> Input <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
[SerializeField] public NodePort[] inputs = new NodePort[0]; [SerializeField] public List<NodePort> inputs = new List<NodePort>();
/// <summary> Output <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary> /// <summary> Output <see cref="NodePort"/>s. It is recommended not to modify these at hand. Instead, see <see cref="InputAttribute"/> </summary>
[SerializeField] public NodePort[] outputs = new NodePort[0]; [SerializeField] public NodePort[] outputs = new NodePort[0];
public int InputCount { get { return inputs.Length; } } public int InputCount { get { return inputs.Count; } }
public int OutputCount { get { return outputs.Length; } } public int OutputCount { get { return outputs.Length; } }
/// <summary> Constructor </summary>
protected Node() { protected Node() {
CachePorts(); //Cache the ports at creation time so we don't have to use reflection at runtime CachePorts(); //Cache the ports at creation time so we don't have to use reflection at runtime
}
protected void OnEnable() {
Init(); Init();
} }
@ -33,7 +34,7 @@ public abstract class Node {
public virtual void OnCreateConnection(NodePort from, NodePort to) { } public virtual void OnCreateConnection(NodePort from, NodePort to) { }
public int GetInputId(NodePort input) { public int GetInputId(NodePort input) {
for (int i = 0; i < inputs.Length; i++) { for (int i = 0; i < inputs.Count; i++) {
if (input == inputs[i]) return i; if (input == inputs[i]) return i;
} }
@ -55,7 +56,7 @@ public abstract class Node {
} }
public void ClearConnections() { public void ClearConnections() {
for (int i = 0; i < inputs.Length; i++) { for (int i = 0; i < inputs.Count; i++) {
inputs[i].ClearConnections(); inputs[i].ClearConnections();
} }
for (int i = 0; i < outputs.Length; i++) { for (int i = 0; i < outputs.Length; i++) {
@ -99,7 +100,7 @@ public abstract class Node {
else if (outputAttrib != null) outputPorts.Add(new NodePort(fieldInfo[i].Name, fieldInfo[i].FieldType, this, NodePort.IO.Output)); else if (outputAttrib != null) outputPorts.Add(new NodePort(fieldInfo[i].Name, fieldInfo[i].FieldType, this, NodePort.IO.Output));
} }
inputs = inputPorts.ToArray(); inputs = inputPorts;
outputs = outputPorts.ToArray(); outputs = outputPorts.ToArray();
} }
} }

View File

@ -5,7 +5,8 @@ using System;
/// <summary> Base class for all node graphs </summary> /// <summary> Base class for all node graphs </summary>
[Serializable] [Serializable]
public abstract class NodeGraph : ScriptableObject { public abstract class NodeGraph : ScriptableObject, ISerializationCallbackReceiver {
/// <summary> All nodes in the graph. <para/> /// <summary> All nodes in the graph. <para/>
/// See: <see cref="AddNode{T}"/> </summary> /// See: <see cref="AddNode{T}"/> </summary>
[SerializeField] public List<Node> nodes = new List<Node>(); [SerializeField] public List<Node> nodes = new List<Node>();
@ -15,7 +16,10 @@ public abstract class NodeGraph : ScriptableObject {
} }
public virtual Node AddNode(Type type) { public virtual Node AddNode(Type type) {
Node node = (Node)Activator.CreateInstance(type); Node node = ScriptableObject.CreateInstance(type) as Node;
#if UNITY_EDITOR
UnityEditor.AssetDatabase.AddObjectToAsset(node, this);
#endif
nodes.Add(node); nodes.Add(node);
node.graph = this; node.graph = this;
return node; return node;
@ -32,5 +36,14 @@ public abstract class NodeGraph : ScriptableObject {
public void Clear() { public void Clear() {
nodes.Clear(); nodes.Clear();
} }
public void OnBeforeSerialize() {
}
public void OnAfterDeserialize() {
for (int i = 0; i < nodes.Count; i++) {
nodes[i].graph = this;
}
}
} }

View File

@ -9,9 +9,7 @@ public class NodePort {
public int ConnectionCount { get { return connections.Count; } } public int ConnectionCount { get { return connections.Count; } }
/// <summary> Return the first connection </summary> /// <summary> Return the first connection </summary>
public NodePort Connection { get { return connections.Count > 0 ? connections[0] : null; } } public NodePort Connection { get { return connections.Count > 0 ? connections[0].Port : null; } }
/// <summary> Returns a copy of the connections list </summary>
public List<NodePort> Connections { get { return new List<NodePort>(connections); } }
public IO direction { get { return _direction; } } public IO direction { get { return _direction; } }
/// <summary> Is this port connected to anytihng? </summary> /// <summary> Is this port connected to anytihng? </summary>
@ -20,11 +18,13 @@ public class NodePort {
public bool IsOutput { get { return direction == IO.Output; } } public bool IsOutput { get { return direction == IO.Output; } }
public Node node { get; private set; } public Node node { get; private set; }
public string name { get { return _name; } set { _name = value; } } public string name { get { return _name; } }
public bool enabled { get { return _enabled; } set { _enabled = value; } } public bool enabled { get { return _enabled; } set { _enabled = value; } }
public string id { get { return _id; } }
[SerializeField] private List<NodePort> connections = new List<NodePort>(); [SerializeField] private List<PortConnection> connections = new List<PortConnection>();
[SerializeField] private string asdf;
[SerializeField] private string _id;
[SerializeField] public Type type; [SerializeField] public Type type;
[SerializeField] private string _name; [SerializeField] private string _name;
[SerializeField] private bool _enabled = true; [SerializeField] private bool _enabled = true;
@ -35,39 +35,75 @@ public class NodePort {
this.type = type; this.type = type;
this.node = node; this.node = node;
_direction = direction; _direction = direction;
_id = node.GetInstanceID() + _name;
} }
/// <summary> Connect this <see cref="NodePort"/> to another </summary> /// <summary> Connect this <see cref="NodePort"/> to another </summary>
/// <param name="port">The <see cref="NodePort"/> to connect to</param> /// <param name="port">The <see cref="NodePort"/> to connect to</param>
public void Connect(NodePort port) { public void Connect(NodePort port) {
if (connections == null) connections = new List<NodePort>(); if (connections == null) connections = new List<PortConnection>();
if (port == null) { Debug.LogWarning("Cannot connect to null port"); return; } if (port == null) { Debug.LogWarning("Cannot connect to null port"); return; }
if (port == this) { Debug.LogWarning("Attempting to connect port to self."); return; } if (port == this) { Debug.LogWarning("Attempting to connect port to self."); return; }
if (connections.Contains(port)) { Debug.LogWarning("Port already connected. "); return; } if (IsConnectedTo(port)) { Debug.LogWarning("Port already connected. "); return; }
if (direction == port.direction) { Debug.LogWarning("Cannot connect two " + (direction == IO.Input ? "input" : "output") + " connections"); return; } if (direction == port.direction) { Debug.LogWarning("Cannot connect two " + (direction == IO.Input ? "input" : "output") + " connections"); return; }
connections.Add(port); connections.Add(new PortConnection(port));
port.connections.Add(this); if (port.connections == null) port.connections = new List<PortConnection>();
port.connections.Add(new PortConnection(this));
node.OnCreateConnection(this, port); node.OnCreateConnection(this, port);
port.node.OnCreateConnection(this, port); port.node.OnCreateConnection(this, port);
} }
public NodePort GetConnection(int i) { public NodePort GetConnection(int i) {
return connections[i]; return connections[i].Port;
} }
public bool IsConnectedTo(NodePort port) { public bool IsConnectedTo(NodePort port) {
return connections.Contains(port); for (int i = 0; i < connections.Count; i++) {
if (connections[i].Port == port) return true;
}
return false;
} }
public void Disconnect(NodePort port) { public void Disconnect(NodePort port) {
connections.Remove(port); for (int i = 0; i < connections.Count; i++) {
port.connections.Remove(this); if (connections[i].Port == port) {
connections.RemoveAt(i);
}
}
for (int i = 0; i < port.connections.Count; i++) {
if (port.connections[i].Port == this) {
port.connections.RemoveAt(i);
}
}
} }
public void ClearConnections() { public void ClearConnections() {
for (int i = 0; i < connections.Count; i++) { for (int i = 0; i < connections.Count; i++) {
connections[i].connections.Remove(this); Disconnect(connections[i].Port);
}
}
[Serializable]
public class PortConnection {
[SerializeField] public Node node;
[SerializeField] public string portID;
public NodePort Port { get { return port != null ? port : port = GetPort(); } }
[NonSerialized] private NodePort port;
public PortConnection(NodePort port) {
this.port = port;
node = port.node;
portID = port.id;
}
private NodePort GetPort() {
for (int i = 0; i < node.OutputCount; i++) {
if (node.outputs[i].id == portID) return node.outputs[i];
}
for (int i = 0; i < node.InputCount; i++) {
if (node.inputs[i].id == portID) return node.inputs[i];
}
return null;
} }
connections.Clear();
} }
} }