1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-04 22:34:54 +08:00

Still haven't found a solution to initializing a static scriptableObject instance

This commit is contained in:
Thor Brigsted 2017-10-10 23:53:27 +02:00
parent d52ecf1931
commit 984364f08f
2 changed files with 20 additions and 59 deletions

View File

@ -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

View File

@ -3,38 +3,20 @@ using System.Collections.Generic;
using UnityEngine;
using System.Reflection;
using System.Linq;
using UnityEditor;
/// <summary> Precaches reflection data in editor so we won't have to do it runtime </summary>
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<NodeDataCache>().FirstOrDefault();
return _instance;
}
}
public static NodeDataCache _instance;
[SerializeField]
private PortDataCache portDataCache = new PortDataCache();
private static NodeDataCache GetInstance() {
NodeDataCache[] ndc = Resources.FindObjectsOfTypeAll<NodeDataCache>();
if (ndc == null || ndc.Length == 0) {
Debug.LogWarning("No NodeDataCache found. Creating.");
NodeDataCache n = ScriptableObject.CreateInstance<NodeDataCache>();
n.BuildCache();
return n;
}
else if (ndc.Length > 1) {
Debug.LogWarning("Multiple NodeDataCaches found.");
}
return ndc[0];
}
private void OnEnable() {
_instance = this;
}
/// <summary> Return port data from cache </summary>
public static void GetPorts(Node node, ref List<NodePort> inputs, ref List<NodePort> outputs) {
//if (_instance == null) Resources.FindObjectsOfTypeAll<NodeDataCache>()[0];
@ -43,16 +25,17 @@ public sealed class NodeDataCache : ScriptableObject {
inputs = new List<NodePort>();
outputs = new List<NodePort>();
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();
}