diff --git a/Example/ExampleNodeGraph.asset b/Example/ExampleNodeGraph.asset index 8e72bce..202aa01 100644 Binary files a/Example/ExampleNodeGraph.asset and b/Example/ExampleNodeGraph.asset differ diff --git a/Scripts/Editor/NodeEditor.cs b/Scripts/Editor/NodeEditor.cs index 1bde7d3..053569c 100644 --- a/Scripts/Editor/NodeEditor.cs +++ b/Scripts/Editor/NodeEditor.cs @@ -83,7 +83,7 @@ public class NodeEditor { Rect handleRect = new Rect(handlePoint.x-8,handlePoint.y-8,16,16); GUI.color = new Color(0.29f, 0.31f, 0.32f); GUI.DrawTexture(handleRect, NodeEditorResources.dotOuter); - GUI.color = NodeEditorUtilities.GetTypeColor(port.type); + GUI.color = NodeEditorPreferences.GetTypeColor(port.type); GUI.DrawTexture(handleRect, NodeEditorResources.dot); GUI.color = col; return handlePoint; diff --git a/Scripts/Editor/NodeEditorAction.cs b/Scripts/Editor/NodeEditorAction.cs index 005efc4..313fbf4 100644 --- a/Scripts/Editor/NodeEditorAction.cs +++ b/Scripts/Editor/NodeEditorAction.cs @@ -133,7 +133,7 @@ public partial class NodeEditorWindow { if (!_portConnectionPoints.ContainsKey(draggedOutput)) return; Vector2 from = _portConnectionPoints[draggedOutput].center; Vector2 to = draggedOutputTarget != null ? portConnectionPoints[draggedOutputTarget].center : WindowToGridPosition(Event.current.mousePosition); - Color col = NodeEditorUtilities.GetTypeColor(draggedOutput.type); + Color col = NodeEditorPreferences.GetTypeColor(draggedOutput.type); col.a = 0.6f; DrawConnection(from, to, col); } diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 4a7e617..a573800 100644 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -131,7 +131,7 @@ public partial class NodeEditorWindow { NodePort input = output.GetConnection(k); if (input == null) return; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return. Vector2 to = _portConnectionPoints[input].center; - DrawConnection(from, to, NodeEditorUtilities.GetTypeColor(output.type)); + DrawConnection(from, to, NodeEditorPreferences.GetTypeColor(output.type)); } } } diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs new file mode 100644 index 0000000..377936f --- /dev/null +++ b/Scripts/Editor/NodeEditorPreferences.cs @@ -0,0 +1,100 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using UnityEditor; +using UnityEngine; + +public static class NodeEditorPreferences { + + /// Have we loaded the prefs yet + private static bool prefsLoaded = false; + + private static Dictionary typeColors; + private static Dictionary generatedTypeColors; + + [PreferenceItem ("Node Editor")] + private static void PreferencesGUI () { + if (!prefsLoaded) LoadPrefs(); + EditorGUILayout.LabelField("Type colors", EditorStyles.boldLabel); + + string[] typeKeys = new string[typeColors.Count]; + typeColors.Keys.CopyTo(typeKeys, 0); + + + foreach (var key in typeKeys) { + EditorGUILayout.BeginHorizontal (); + Color col = typeColors[key]; + col = EditorGUILayout.ColorField (key, col); + typeColors[key] = col; + if (!GUILayout.Toggle(true, "")) { + typeColors.Remove(key); + SavePrefs(); + } + EditorGUILayout.EndHorizontal (); + } + if(GUI.changed) { + SavePrefs(); + } + + + string[] generatedTypeKeys = new string[generatedTypeColors.Count]; + generatedTypeColors.Keys.CopyTo(generatedTypeKeys,0); + foreach (var key in generatedTypeKeys) { + EditorGUILayout.BeginHorizontal (); + Color col = generatedTypeColors[key]; + EditorGUI.BeginDisabledGroup(true); + col = EditorGUILayout.ColorField (key, col); + EditorGUI.EndDisabledGroup(); + if (GUILayout.Toggle(false, "")) { + typeColors.Add(key,generatedTypeColors[key]); + generatedTypeColors.Remove(key); + SavePrefs(); + } + EditorGUILayout.EndHorizontal (); + } + } + + private static void LoadPrefs() { + generatedTypeColors = new Dictionary(); + typeColors = GetTypeColors(); + prefsLoaded = true; + } + + private static void SavePrefs() { + if (!prefsLoaded) return; + string s = ""; + foreach (var item in typeColors) { + s+= item.Key+","+ColorUtility.ToHtmlStringRGB(item.Value)+ ","; + } + EditorPrefs.SetString("unec_typecolors",s); + } + + public static void SetDefaultTypeColors () { + EditorPrefs.SetString ("unec_typecolors", "int,2568CA,string,CE743A,bool,00FF00"); + } + + public static Dictionary GetTypeColors () { + if (prefsLoaded) return typeColors; + if (!EditorPrefs.HasKey ("unec_typecolors")) SetDefaultTypeColors (); + string[] data = EditorPrefs.GetString("unec_typecolors").Split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries); + Dictionary dict = new Dictionary (); + for (int i = 0; i < data.Length; i += 2) { + Color col; + if (ColorUtility.TryParseHtmlString("#"+data[i+1], out col)) { + dict.Add(data[i], col); + } + } + return dict; + } + + /// Return color based on type + public static Color GetTypeColor(System.Type type) { + if (!prefsLoaded) LoadPrefs(); + if (type == null) return Color.gray; + if (typeColors.ContainsKey(type.Name)) return typeColors[type.Name]; + if (generatedTypeColors.ContainsKey(type.Name)) return generatedTypeColors[type.Name]; + UnityEngine.Random.InitState (type.Name.GetHashCode ()); + generatedTypeColors.Add(type.Name, new Color (UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value)); + return generatedTypeColors[type.Name]; + } +} \ No newline at end of file diff --git a/Scripts/Editor/NodeEditorPreferences.cs.meta b/Scripts/Editor/NodeEditorPreferences.cs.meta new file mode 100644 index 0000000..156543b --- /dev/null +++ b/Scripts/Editor/NodeEditorPreferences.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: 6b1f47e387a6f714c9f2ff82a6888c85 +timeCreated: 1507920216 +licenseType: Free +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Scripts/Editor/NodeEditorUtilities.cs b/Scripts/Editor/NodeEditorUtilities.cs index 78ee295..4d6f904 100644 --- a/Scripts/Editor/NodeEditorUtilities.cs +++ b/Scripts/Editor/NodeEditorUtilities.cs @@ -39,13 +39,6 @@ public static class NodeEditorUtilities { return char.ToUpper(s[0]) + s.Substring(1); } - /// Return color based on type - public static Color GetTypeColor(Type type) { - if (type == null) return Color.gray; - UnityEngine.Random.InitState(type.Name.GetHashCode()); - return new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value); - } - /// Returns true if this can be casted to public static bool IsCastableTo(this Type from, Type to) { if (to.IsAssignableFrom(from)) return true;