Skip to content

Commit 187d22c

Browse files
author
Benoit Hudson
committed
UNI-22401: use json rather than yaml to serialize
We could save the yaml fine, but reloading it, it was creating some other class instead. Using json works reliably. If you have a YAML preference asset, you'll get an error and you'll lose your settings, but after that it'll all work.
1 parent 8506f1f commit 187d22c

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

Assets/FbxExporters/Editor/FbxExportSettings.cs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private string GetRelativePath(string fromDir, string toDir) {
117117
}
118118

119119
[FilePath("ProjectSettings/FbxExportSettings.asset",FilePathAttribute.Location.ProjectFolder)]
120-
public class ExportSettings : FbxExporters.EditorTools.ScriptableSingleton<ExportSettings>
120+
public class ExportSettings : ScriptableSingleton<ExportSettings>
121121
{
122122
public const string kDefaultSavePath = "Objects";
123123

@@ -183,71 +183,70 @@ public static T instance
183183
{
184184
get
185185
{
186-
if (ScriptableSingleton<T>.s_Instance == null)
186+
if (s_Instance == null)
187187
{
188-
return ScriptableSingleton<T>.CreateAndLoad();
188+
return CreateAndLoad();
189189
}
190-
return ScriptableSingleton<T>.s_Instance;
190+
return s_Instance;
191191
}
192192
}
193193

194194
protected ScriptableSingleton()
195195
{
196-
if (ScriptableSingleton<T>.s_Instance != null)
196+
if (s_Instance != null)
197197
{
198-
Debug.LogError("ScriptableSingleton already exists. Did you query the singleton in a constructor?");
198+
Debug.LogError(typeof(T) + " already exists. Did you query the singleton in a constructor?");
199199
}
200200
}
201+
201202
private static T CreateAndLoad()
202203
{
203-
string filePath = ScriptableSingleton<T>.GetFilePath();
204-
if (!string.IsNullOrEmpty(filePath))
204+
// First create.
205+
if (s_Instance == null)
205206
{
206-
var loaded = InternalEditorUtility.LoadSerializedFileAndForget(filePath);
207+
T t = ScriptableObject.CreateInstance<T>();
208+
s_Instance = t;
209+
}
207210

208-
if (loaded.Length > 0) {
209-
ScriptableSingleton<T>.s_Instance = loaded [0] as T;
211+
// Then load.
212+
string filePath = GetFilePath();
213+
if (System.IO.File.Exists(filePath)) {
214+
try {
215+
var fileData = System.IO.File.ReadAllText(filePath);
216+
EditorJsonUtility.FromJsonOverwrite(fileData, s_Instance);
217+
} catch(Exception xcp) {
218+
// Quash the exception and go on with the default settings.
219+
Debug.LogException(xcp);
210220
}
211221
}
212-
if (ScriptableSingleton<T>.s_Instance == null)
213-
{
214-
T t = ScriptableObject.CreateInstance<T>();
215-
ScriptableSingleton<T>.s_Instance = t;
216-
}
217-
return ScriptableSingleton<T>.s_Instance;
222+
return s_Instance;
218223
}
224+
219225
protected virtual void Save(bool saveAsText)
220226
{
221-
if (ScriptableSingleton<T>.s_Instance == null)
227+
if (s_Instance == null)
222228
{
223229
Debug.Log("Cannot save ScriptableSingleton: no instance!");
224230
return;
225231
}
226-
string filePath = ScriptableSingleton<T>.GetFilePath();
232+
string filePath = GetFilePath();
227233
if (!string.IsNullOrEmpty(filePath))
228234
{
229235
string directoryName = Path.GetDirectoryName(filePath);
230236
if (!Directory.Exists(directoryName))
231237
{
232238
Directory.CreateDirectory(directoryName);
233239
}
234-
InternalEditorUtility.SaveToSerializedFileAndForget(new T[]
235-
{
236-
ScriptableSingleton<T>.s_Instance
237-
}, filePath, saveAsText);
240+
System.IO.File.WriteAllText(filePath, EditorJsonUtility.ToJson(s_Instance, true));
238241
}
239242
}
243+
240244
private static string GetFilePath()
241245
{
242-
Type typeFromHandle = typeof(T);
243-
object[] customAttributes = typeFromHandle.GetCustomAttributes(true);
244-
object[] array = customAttributes;
245-
for (int i = 0; i < array.Length; i++)
246-
{
247-
object obj = array[i];
248-
if (obj is FilePathAttribute)
246+
foreach(var attr in typeof(T).GetCustomAttributes(true)) {
247+
FilePathAttribute filePathAttribute = attr as FilePathAttribute;
248+
if (filePathAttribute != null)
249249
{
250-
FilePathAttribute filePathAttribute = obj as FilePathAttribute;
251250
return filePathAttribute.filepath;
252251
}
253252
}

0 commit comments

Comments
 (0)