diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset index 16e79ec..b15bf9c 100644 Binary files a/Example/ExampleNodeGraph.asset and b/Example/ExampleNodeGraph.asset differ diff --git a/Resources/NodeDataCache.asset b/Resources/NodeDataCache.asset index d118730..dbef4f7 100644 Binary files a/Resources/NodeDataCache.asset and b/Resources/NodeDataCache.asset differ diff --git a/Scripts/Node.cs b/Scripts/Node.cs index ea0d485..2f5dbf0 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -19,18 +19,11 @@ public abstract class Node : ScriptableObject { public int InputCount { get { return inputs.Count; } } public int OutputCount { get { return outputs.Count; } } - protected Node() { - - GetPorts(); //Cache the ports at creation time so we don't have to use reflection at runtime - } - protected void OnEnable() { - VerifyConnections(); GetPorts(); Init(); } - /// Checks all connections for invalid references, and removes them. public void VerifyConnections() { for (int i = 0; i < InputCount; i++) { @@ -119,6 +112,6 @@ public abstract class Node : ScriptableObject { } private void GetPorts() { - NodeDataCache.GetPorts(this, out inputs, out outputs); + NodeDataCache.GetPorts(this, ref inputs, ref outputs); } } diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index 8608d21..d5e0e7e 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -6,35 +6,43 @@ using System.Linq; /// Precaches reflection data in editor so we won't have to do it runtime public sealed class NodeDataCache : ScriptableObject { - public static NodeDataCache instance { get { return _instance; } } + public static NodeDataCache instance { get { + if (!_instance) + _instance = GetInstance(); + return _instance; + } } private static NodeDataCache _instance; - [SerializeField] + [SerializeField] private PortDataCache portDataCache = new PortDataCache(); - [RuntimeInitializeOnLoadMethod] - private static void InitializeInstance() { - Debug.Log("INIT"); + private static NodeDataCache GetInstance() { NodeDataCache[] ndc = Resources.FindObjectsOfTypeAll(); if (ndc == null || ndc.Length == 0) { Debug.LogWarning("No NodeDataCache found. Creating."); - _instance = ScriptableObject.CreateInstance(); - _instance.BuildCache(); + NodeDataCache n = ScriptableObject.CreateInstance(); + n.BuildCache(); + return n; } else if (ndc.Length > 1) { Debug.LogWarning("Multiple NodeDataCaches found."); } - _instance = ndc[0]; + return ndc[0]; } + private void OnEnable() { + _instance = this; + } + + /// Return port data from cache - public static void GetPorts(Node node, out List inputs, out List outputs) { - if (_instance == null) InitializeInstance(); + public static void GetPorts(Node node, ref List inputs, ref List outputs) { + //if (_instance == null) Resources.FindObjectsOfTypeAll()[0]; System.Type nodeType = node.GetType(); inputs = new List(); outputs = new List(); - if (!_instance.portDataCache.ContainsKey(nodeType)) return; + if (!instance.portDataCache.ContainsKey(nodeType)) return; for (int i = 0; i < _instance.portDataCache[nodeType].Count; i++) { if (_instance.portDataCache[nodeType][i].direction == NodePort.IO.Input) inputs.Add(new NodePort(_instance.portDataCache[nodeType][i], node)); else outputs.Add(new NodePort(_instance.portDataCache[nodeType][i], node));