mirror of
https://github.com/Siccity/xNode.git
synced 2026-03-26 22:49:02 +08:00
Fixes the clone behaviour so that nodes have the correct graph reference when OnEnable/Init is called during a clone operation
This commit is contained in:
parent
35861d20c6
commit
956d8b1002
@ -63,6 +63,9 @@ namespace XNode {
|
|||||||
protected void OnEnable() {
|
protected void OnEnable() {
|
||||||
UpdateStaticPorts();
|
UpdateStaticPorts();
|
||||||
Init();
|
Init();
|
||||||
|
if (graph != null && !graph.CopyInProgress) {
|
||||||
|
OnEnableOverride();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Update static ports to reflect class fields. This happens automatically on enable. </summary>
|
/// <summary> Update static ports to reflect class fields. This happens automatically on enable. </summary>
|
||||||
@ -73,6 +76,18 @@ namespace XNode {
|
|||||||
/// <summary> Initialize node. Called on creation. </summary>
|
/// <summary> Initialize node. Called on creation. </summary>
|
||||||
protected virtual void Init() { }
|
protected virtual void Init() { }
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initialize node. Called on creation and after clone in the correct order.
|
||||||
|
/// </summary>
|
||||||
|
// Implementation note: This method is called after a node has been instantiated, and also
|
||||||
|
// called directly when cloning a full graph. This simply calls Init(), which cannot be
|
||||||
|
// called from outside, as it is protected, not public, and making it public would break
|
||||||
|
// existing code.
|
||||||
|
public virtual void OnEnableOverride() {
|
||||||
|
Init();
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
/// <summary> Checks all connections for invalid references, and removes them. </summary>
|
||||||
public void VerifyConnections() {
|
public void VerifyConnections() {
|
||||||
foreach (NodePort port in Ports) port.VerifyConnections();
|
foreach (NodePort port in Ports) port.VerifyConnections();
|
||||||
|
|||||||
@ -11,6 +11,11 @@ namespace XNode {
|
|||||||
/// 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>();
|
||||||
|
|
||||||
|
// Non serialized flag to indicate a copy operation is currently underway.
|
||||||
|
// This stops nodes from calling OnEnableOverride before we updated the
|
||||||
|
// graph reference.
|
||||||
|
public bool CopyInProgress { get; protected set; }
|
||||||
|
|
||||||
/// <summary> Add a node to the graph by type </summary>
|
/// <summary> Add a node to the graph by type </summary>
|
||||||
public T AddNode<T>() where T : Node {
|
public T AddNode<T>() where T : Node {
|
||||||
return AddNode(typeof(T)) as T;
|
return AddNode(typeof(T)) as T;
|
||||||
@ -21,16 +26,24 @@ namespace XNode {
|
|||||||
Node node = ScriptableObject.CreateInstance(type) as Node;
|
Node node = ScriptableObject.CreateInstance(type) as Node;
|
||||||
nodes.Add(node);
|
nodes.Add(node);
|
||||||
node.graph = this;
|
node.graph = this;
|
||||||
|
node.OnEnableOverride();
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Creates a copy of the original node in the graph </summary>
|
/// <summary> Creates a copy of the original node in the graph </summary>
|
||||||
public virtual Node CopyNode(Node original) {
|
public virtual Node CopyNode(Node original) {
|
||||||
Node node = ScriptableObject.Instantiate(original);
|
this.CopyInProgress = true;
|
||||||
node.ClearConnections();
|
try {
|
||||||
nodes.Add(node);
|
Node node = ScriptableObject.Instantiate(original);
|
||||||
node.graph = this;
|
node.ClearConnections();
|
||||||
return node;
|
nodes.Add(node);
|
||||||
|
node.graph = this;
|
||||||
|
node.OnEnableOverride();
|
||||||
|
return node;
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
this.CopyInProgress = false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary> Safely remove a node and all its connections </summary>
|
/// <summary> Safely remove a node and all its connections </summary>
|
||||||
@ -55,20 +68,34 @@ namespace XNode {
|
|||||||
public XNode.NodeGraph Copy() {
|
public XNode.NodeGraph Copy() {
|
||||||
// Instantiate a new nodegraph instance
|
// Instantiate a new nodegraph instance
|
||||||
NodeGraph graph = Instantiate(this);
|
NodeGraph graph = Instantiate(this);
|
||||||
// Instantiate all nodes inside the graph
|
graph.CopyInProgress = true;
|
||||||
for (int i = 0; i < nodes.Count; i++) {
|
try {
|
||||||
if (nodes[i] == null) continue;
|
// Instantiate all nodes inside the graph
|
||||||
Node node = Instantiate(nodes[i]) as Node;
|
for (int i = 0; i < nodes.Count; i++) {
|
||||||
node.graph = graph;
|
if (nodes[i] == null) continue;
|
||||||
graph.nodes[i] = node;
|
Node node = Instantiate(nodes[i]) as Node;
|
||||||
}
|
node.graph = graph;
|
||||||
|
graph.nodes[i] = node;
|
||||||
// Redirect all connections
|
|
||||||
for (int i = 0; i < graph.nodes.Count; i++) {
|
|
||||||
if (graph.nodes[i] == null) continue;
|
|
||||||
foreach (NodePort port in graph.nodes[i].Ports) {
|
|
||||||
port.Redirect(nodes, graph.nodes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Redirect all connections
|
||||||
|
for (int i = 0; i < graph.nodes.Count; i++) {
|
||||||
|
if (graph.nodes[i] == null) continue;
|
||||||
|
foreach (NodePort port in graph.nodes[i].Ports) {
|
||||||
|
port.Redirect(nodes, graph.nodes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call the Enable notifier, which was not fired during the
|
||||||
|
// normal instantiation.
|
||||||
|
for (int i = 0; i < graph.nodes.Count; i++) {
|
||||||
|
var node = graph.nodes[i];
|
||||||
|
if (node == null) continue;
|
||||||
|
node.OnEnableOverride();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
finally {
|
||||||
|
graph.CopyInProgress = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return graph;
|
return graph;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user