Skip to content

Commit 3e5a949

Browse files
committed
fix Dictionary not getting saved to json
Unity built in json doesn't handle Dictionary, so just use two lists instead to save both the name and the path.
1 parent 873665f commit 3e5a949

File tree

1 file changed

+49
-32
lines changed

1 file changed

+49
-32
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 49 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEditorInternal;
44
using UnityEngine;
55
using UnityEditor;
6+
using System.Collections.Generic;
67

78
namespace FbxExporters.EditorTools {
89

@@ -112,22 +113,20 @@ public override void OnInspectorGUI() {
112113
if (exportSettings.selectedMayaApp == options.Length - 1) {
113114
exportSettings.selectedMayaApp = 0;
114115
}
115-
int result = EditorGUILayout.Popup(exportSettings.selectedMayaApp, options);
116-
if (result == options.Length - 1) {
116+
int oldValue = exportSettings.selectedMayaApp;
117+
exportSettings.selectedMayaApp = EditorGUILayout.Popup(exportSettings.selectedMayaApp, options);
118+
if (exportSettings.selectedMayaApp == options.Length - 1) {
117119
string mayaPath = EditorUtility.OpenFilePanel ("Select Maya Application", ExportSettings.kDefaultAdskRoot, "exe");
118120

119121
// check that the path is valid and references the maya executable
120122
if (!string.IsNullOrEmpty (mayaPath) &&
121-
Path.GetFileNameWithoutExtension(mayaPath).ToLower().Equals("maya")
122-
) {
123+
Path.GetFileNameWithoutExtension (mayaPath).ToLower ().Equals ("maya")) {
123124
ExportSettings.AddMayaOption (mayaPath);
124125
Repaint ();
126+
} else {
127+
exportSettings.selectedMayaApp = oldValue;
125128
}
126-
} else {
127-
exportSettings.selectedMayaApp = result;
128129
}
129-
130-
131130
GUILayout.EndHorizontal ();
132131

133132
if (GUILayout.Button ("Install Maya Integration")) {
@@ -187,12 +186,12 @@ public class ExportSettings : ScriptableSingleton<ExportSettings>
187186
[SerializeField]
188187
string convertToModelSavePath;
189188

189+
// List of names in order that they appear in option list
190190
[SerializeField]
191+
private List<string> mayaOptionNames;
191192
// List of paths in order that they appear in the option list
192-
private System.Collections.Generic.List<string> mayaOptionPaths;
193193
[SerializeField]
194-
// Dictionary of path -> display name
195-
private System.Collections.Generic.Dictionary<string, string> mayaAppOptions;
194+
private List<string> mayaOptionPaths;
196195

197196
protected override void LoadDefaults()
198197
{
@@ -201,7 +200,8 @@ protected override void LoadDefaults()
201200
centerObjects = true;
202201
convertToModelSavePath = kDefaultSavePath;
203202
turntableScene = null;
204-
mayaAppOptions = null;
203+
mayaOptionPaths = null;
204+
mayaOptionNames = null;
205205
}
206206

207207
/// <summary>
@@ -210,7 +210,7 @@ protected override void LoadDefaults()
210210
/// <returns>The unique name.</returns>
211211
/// <param name="name">Name.</param>
212212
private static string GetUniqueName(string name){
213-
if (!instance.mayaAppOptions.ContainsValue (name)) {
213+
if (!instance.mayaOptionNames.Contains(name)) {
214214
return name;
215215
}
216216
var format = "{1} ({0})";
@@ -231,7 +231,7 @@ private static string GetUniqueName(string name){
231231
do {
232232
uniqueName = string.Format (format, index, name);
233233
index++;
234-
} while (File.Exists (uniqueName));
234+
} while (instance.mayaOptionNames.Contains(name));
235235

236236
return uniqueName;
237237
}
@@ -243,16 +243,17 @@ private static string GetUniqueName(string name){
243243
/// If MAYA_LOCATION is set, add this to the list as well.
244244
/// </summary>
245245
private static void FindMayaInstalls() {
246-
if (instance.mayaAppOptions == null) {
247-
instance.mayaAppOptions = new System.Collections.Generic.Dictionary<string, string> ();
248-
}
249-
var mayaAppOptions = instance.mayaAppOptions;
246+
instance.mayaOptionPaths = new List<string> ();
247+
instance.mayaOptionNames = new List<string> ();
248+
var mayaOptionName = instance.mayaOptionNames;
249+
var mayaOptionPath = instance.mayaOptionPaths;
250250

251251
// If the location is given by the environment, use it.
252252
var location = System.Environment.GetEnvironmentVariable ("MAYA_LOCATION");
253253
if (!string.IsNullOrEmpty(location)) {
254254
location = location.TrimEnd('/');
255-
mayaAppOptions.Add (GetMayaExePath(location.Replace("\\","/")), "MAYA_LOCATION");
255+
mayaOptionPath.Add (GetMayaExePath (location.Replace ("\\", "/")));
256+
mayaOptionName.Add ("MAYA_LOCATION");
256257
}
257258

258259
// List that directory and find the right version:
@@ -269,7 +270,8 @@ private static void FindMayaInstalls() {
269270
if (product.StartsWith("mayalt", StringComparison.InvariantCultureIgnoreCase)) {
270271
continue;
271272
}
272-
mayaAppOptions.Add (GetMayaExePath(productDir.FullName.Replace("\\","/")), GetUniqueName(product));
273+
mayaOptionPath.Add (GetMayaExePath (productDir.FullName.Replace ("\\", "/")));
274+
mayaOptionName.Add (GetUniqueName(product));
273275
}
274276
}
275277

@@ -300,17 +302,32 @@ private static string GetMayaExePath(string location)
300302
}
301303

302304
public static GUIContent[] GetMayaOptions(){
303-
if (instance.mayaAppOptions == null) {
305+
if (instance.mayaOptionNames == null ||
306+
instance.mayaOptionNames.Count != instance.mayaOptionPaths.Count ||
307+
instance.mayaOptionNames.Count == 0) {
304308
FindMayaInstalls ();
305-
instance.mayaOptionPaths = new System.Collections.Generic.List<string> ();
306-
foreach (var key in instance.mayaAppOptions.Keys) {
307-
instance.mayaOptionPaths.Add (key);
309+
}
310+
311+
// remove options that no longer exist
312+
List<int> toDelete = new List<int>();
313+
for(int i = 0; i < instance.mayaOptionPaths.Count; i++) {
314+
var mayaPath = instance.mayaOptionPaths [i];
315+
if (!File.Exists (mayaPath)) {
316+
if (i == instance.selectedMayaApp) {
317+
instance.selectedMayaApp = 0;
318+
}
319+
instance.mayaOptionNames.RemoveAt (i);
320+
toDelete.Add (i);
308321
}
309322
}
310-
GUIContent[] optionArray = new GUIContent[instance.mayaAppOptions.Count+1];
323+
foreach (var index in toDelete) {
324+
instance.mayaOptionPaths.RemoveAt (index);
325+
}
326+
327+
GUIContent[] optionArray = new GUIContent[instance.mayaOptionPaths.Count+1];
311328
for(int i = 0; i < instance.mayaOptionPaths.Count; i++){
312329
optionArray [i] = new GUIContent(
313-
instance.mayaAppOptions [instance.mayaOptionPaths [i]],
330+
instance.mayaOptionNames[i],
314331
instance.mayaOptionPaths[i]
315332
);
316333
}
@@ -320,17 +337,17 @@ public static GUIContent[] GetMayaOptions(){
320337
}
321338

322339
public static void AddMayaOption(string newOption){
323-
var mayaAppOptions = instance.mayaAppOptions;
324-
if (mayaAppOptions.ContainsKey (newOption)) {
325-
instance.selectedMayaApp = instance.mayaOptionPaths.IndexOf (newOption);
340+
var mayaOptionPaths = instance.mayaOptionPaths;
341+
if (mayaOptionPaths.Contains(newOption)) {
342+
instance.selectedMayaApp = mayaOptionPaths.IndexOf (newOption);
326343
return;
327344
}
328345

329346
// get the version
330347
var version = AskMayaVersion(newOption);
331-
mayaAppOptions.Add (newOption, GetUniqueName("Maya"+version));
332-
instance.mayaOptionPaths.Add (newOption);
333-
instance.selectedMayaApp = instance.mayaOptionPaths.Count - 1;
348+
instance.mayaOptionNames.Add (GetUniqueName("Maya"+version));
349+
mayaOptionPaths.Add (newOption);
350+
instance.selectedMayaApp = mayaOptionPaths.Count - 1;
334351
}
335352

336353
/// <summary>

0 commit comments

Comments
 (0)