diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset index b15bf9c..c624f53 100644 --- a/Example/ExampleNodeGraph.asset +++ b/Example/ExampleNodeGraph.asset @@ -11,10 +11,8 @@ MonoBehaviour: m_Script: {fileID: 11500000, guid: a6399826e2c44b447b32a3ed06646162, type: 3} m_Name: ExampleNodeGraph m_EditorClassIdentifier: - nodes: - - {fileID: 114778235353399140} - - {fileID: 114295993062301438} ---- !u!114 &114295993062301438 + nodes: [] +--- !u!114 &114063446055957998 MonoBehaviour: m_ObjectHideFlags: 0 m_PrefabParentObject: {fileID: 0} @@ -27,33 +25,13 @@ MonoBehaviour: m_EditorClassIdentifier: rect: serializedVersion: 2 - x: -330.1812 - y: -108.116974 + x: 0 + y: 0 width: 200 height: 200 inputs: [] outputs: [] - a: 2.01 - b: 0.01 + a: 0 + b: 0 result: 0 mathType: 0 ---- !u!114 &114778235353399140 -MonoBehaviour: - m_ObjectHideFlags: 0 - m_PrefabParentObject: {fileID: 0} - m_PrefabInternal: {fileID: 0} - m_GameObject: {fileID: 0} - m_Enabled: 1 - m_EditorHideFlags: 0 - m_Script: {fileID: 11500000, guid: 98f6f901f0da53142b79277ea3f42518, type: 3} - m_Name: DisplayValue - m_EditorClassIdentifier: - rect: - serializedVersion: 2 - x: 100.23894 - y: -132.62592 - width: 200 - height: 200 - inputs: [] - outputs: [] - value: 0 diff --git a/Scripts/NodeDataCache.cs b/Scripts/NodeDataCache.cs index d5e0e7e..61aa3db 100644 --- a/Scripts/NodeDataCache.cs +++ b/Scripts/NodeDataCache.cs @@ -3,38 +3,20 @@ using System.Collections.Generic; using UnityEngine; using System.Reflection; using System.Linq; +using UnityEditor; /// Precaches reflection data in editor so we won't have to do it runtime public sealed class NodeDataCache : ScriptableObject { - public static NodeDataCache instance { get { - if (!_instance) - _instance = GetInstance(); - return _instance; - } } - private static NodeDataCache _instance; + public static NodeDataCache instance { + get { + if (_instance == null) _instance = Resources.FindObjectsOfTypeAll().FirstOrDefault(); + return _instance; + } + } + public static NodeDataCache _instance; [SerializeField] private PortDataCache portDataCache = new PortDataCache(); - - private static NodeDataCache GetInstance() { - NodeDataCache[] ndc = Resources.FindObjectsOfTypeAll(); - if (ndc == null || ndc.Length == 0) { - Debug.LogWarning("No NodeDataCache found. Creating."); - NodeDataCache n = ScriptableObject.CreateInstance(); - n.BuildCache(); - return n; - } - else if (ndc.Length > 1) { - Debug.LogWarning("Multiple NodeDataCaches found."); - } - return ndc[0]; - } - - private void OnEnable() { - _instance = this; - } - - /// Return port data from cache public static void GetPorts(Node node, ref List inputs, ref List outputs) { //if (_instance == null) Resources.FindObjectsOfTypeAll()[0]; @@ -43,16 +25,17 @@ public sealed class NodeDataCache : ScriptableObject { inputs = new List(); outputs = new List(); 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)); + 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)); } } - + #if UNITY_EDITOR - [UnityEditor.InitializeOnLoadMethod] + [UnityEditor.InitializeOnLoadMethod( )] #endif private static void Init() { + Debug.Log("Init"); instance.BuildCache(); }