From 842101720e3c39e35ddb0f35da6d845086a486cb Mon Sep 17 00:00:00 2001 From: Simon Rodriguez Date: Fri, 21 Dec 2018 13:39:52 +0100 Subject: [PATCH 1/2] Changed so Dictionary now uses Type as key. Uses PrettyName less. Also changed to TryGetValue instead of ContainsKey in GetTypeColor() --- Scripts/Editor/NodeEditorPreferences.cs | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs index 210a619..35c39a1 100644 --- a/Scripts/Editor/NodeEditorPreferences.cs +++ b/Scripts/Editor/NodeEditorPreferences.cs @@ -12,7 +12,7 @@ namespace XNodeEditor { /// The last key we checked. This should be the one we modify private static string lastKey = "xNode.Settings"; - private static Dictionary typeColors = new Dictionary(); + private static Dictionary typeColors = new Dictionary(); private static Dictionary settings = new Dictionary(); [System.Serializable] @@ -134,15 +134,16 @@ namespace XNodeEditor { EditorGUILayout.LabelField("Types", EditorStyles.boldLabel); //Display type colors. Save them if they are edited by the user - List typeColorKeys = new List(typeColors.Keys); - foreach (string typeColorKey in typeColorKeys) { - Color col = typeColors[typeColorKey]; + foreach (var typeColor in typeColors) { + Type type = typeColor.Key; + string typeColorKey = NodeEditorUtilities.PrettyName(type); + Color col = typeColor.Value; EditorGUI.BeginChangeCheck(); EditorGUILayout.BeginHorizontal(); col = EditorGUILayout.ColorField(typeColorKey, col); EditorGUILayout.EndHorizontal(); if (EditorGUI.EndChangeCheck()) { - typeColors[typeColorKey] = col; + typeColors[type] = col; if (settings.typeColors.ContainsKey(typeColorKey)) settings.typeColors[typeColorKey] = col; else settings.typeColors.Add(typeColorKey, col); SavePrefs(typeColorKey, settings); @@ -165,7 +166,7 @@ namespace XNodeEditor { public static void ResetPrefs() { if (EditorPrefs.HasKey(lastKey)) EditorPrefs.DeleteKey(lastKey); if (settings.ContainsKey(lastKey)) settings.Remove(lastKey); - typeColors = new Dictionary(); + typeColors = new Dictionary(); VerifyLoaded(); NodeEditorWindow.RepaintAll(); } @@ -184,19 +185,21 @@ namespace XNodeEditor { public static Color GetTypeColor(System.Type type) { VerifyLoaded(); if (type == null) return Color.gray; - string typeName = type.PrettyName(); - if (!typeColors.ContainsKey(typeName)) { - if (settings[lastKey].typeColors.ContainsKey(typeName)) typeColors.Add(typeName, settings[lastKey].typeColors[typeName]); + Color col; + if (!typeColors.TryGetValue(type, out col)) { + string typeName = type.PrettyName(); + if (settings[lastKey].typeColors.ContainsKey(typeName)) typeColors.Add(type, settings[lastKey].typeColors[typeName]); else { #if UNITY_5_4_OR_NEWER UnityEngine.Random.InitState(typeName.GetHashCode()); #else UnityEngine.Random.seed = typeName.GetHashCode(); #endif - typeColors.Add(typeName, new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value)); + col = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value); + typeColors.Add(type, col); } } - return typeColors[typeName]; + return col; } } } \ No newline at end of file From 8d445fa3f49e58ed29f2cf6fa0143e7a06e690b5 Mon Sep 17 00:00:00 2001 From: Thor Brigsted Date: Thu, 27 Dec 2018 02:47:15 +0100 Subject: [PATCH 2/2] Fixed formatting --- Scripts/Editor/NodeEditorPreferences.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs index 35c39a1..fd5ddfd 100644 --- a/Scripts/Editor/NodeEditorPreferences.cs +++ b/Scripts/Editor/NodeEditorPreferences.cs @@ -135,9 +135,9 @@ namespace XNodeEditor { //Display type colors. Save them if they are edited by the user foreach (var typeColor in typeColors) { - Type type = typeColor.Key; - string typeColorKey = NodeEditorUtilities.PrettyName(type); - Color col = typeColor.Value; + Type type = typeColor.Key; + string typeColorKey = NodeEditorUtilities.PrettyName(type); + Color col = typeColor.Value; EditorGUI.BeginChangeCheck(); EditorGUILayout.BeginHorizontal(); col = EditorGUILayout.ColorField(typeColorKey, col); @@ -185,9 +185,9 @@ namespace XNodeEditor { public static Color GetTypeColor(System.Type type) { VerifyLoaded(); if (type == null) return Color.gray; - Color col; + Color col; if (!typeColors.TryGetValue(type, out col)) { - string typeName = type.PrettyName(); + string typeName = type.PrettyName(); if (settings[lastKey].typeColors.ContainsKey(typeName)) typeColors.Add(type, settings[lastKey].typeColors[typeName]); else { #if UNITY_5_4_OR_NEWER @@ -195,8 +195,8 @@ namespace XNodeEditor { #else UnityEngine.Random.seed = typeName.GetHashCode(); #endif - col = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value); - typeColors.Add(type, col); + col = new Color(UnityEngine.Random.value, UnityEngine.Random.value, UnityEngine.Random.value); + typeColors.Add(type, col); } } return col;