@@ -117,7 +117,7 @@ private string GetRelativePath(string fromDir, string toDir) {
117
117
}
118
118
119
119
[ FilePath ( "ProjectSettings/FbxExportSettings.asset" , FilePathAttribute . Location . ProjectFolder ) ]
120
- public class ExportSettings : FbxExporters . EditorTools . ScriptableSingleton < ExportSettings >
120
+ public class ExportSettings : ScriptableSingleton < ExportSettings >
121
121
{
122
122
public const string kDefaultSavePath = "Objects" ;
123
123
@@ -183,71 +183,70 @@ public static T instance
183
183
{
184
184
get
185
185
{
186
- if ( ScriptableSingleton < T > . s_Instance == null )
186
+ if ( s_Instance == null )
187
187
{
188
- return ScriptableSingleton < T > . CreateAndLoad ( ) ;
188
+ return CreateAndLoad ( ) ;
189
189
}
190
- return ScriptableSingleton < T > . s_Instance ;
190
+ return s_Instance ;
191
191
}
192
192
}
193
193
194
194
protected ScriptableSingleton ( )
195
195
{
196
- if ( ScriptableSingleton < T > . s_Instance != null )
196
+ if ( s_Instance != null )
197
197
{
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?") ;
199
199
}
200
200
}
201
+
201
202
private static T CreateAndLoad ( )
202
203
{
203
- string filePath = ScriptableSingleton < T > . GetFilePath ( ) ;
204
- if ( ! string . IsNullOrEmpty ( filePath ) )
204
+ // First create.
205
+ if ( s_Instance == null )
205
206
{
206
- var loaded = InternalEditorUtility . LoadSerializedFileAndForget ( filePath ) ;
207
+ T t = ScriptableObject . CreateInstance < T > ( ) ;
208
+ s_Instance = t ;
209
+ }
207
210
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 ) ;
210
220
}
211
221
}
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 ;
218
223
}
224
+
219
225
protected virtual void Save ( bool saveAsText )
220
226
{
221
- if ( ScriptableSingleton < T > . s_Instance == null )
227
+ if ( s_Instance == null )
222
228
{
223
229
Debug . Log ( "Cannot save ScriptableSingleton: no instance!" ) ;
224
230
return ;
225
231
}
226
- string filePath = ScriptableSingleton < T > . GetFilePath ( ) ;
232
+ string filePath = GetFilePath ( ) ;
227
233
if ( ! string . IsNullOrEmpty ( filePath ) )
228
234
{
229
235
string directoryName = Path . GetDirectoryName ( filePath ) ;
230
236
if ( ! Directory . Exists ( directoryName ) )
231
237
{
232
238
Directory . CreateDirectory ( directoryName ) ;
233
239
}
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 ) ) ;
238
241
}
239
242
}
243
+
240
244
private static string GetFilePath ( )
241
245
{
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 )
249
249
{
250
- FilePathAttribute filePathAttribute = obj as FilePathAttribute ;
251
250
return filePathAttribute . filepath ;
252
251
}
253
252
}
0 commit comments