mirror of
https://github.com/Siccity/xNode.git
synced 2025-12-20 09:16:01 +08:00
Serialization now works. Node.cs is now a ScriptableObject.
This commit is contained in:
parent
91ccab60db
commit
698f02d716
Binary file not shown.
@ -1,6 +1,6 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 2e27fbb85ccd5994e932fd8a6d34e4b3
|
||||
timeCreated: 1506790922
|
||||
guid: ce9064d2750a8344784c005788637f15
|
||||
timeCreated: 1507333474
|
||||
licenseType: Free
|
||||
NativeFormatImporter:
|
||||
mainObjectFileID: 11400000
|
||||
|
||||
@ -93,6 +93,8 @@ public partial class NodeEditorWindow {
|
||||
//If connection is valid, save it
|
||||
if (draggedOutputTarget != null) {
|
||||
if (graph.nodes.Count != 0) draggedOutput.Connect(draggedOutputTarget);
|
||||
EditorUtility.SetDirty(graph);
|
||||
AssetDatabase.SaveAssets();
|
||||
}
|
||||
//Release dragged connection
|
||||
draggedOutput = null;
|
||||
|
||||
@ -5,23 +5,24 @@ using System;
|
||||
|
||||
/// <summary> Base class for all nodes </summary>
|
||||
[Serializable]
|
||||
public abstract class Node {
|
||||
public abstract class Node : ScriptableObject {
|
||||
|
||||
/// <summary> Name of the node </summary>
|
||||
[SerializeField] public string name = "";
|
||||
[SerializeField] public NodeGraph graph;
|
||||
[NonSerialized] public NodeGraph graph;
|
||||
[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>
|
||||
[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>
|
||||
[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; } }
|
||||
|
||||
/// <summary> Constructor </summary>
|
||||
protected Node() {
|
||||
CachePorts(); //Cache the ports at creation time so we don't have to use reflection at runtime
|
||||
}
|
||||
|
||||
protected void OnEnable() {
|
||||
Init();
|
||||
}
|
||||
|
||||
@ -33,7 +34,7 @@ public abstract class Node {
|
||||
public virtual void OnCreateConnection(NodePort from, NodePort to) { }
|
||||
|
||||
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;
|
||||
|
||||
}
|
||||
@ -55,7 +56,7 @@ public abstract class Node {
|
||||
}
|
||||
|
||||
public void ClearConnections() {
|
||||
for (int i = 0; i < inputs.Length; i++) {
|
||||
for (int i = 0; i < inputs.Count; i++) {
|
||||
inputs[i].ClearConnections();
|
||||
}
|
||||
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));
|
||||
}
|
||||
|
||||
inputs = inputPorts.ToArray();
|
||||
inputs = inputPorts;
|
||||
outputs = outputPorts.ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
@ -5,7 +5,8 @@ using System;
|
||||
|
||||
/// <summary> Base class for all node graphs </summary>
|
||||
[Serializable]
|
||||
public abstract class NodeGraph : ScriptableObject {
|
||||
public abstract class NodeGraph : ScriptableObject, ISerializationCallbackReceiver {
|
||||
|
||||
/// <summary> All nodes in the graph. <para/>
|
||||
/// See: <see cref="AddNode{T}"/> </summary>
|
||||
[SerializeField] public List<Node> nodes = new List<Node>();
|
||||
@ -15,7 +16,10 @@ public abstract class NodeGraph : ScriptableObject {
|
||||
}
|
||||
|
||||
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);
|
||||
node.graph = this;
|
||||
return node;
|
||||
@ -32,5 +36,14 @@ public abstract class NodeGraph : ScriptableObject {
|
||||
public void Clear() {
|
||||
nodes.Clear();
|
||||
}
|
||||
|
||||
public void OnBeforeSerialize() {
|
||||
}
|
||||
|
||||
public void OnAfterDeserialize() {
|
||||
for (int i = 0; i < nodes.Count; i++) {
|
||||
nodes[i].graph = this;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -9,9 +9,7 @@ public class NodePort {
|
||||
|
||||
public int ConnectionCount { get { return connections.Count; } }
|
||||
/// <summary> Return the first connection </summary>
|
||||
public NodePort Connection { get { return connections.Count > 0 ? connections[0] : null; } }
|
||||
/// <summary> Returns a copy of the connections list </summary>
|
||||
public List<NodePort> Connections { get { return new List<NodePort>(connections); } }
|
||||
public NodePort Connection { get { return connections.Count > 0 ? connections[0].Port : null; } }
|
||||
|
||||
public IO direction { get { return _direction; } }
|
||||
/// <summary> Is this port connected to anytihng? </summary>
|
||||
@ -20,11 +18,13 @@ public class NodePort {
|
||||
public bool IsOutput { get { return direction == IO.Output; } }
|
||||
|
||||
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 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] private string _name;
|
||||
[SerializeField] private bool _enabled = true;
|
||||
@ -35,39 +35,75 @@ public class NodePort {
|
||||
this.type = type;
|
||||
this.node = node;
|
||||
_direction = direction;
|
||||
_id = node.GetInstanceID() + _name;
|
||||
}
|
||||
|
||||
/// <summary> Connect this <see cref="NodePort"/> to another </summary>
|
||||
/// <param name="port">The <see cref="NodePort"/> to connect to</param>
|
||||
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 == 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; }
|
||||
connections.Add(port);
|
||||
port.connections.Add(this);
|
||||
connections.Add(new PortConnection(port));
|
||||
if (port.connections == null) port.connections = new List<PortConnection>();
|
||||
port.connections.Add(new PortConnection(this));
|
||||
node.OnCreateConnection(this, port);
|
||||
port.node.OnCreateConnection(this, port);
|
||||
}
|
||||
|
||||
public NodePort GetConnection(int i) {
|
||||
return connections[i];
|
||||
return connections[i].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) {
|
||||
connections.Remove(port);
|
||||
port.connections.Remove(this);
|
||||
for (int i = 0; i < connections.Count; i++) {
|
||||
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() {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user