diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index 0a686f3..5d093a7 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -9,12 +9,10 @@ namespace XNodeEditor { /// Base class to derive custom Node editors from. Use this to create your own custom inspectors and editors for your nodes. [CustomNodeEditor(typeof(Node))] - public class NodeEditor : XNodeInternal.NodeEditorBase { + public class NodeEditor : XNodeInternal.NodeEditorBase { /// Fires every whenever a node was modified through the editor public static Action onUpdateNode; - public Node target; - public SerializedObject serializedObject; public static Dictionary portPositions; /// Draws the node GUI. diff --git a/Scripts/Editor/NodeEditorBase.cs b/Scripts/Editor/NodeEditorBase.cs index 3c31777..4d6551e 100644 --- a/Scripts/Editor/NodeEditorBase.cs +++ b/Scripts/Editor/NodeEditorBase.cs @@ -3,16 +3,28 @@ using System.Collections; using System.Collections.Generic; using System.Reflection; using UnityEngine; +using UnityEditor; using XNode; using XNodeEditor; namespace XNodeInternal { /// Handles caching of custom editor classes and their target types. Accessible with GetEditor(Type type) - public class NodeEditorBase where A : Attribute, NodeEditorBase.INodeEditorAttrib where T : class { + public class NodeEditorBase where A : Attribute, NodeEditorBase.INodeEditorAttrib where T : NodeEditorBase where K : ScriptableObject { /// Custom editors defined with [CustomNodeEditor] private static Dictionary editors; + public K target; + public SerializedObject serializedObject; - public static T GetEditor(Type type) { + public static T GetEditor(K target) { + if (target == null) return null; + Type type = target.GetType(); + T editor = GetEditor(type); + editor.target = target; + editor.serializedObject = new SerializedObject(target); + return editor; + } + + private static T GetEditor(Type type) { if (type == null) return null; if (editors == null) CacheCustomEditors(); if (editors.ContainsKey(type)) return editors[type]; diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 97c3afb..c135401 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -13,9 +13,7 @@ namespace XNodeEditor { Event e = Event.current; Matrix4x4 m = GUI.matrix; if (graph == null) return; - currentGraphEditor = NodeGraphEditor.GetEditor(graph.GetType()); - currentGraphEditor.target = graph; - currentGraphEditor.serializedObject = new SerializedObject(graph); + currentGraphEditor = NodeGraphEditor.GetEditor(graph); Controls(); @@ -162,7 +160,8 @@ namespace XNodeEditor { if (!input.IsConnectedTo(output)) input.Connect(output); if (!_portConnectionPoints.ContainsKey(input)) continue; Vector2 to = _portConnectionPoints[input].center; - DrawConnection(from, to, NodeEditorPreferences.GetTypeColor(output.ValueType)); + Color connectionColor = currentGraphEditor.GetTypeColor(output.ValueType); + DrawConnection(from, to, connectionColor); } } } @@ -198,11 +197,8 @@ namespace XNodeEditor { while (graph.nodes[n] == null) graph.nodes.RemoveAt(n); if (n >= graph.nodes.Count) return; Node node = graph.nodes[n]; - Type nodeType = node.GetType(); - NodeEditor nodeEditor = NodeEditor.GetEditor(nodeType); - nodeEditor.target = node; - nodeEditor.serializedObject = new SerializedObject(node); + NodeEditor nodeEditor = NodeEditor.GetEditor(node); NodeEditor.portPositions = new Dictionary(); //Get node position diff --git a/Scripts/Editor/NodeEditorGUILayout.cs b/Scripts/Editor/NodeEditorGUILayout.cs index 2740fef..e01e50b 100644 --- a/Scripts/Editor/NodeEditorGUILayout.cs +++ b/Scripts/Editor/NodeEditorGUILayout.cs @@ -93,7 +93,8 @@ namespace XNodeEditor { Color backgroundColor = new Color32(90, 97, 105, 255); if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()]; - DrawPortHandle(rect, port.ValueType, backgroundColor); + Color col = NodeGraphEditor.GetEditor(port.node.graph).GetTypeColor(port.ValueType); + DrawPortHandle(rect, backgroundColor, col); // Register the handle position Vector2 portPos = rect.center; @@ -119,7 +120,8 @@ namespace XNodeEditor { Color backgroundColor = new Color32(90, 97, 105, 255); if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()]; - DrawPortHandle(rect, port.ValueType, backgroundColor); + Color col = NodeGraphEditor.GetEditor(port.node.graph).GetTypeColor(port.ValueType); + DrawPortHandle(rect, backgroundColor, col); // Register the handle position Vector2 portPos = rect.center; @@ -127,11 +129,11 @@ namespace XNodeEditor { else NodeEditor.portPositions.Add(port, portPos); } - private static void DrawPortHandle(Rect rect, Type type, Color backgroundColor) { + private static void DrawPortHandle(Rect rect, Color backgroundColor, Color typeColor) { Color col = GUI.color; GUI.color = backgroundColor; GUI.DrawTexture(rect, NodeEditorResources.dotOuter); - GUI.color = NodeEditorPreferences.GetTypeColor(type); + GUI.color = typeColor; GUI.DrawTexture(rect, NodeEditorResources.dot); GUI.color = col; } diff --git a/Scripts/Editor/NodeGraphEditor.cs b/Scripts/Editor/NodeGraphEditor.cs index d791f59..2205e84 100644 --- a/Scripts/Editor/NodeGraphEditor.cs +++ b/Scripts/Editor/NodeGraphEditor.cs @@ -8,13 +8,10 @@ using XNode; namespace XNodeEditor { /// Base class to derive custom Node Graph editors from. Use this to override how graphs are drawn in the editor. [CustomNodeGraphEditor(typeof(NodeGraph))] - public class NodeGraphEditor : XNodeInternal.NodeEditorBase { + public class NodeGraphEditor : XNodeInternal.NodeEditorBase { /// Custom node editors defined with [CustomNodeGraphEditor] [NonSerialized] private static Dictionary editors; - public NodeGraph target; - public SerializedObject serializedObject; - public virtual Texture2D GetGridTexture() { return NodeEditorPreferences.gridTexture; } @@ -33,6 +30,10 @@ namespace XNodeEditor { return ObjectNames.NicifyVariableName(type.ToString().Replace('.', '/')); } + public virtual Color GetTypeColor(Type type) { + return NodeEditorPreferences.GetTypeColor(type); + } + [AttributeUsage(AttributeTargets.Class)] public class CustomNodeGraphEditorAttribute : Attribute, INodeEditorAttrib {