diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 76197a5..740d69e 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -159,12 +159,15 @@ namespace XNodeEditor { hoveredPort = null; } + //Save guiColor so we can revert it + Color guiColor = GUI.color; for (int n = 0; n < graph.nodes.Count; n++) { 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 = GetNodeEditor(node.GetType()); + NodeEditor nodeEditor = GetNodeEditor(nodeType); nodeEditor.target = node; nodeEditor.serializedObject = new SerializedObject(node); NodeEditor.portPositions = new Dictionary(); @@ -175,7 +178,9 @@ namespace XNodeEditor { GUILayout.BeginArea(new Rect(nodePos, new Vector2(nodeEditor.GetWidth(), 4000))); GUIStyle style = NodeEditorResources.styles.nodeBody; + if (nodeTint.ContainsKey(nodeType)) GUI.color = nodeTint[nodeType]; GUILayout.BeginVertical(new GUIStyle(style)); + GUI.color = guiColor; EditorGUI.BeginChangeCheck(); //Draw node contents diff --git a/Scripts/Editor/NodeEditorGUILayout.cs b/Scripts/Editor/NodeEditorGUILayout.cs index 0062ac1..cfb42e7 100644 --- a/Scripts/Editor/NodeEditorGUILayout.cs +++ b/Scripts/Editor/NodeEditorGUILayout.cs @@ -42,7 +42,9 @@ namespace XNodeEditor { rect.size = new Vector2(16, 16); - DrawPortHandle(rect, port.ValueType); + 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); // Register the handle position Vector2 portPos = rect.center; @@ -66,7 +68,9 @@ namespace XNodeEditor { else if (port.direction == NodePort.IO.Output) rect.position = rect.position + new Vector2(rect.width, 0); rect.size = new Vector2(16, 16); - DrawPortHandle(rect, port.ValueType); + 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); // Register the handle position Vector2 portPos = rect.center; @@ -74,9 +78,9 @@ namespace XNodeEditor { else NodeEditor.portPositions.Add(port, portPos); } - private static void DrawPortHandle(Rect rect, Type type) { + private static void DrawPortHandle(Rect rect, Type type, Color backgroundColor) { Color col = GUI.color; - GUI.color = new Color32(90, 97, 105, 255); + GUI.color = backgroundColor; GUI.DrawTexture(rect, NodeEditorResources.dotOuter); GUI.color = NodeEditorPreferences.GetTypeColor(type); GUI.DrawTexture(rect, NodeEditorResources.dot); diff --git a/Scripts/Editor/NodeEditorReflection.cs b/Scripts/Editor/NodeEditorReflection.cs index 99448ae..861c764 100644 --- a/Scripts/Editor/NodeEditorReflection.cs +++ b/Scripts/Editor/NodeEditorReflection.cs @@ -2,14 +2,20 @@ using System.Collections.Generic; using System.Linq; using System.Reflection; +using UnityEngine; using XNode; namespace XNodeEditor { /// Contains reflection-related info public partial class NodeEditorWindow { + /// Custom node editors defined with [CustomNodeInspector] [NonSerialized] private static Dictionary customNodeEditor; + /// Custom node tint colors defined with [NodeColor(r, g, b)] + public static Dictionary nodeTint { get { return _nodeTint != null ? _nodeTint : _nodeTint = GetNodeTint(); } } + [NonSerialized] private static Dictionary _nodeTint; + /// All available node types public static Type[] nodeTypes { get { return _nodeTypes != null ? _nodeTypes : _nodeTypes = GetNodeTypes(); } } - [NonSerialized] private static Type[] _nodeTypes = null; + [NonSerialized] private static Type[] _nodeTypes = null; public static NodeEditor GetNodeEditor(Type node) { if (customNodeEditor == null) CacheCustomNodeEditors(); @@ -36,6 +42,17 @@ namespace XNodeEditor { } } + public static Dictionary GetNodeTint() { + Dictionary tints = new Dictionary(); + for (int i = 0; i < nodeTypes.Length; i++) { + var attribs = nodeTypes[i].GetCustomAttributes(typeof(Node.NodeTint), true); + if (attribs == null || attribs.Length == 0) continue; + Node.NodeTint attrib = attribs[0] as Node.NodeTint; + tints.Add(nodeTypes[i], attrib.color); + } + return tints; + } + public static Type[] GetDerivedTypes(Type baseType) { //Get all classes deriving from baseType via reflection Assembly assembly = Assembly.GetAssembly(baseType); diff --git a/Scripts/Node.cs b/Scripts/Node.cs index d7592a3..9fb9297 100644 --- a/Scripts/Node.cs +++ b/Scripts/Node.cs @@ -174,6 +174,24 @@ namespace XNode { } } + [AttributeUsage(AttributeTargets.Class, AllowMultiple = false)] + public class NodeTint : Attribute { + public Color color; + /// Specify a color for this node type + /// Red [0..1] + /// Green [0..1] + /// Blue [0..1] + public NodeTint(float r, float g, float b) { + color = new Color(r, g, b); + } + + /// Specify a color for this node type + /// HEX color value + public NodeTint(string hex) { + ColorUtility.TryParseHtmlString(hex, out color); + } + } + [Serializable] private class NodePortDictionary : Dictionary, ISerializationCallbackReceiver { [SerializeField] private List keys = new List(); [SerializeField] private List values = new List();