diff --git a/Scripts/Node.cs b/Scripts/Node.cs index f27af6a..881a297 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -60,7 +60,13 @@ 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() { + if (graphHotfix != null) graph = graphHotfix; + graphHotfix = null; UpdateStaticPorts(); Init(); } @@ -308,4 +314,4 @@ namespace XNode { } } } -} \ No newline at end of file +} 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;