From e6f5ac2d479f0b557afad4befd7ad2853f53ad08 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Tue, 11 Dec 2018 01:21:19 +0100 Subject: [PATCH] Attempting to fix #81 in a cleaner way. Still not perfect --- Scripts/Node.cs | 5 +++++ Scripts/NodeGraph.cs | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Scripts/Node.cs b/Scripts/Node.cs index f27af6a..bd28553 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -60,7 +60,12 @@ namespace XNode { /// It is recommended not to modify these at hand. Instead, see and [SerializeField] private NodePortDictionary ports = new NodePortDictionary(); + /// Used during node instantiation to fix null/misconfigured graph during OnEnable/Init. Set it before instantiating a node. Will automatically be unset during OnEnable + public static NodeGraph graphHotfix; + protected void OnEnable() { + graph = graphHotfix; + graphHotfix = null; UpdateStaticPorts(); Init(); } diff --git a/Scripts/NodeGraph.cs b/Scripts/NodeGraph.cs index d6d766c..6b95fc2 100644 --- a/Scripts/NodeGraph.cs +++ b/Scripts/NodeGraph.cs @@ -18,18 +18,20 @@ namespace XNode { /// Add a node to the graph by type public virtual Node AddNode(Type type) { + Node.graphHotfix = this; Node node = ScriptableObject.CreateInstance(type) as Node; - nodes.Add(node); node.graph = this; + nodes.Add(node); return node; } /// Creates a copy of the original node in the graph public virtual Node CopyNode(Node original) { + Node.graphHotfix = this; Node node = ScriptableObject.Instantiate(original); + node.graph = this; node.ClearConnections(); nodes.Add(node); - node.graph = this; return node; } @@ -58,6 +60,7 @@ namespace XNode { // Instantiate all nodes inside the graph for (int i = 0; i < nodes.Count; i++) { if (nodes[i] == null) continue; + Node.graphHotfix = graph; Node node = Instantiate(nodes[i]) as Node; node.graph = graph; graph.nodes[i] = node;