1
0
mirror of https://github.com/Siccity/xNode.git synced 2026-02-19 00:06:42 +08:00

NodeDataCache changed from ScriptableObject to static class.

Nodes still don't load properly, but we're getting there.
This commit is contained in:
Thor Brigsted 2017-10-12 00:18:40 +02:00
parent d608eaab9a
commit 8ce0d63903
10 changed files with 51 additions and 58 deletions

Binary file not shown.

View File

@ -1,7 +1,7 @@
fileFormatVersion: 2 fileFormatVersion: 2
guid: b21c76861277d8445a55b4691205fd00 guid: 2c7ed5ea36166484a8bcecda71ad8cc8
timeCreated: 1507704049 timeCreated: 1507760054
licenseType: Pro licenseType: Free
NativeFormatImporter: NativeFormatImporter:
mainObjectFileID: 11400000 mainObjectFileID: 11400000
userData: userData:

View File

@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: 65ed9b337b7f0b74ab007799bb407a5e
folderAsset: yes
timeCreated: 1507567488
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

Binary file not shown.

View File

@ -1,9 +0,0 @@
fileFormatVersion: 2
guid: a05ee176e92214f48a94c321dad1614d
timeCreated: 1507703437
licenseType: Pro
NativeFormatImporter:
mainObjectFileID: 11400000
userData:
assetBundleName:
assetBundleVariant:

View File

@ -41,6 +41,7 @@ public static class NodeEditorUtilities {
/// <summary> Return color based on type </summary> /// <summary> Return color based on type </summary>
public static Color GetTypeColor(Type type) { public static Color GetTypeColor(Type type) {
if (type == null) return Color.gray;
UnityEngine.Random.InitState(type.Name.GetHashCode()); UnityEngine.Random.InitState(type.Name.GetHashCode());
return new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value); return new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value);
} }

View File

@ -112,6 +112,6 @@ public abstract class Node : ScriptableObject {
} }
private void GetPorts() { private void GetPorts() {
NodeDataCache.GetPorts(this, ref inputs, ref outputs); NodeDataCache.UpdatePorts(this, inputs, outputs);
} }
} }

View File

@ -6,40 +6,54 @@ using System.Linq;
using UnityEditor; using UnityEditor;
/// <summary> Precaches reflection data in editor so we won't have to do it runtime </summary> /// <summary> Precaches reflection data in editor so we won't have to do it runtime </summary>
public sealed class NodeDataCache : ScriptableObject { public static class NodeDataCache {
public static NodeDataCache instance { get; private set;} private static PortDataCache portDataCache;
public static bool Initialized { get { return instance != null; } } private static bool Initialized { get { return portDataCache != null; } }
[SerializeField] private PortDataCache portDataCache; /// <summary> Checks for invalid and removes them.
/// Checks for missing ports and adds them.
/// Checks for invalid connections and removes them. </summary>
public static void UpdatePorts(Node node, List<NodePort> inputs, List<NodePort> outputs) {
if (!Initialized) BuildCache();
#if UNITY_EDITOR List<NodePort> inputPorts = new List<NodePort>();
[UnityEditor.Callbacks.DidReloadScripts] List<NodePort> outputPorts = new List<NodePort>();
#endif
public static void Initialize() { System.Type nodeType = node.GetType();
if (!Initialized) { inputPorts = new List<NodePort>();
instance = Resources.LoadAll<NodeDataCache>("").FirstOrDefault(); outputPorts = new List<NodePort>();
#if UNITY_EDITOR if (!portDataCache.ContainsKey(nodeType)) return;
instance.BuildCache(); for (int i = 0; i < portDataCache[nodeType].Count; i++) {
#endif if (portDataCache[nodeType][i].direction == NodePort.IO.Input) inputPorts.Add(new NodePort(portDataCache[nodeType][i], node));
else outputPorts.Add(new NodePort(portDataCache[nodeType][i], node));
}
for (int i = inputs.Count-1; i >= 0; i--) {
int index = inputPorts.FindIndex(x => inputs[i].fieldName == x.fieldName);
//If input nodeport does not exist, remove it
if (index == -1) inputs.RemoveAt(i);
//If input nodeport does exist, update it
else inputs[i].type = inputPorts[index].type;
}
for (int i = outputs.Count - 1; i >= 0; i--) {
int index = outputPorts.FindIndex(x => outputs[i].fieldName == x.fieldName);
//If output nodeport does not exist, remove it
if (index == -1) outputs.RemoveAt(i);
//If output nodeport does exist, update it
else outputs[i].type = outputPorts[index].type;
}
//Add
for (int i = 0; i < inputPorts.Count; i++) {
//If inputports contains a new port, add it
if (!inputs.Any(x => x.fieldName == inputPorts[i].fieldName)) inputs.Add(inputPorts[i]);
}
for (int i = 0; i < outputPorts.Count; i++) {
//If inputports contains a new port, add it
if (!outputs.Any(x => x.fieldName == outputPorts[i].fieldName)) outputs.Add(outputPorts[i]);
} }
} }
/// <summary> Return port data from cache </summary> private static void BuildCache() {
public static void GetPorts(Node node, ref List<NodePort> inputs, ref List<NodePort> outputs) {
Initialize();
System.Type nodeType = node.GetType();
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));
}
}
#if UNITY_EDITOR
private void BuildCache() {
portDataCache = new PortDataCache(); portDataCache = new PortDataCache();
System.Type baseType = typeof(Node); System.Type baseType = typeof(Node);
Assembly assembly = Assembly.GetAssembly(baseType); Assembly assembly = Assembly.GetAssembly(baseType);
@ -53,7 +67,7 @@ public sealed class NodeDataCache : ScriptableObject {
} }
} }
private void CachePorts(System.Type nodeType) { private static void CachePorts(System.Type nodeType) {
System.Reflection.FieldInfo[] fieldInfo = nodeType.GetFields(); System.Reflection.FieldInfo[] fieldInfo = nodeType.GetFields();
for (int i = 0; i < fieldInfo.Length; i++) { for (int i = 0; i < fieldInfo.Length; i++) {
@ -71,7 +85,6 @@ public sealed class NodeDataCache : ScriptableObject {
} }
} }
} }
#endif
[System.Serializable] [System.Serializable]
private class PortDataCache : Dictionary<System.Type, List<NodePort>>, ISerializationCallbackReceiver { private class PortDataCache : Dictionary<System.Type, List<NodePort>>, ISerializationCallbackReceiver {

View File

@ -15,7 +15,6 @@ public abstract class NodeGraph : ScriptableObject, ISerializationCallbackReceiv
} }
public virtual Node AddNode(Type type) { public virtual Node AddNode(Type type) {
if (!NodeDataCache.Initialized) NodeDataCache.Initialize();
Node node = ScriptableObject.CreateInstance(type) as Node; Node node = ScriptableObject.CreateInstance(type) as Node;
#if UNITY_EDITOR #if UNITY_EDITOR
if (!Application.isPlaying) { if (!Application.isPlaying) {

View File

@ -19,14 +19,12 @@ public class NodePort {
public bool IsOutput { get { return direction == IO.Output; } } public bool IsOutput { get { return direction == IO.Output; } }
public Node node { get; private set; } public Node node { get; private set; }
public bool enabled { get { return _enabled; } set { _enabled = value; } }
public string fieldName { get { return _fieldName; } } public string fieldName { get { return _fieldName; } }
[SerializeField] private List<PortConnection> connections = new List<PortConnection>();
[SerializeField] private string _fieldName; [SerializeField] private string _fieldName;
[SerializeField] public Type type; [SerializeField] public Type type;
[SerializeField] private bool _enabled = true; [SerializeField] private List<PortConnection> connections = new List<PortConnection>();
[SerializeField] private IO _direction; [SerializeField] private IO _direction;
public NodePort(FieldInfo fieldInfo) { public NodePort(FieldInfo fieldInfo) {
@ -59,7 +57,7 @@ public class NodePort {
} }
} }
public object GetValue() { public object GetValue() {
return node.GetValue(this); return node.GetValue(this);
} }