diff --git a/Scripts/Editor/NodeEditorPreferences.cs b/Scripts/Editor/NodeEditorPreferences.cs
index 59c1285..a34d678 100644
--- a/Scripts/Editor/NodeEditorPreferences.cs
+++ b/Scripts/Editor/NodeEditorPreferences.cs
@@ -9,7 +9,7 @@ namespace XNodeEditor {
public static Texture2D gridTexture {
get {
VerifyLoaded();
- if (_gridTexture == null) _gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor);
+ if (_gridTexture == null) _gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
return _gridTexture;
}
}
@@ -17,25 +17,47 @@ namespace XNodeEditor {
public static Texture2D crossTexture {
get {
VerifyLoaded();
- if (_crossTexture == null) _crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor);
+ if (_crossTexture == null) _crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
return _crossTexture;
}
}
private static Texture2D _crossTexture;
- /// Have we loaded the prefs yet
- private static bool prefsLoaded = false;
-
/// TypeColors requested by the editor
+ public static bool gridSnap { get { VerifyLoaded(); return settings.gridSnap; } }
+
private static Dictionary typeColors = new Dictionary();
- /// TypeColors available in EditorPrefs
- private static Dictionary prefsTypeColors = new Dictionary();
- public static bool gridSnap { get { VerifyLoaded(); return _gridSnap; } }
- private static bool _gridSnap = true;
- public static Color gridLineColor { get { VerifyLoaded(); return _gridLineColor; } }
- private static Color _gridLineColor;
- public static Color gridBgColor { get { VerifyLoaded(); return _gridBgColor; } }
- private static Color _gridBgColor;
+ private static Settings settings;
+
+ [System.Serializable]
+ private class Settings : ISerializationCallbackReceiver {
+ public Color32 gridLineColor = new Color(0.45f, 0.45f, 0.45f);
+ public Color32 gridBgColor = new Color(0.18f, 0.18f, 0.18f);
+ public bool gridSnap = true;
+ public string typeColorsData = "";
+ public Dictionary typeColors = new Dictionary();
+
+ public void OnAfterDeserialize() {
+ // Deserialize typeColorsData
+ typeColors = new Dictionary();
+ string[] data = typeColorsData.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
+ for (int i = 0; i < data.Length; i += 2) {
+ Color col;
+ if (ColorUtility.TryParseHtmlString("#" + data[i + 1], out col)) {
+ typeColors.Add(data[i], col);
+ }
+ }
+ }
+
+ public void OnBeforeSerialize() {
+ // Serialize typeColors
+ typeColorsData = "";
+ foreach (var item in typeColors) {
+ typeColorsData += item.Key + "," + ColorUtility.ToHtmlStringRGB(item.Value) + ",";
+ }
+ }
+ }
+
[PreferenceItem("Node Editor")]
private static void PreferencesGUI() {
VerifyLoaded();
@@ -50,15 +72,14 @@ namespace XNodeEditor {
private static void GridSettingsGUI() {
//Label
EditorGUILayout.LabelField("Grid", EditorStyles.boldLabel);
- _gridSnap = EditorGUILayout.Toggle("Snap", _gridSnap);
+ settings.gridSnap = EditorGUILayout.Toggle("Snap", settings.gridSnap);
- //EditorGUIUtility.labelWidth = 30;
- _gridLineColor = EditorGUILayout.ColorField("Color", _gridLineColor);
- _gridBgColor = EditorGUILayout.ColorField(" ", _gridBgColor);
+ settings.gridLineColor = EditorGUILayout.ColorField("Color", settings.gridLineColor);
+ settings.gridBgColor = EditorGUILayout.ColorField(" ", settings.gridBgColor);
if (GUI.changed) {
SavePrefs();
- _gridTexture = NodeEditorResources.GenerateGridTexture(_gridLineColor, _gridBgColor);
- _crossTexture = NodeEditorResources.GenerateCrossTexture(_gridLineColor);
+ _gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
+ _crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
NodeEditorWindow.RepaintAll();
}
EditorGUILayout.Space();
@@ -78,74 +99,42 @@ namespace XNodeEditor {
EditorGUILayout.EndHorizontal();
if (EditorGUI.EndChangeCheck()) {
typeColors[key] = col;
- SaveTypeColor(key, col);
+ if (settings.typeColors.ContainsKey(key)) settings.typeColors[key] = col;
+ else settings.typeColors.Add(key, col);
+ SavePrefs();
NodeEditorWindow.RepaintAll();
}
}
}
- private static void LoadPrefs() {
- prefsTypeColors = LoadTypeColors();
+ private static Settings LoadPrefs() {
+ // Remove obsolete editorprefs
+ if (EditorPrefs.HasKey("xnode_typecolors")) EditorPrefs.DeleteKey("xnode_typecolors");
+ if (EditorPrefs.HasKey("xnode_gridcolor0")) EditorPrefs.DeleteKey("xnode_gridcolor0");
+ if (EditorPrefs.HasKey("xnode_gridcolor1")) EditorPrefs.DeleteKey("xnode_gridcolor1");
+ if (EditorPrefs.HasKey("xnode_gridsnap")) EditorPrefs.DeleteKey("xnode_gridcolor1");
- //Load grid colors
- if (!EditorPrefs.HasKey("xnode_gridcolor0")) EditorPrefs.SetString("xnode_gridcolor0", ColorUtility.ToHtmlStringRGB(new Color(0.45f, 0.45f, 0.45f)));
- ColorUtility.TryParseHtmlString("#" + EditorPrefs.GetString("xnode_gridcolor0"), out _gridLineColor);
- if (!EditorPrefs.HasKey("xnode_gridcolor1")) EditorPrefs.SetString("xnode_gridcolor1", ColorUtility.ToHtmlStringRGB(new Color(0.18f, 0.18f, 0.18f)));
- ColorUtility.TryParseHtmlString("#" + EditorPrefs.GetString("xnode_gridcolor1"), out _gridBgColor);
-
- //Load snap option
- if (EditorPrefs.HasKey("xnode_gridsnap")) _gridSnap = EditorPrefs.GetBool("xnode_gridsnap");
-
- NodeEditorWindow.RepaintAll();
- prefsLoaded = true;
- }
-
- /// Get Type Colors from EditorPrefs. Colors are saved as CSV in pairs of two hexcolor/name
- public static Dictionary LoadTypeColors() {
- //Load type colors
- Dictionary result = new Dictionary();
-
- if (!EditorPrefs.HasKey("xnode_typecolors")) EditorPrefs.SetString("xnode_typecolors", "");
- string[] data = EditorPrefs.GetString("xnode_typecolors").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
- for (int i = 0; i < data.Length; i += 2) {
- Color col;
- if (ColorUtility.TryParseHtmlString("#" + data[i + 1], out col)) {
- result.Add(data[i], col);
- }
- }
- return result;
- }
-
- /// Get Type Colors from EditorPrefs. Colors are saved as CSV in pairs of two hexcolor/name ""
- public static Dictionary SaveTypeColor(string typeName, Color col) {
- //Load type colors
- Dictionary result = LoadTypeColors();
- if (result.ContainsKey(typeName)) result[typeName] = col;
- else result.Add(typeName, col);
- string s = "";
- foreach (var item in result) {
- s += item.Key + "," + ColorUtility.ToHtmlStringRGB(item.Value) + ",";
- }
- EditorPrefs.SetString("xnode_typecolors", s);
- return result;
+ if (!EditorPrefs.HasKey("xNode.Settings")) EditorPrefs.SetString("xNode.Settings", JsonUtility.ToJson(new Settings()));
+ return JsonUtility.FromJson(EditorPrefs.GetString("xNode.Settings"));
}
/// Delete all prefs
public static void ResetPrefs() {
- if (EditorPrefs.HasKey("xnode_typecolors")) EditorPrefs.DeleteKey("xnode_typecolors");
- if (EditorPrefs.HasKey("xnode_gridcolor0")) EditorPrefs.DeleteKey("xnode_gridcolor0");
- if (EditorPrefs.HasKey("xnode_gridcolor1")) EditorPrefs.DeleteKey("xnode_gridcolor1");
- LoadPrefs();
+ if (EditorPrefs.HasKey("xNode.Settings")) EditorPrefs.DeleteKey("xNode.Settings");
+
+ settings = LoadPrefs();
+ typeColors = new Dictionary();
+ _gridTexture = NodeEditorResources.GenerateGridTexture(settings.gridLineColor, settings.gridBgColor);
+ _crossTexture = NodeEditorResources.GenerateCrossTexture(settings.gridLineColor);
+ NodeEditorWindow.RepaintAll();
}
private static void SavePrefs() {
- EditorPrefs.SetString("xnode_gridcolor0", ColorUtility.ToHtmlStringRGB(_gridLineColor));
- EditorPrefs.SetString("xnode_gridcolor1", ColorUtility.ToHtmlStringRGB(_gridBgColor));
- EditorPrefs.SetBool("xnode_gridsnap", _gridSnap);
+ EditorPrefs.SetString("xNode.Settings", JsonUtility.ToJson(settings));
}
private static void VerifyLoaded() {
- if (!prefsLoaded) LoadPrefs();
+ if (settings == null) settings = LoadPrefs();
}
/// Return color based on type
@@ -154,7 +143,7 @@ namespace XNodeEditor {
if (type == null) return Color.gray;
string typeName = type.PrettyName();
if (!typeColors.ContainsKey(typeName)) {
- if (prefsTypeColors.ContainsKey(typeName)) typeColors.Add(typeName, prefsTypeColors[typeName]);
+ if (settings.typeColors.ContainsKey(typeName)) typeColors.Add(typeName, settings.typeColors[typeName]);
else {
#if UNITY_5_4_OR_NEWER
UnityEngine.Random.InitState(typeName.GetHashCode());