@@ -25,11 +25,11 @@ public static void Save<T>(string key, T dataToSave)
2525 if ( _data . TryGetValue ( key , out var data ) )
2626 {
2727 var item = data ;
28- item . Value = SerializationUtility . SerializeValue ( dataToSave , DATA_FORMAT , _serializationContext ) ;
28+ item . Value = Serialize ( dataToSave ) ;
2929 }
3030 else
3131 {
32- var saveItem = new Item { Value = SerializationUtility . SerializeValue ( dataToSave , DATA_FORMAT , _serializationContext ) } ;
32+ var saveItem = new Item { Value = Serialize ( dataToSave ) } ;
3333 _data . Add ( key , saveItem ) ;
3434 }
3535 }
@@ -39,7 +39,7 @@ public static T Load<T>(string key)
3939 _data . TryGetValue ( key , out var value ) ;
4040 var loadItem = value ;
4141
42- return SerializationUtility . DeserializeValue < T > ( loadItem . Value , DATA_FORMAT , _deserializationContext ) ;
42+ return Deserialize < T > ( loadItem . Value ) ;
4343 }
4444
4545 public static bool TryLoad < T > ( string key , out T data )
@@ -49,7 +49,7 @@ public static bool TryLoad<T>(string key, out T data)
4949 if ( _data . TryGetValue ( key , out var value ) )
5050 {
5151 var loadItem = value ;
52- data = SerializationUtility . DeserializeValue < T > ( loadItem . Value , DATA_FORMAT , _deserializationContext ) ;
52+ data = Deserialize < T > ( loadItem . Value ) ;
5353 hasKey = true ;
5454 }
5555 else
@@ -94,14 +94,22 @@ private static void Setup()
9494 GeneratePath ( ) ;
9595
9696 LoadFile ( ) ;
97- Application . quitting += SaveFile ;
97+ }
98+
99+ [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
100+ private static void CreateObserver ( )
101+ {
102+ var obj = new GameObject ( "ApplicationStateObserver" ) ;
103+ var observer = obj . AddComponent < ApplicationStateObserver > ( ) ;
104+ UnityEngine . Object . DontDestroyOnLoad ( obj ) ;
105+ observer . OnQuit += SaveFile ;
98106 }
99107
100108 private static void SaveFile ( )
101109 {
102110 FileSaving ? . Invoke ( ) ;
103111
104- var bytes = SerializationUtility . SerializeValue ( _data , DATA_FORMAT , _serializationContext ) ;
112+ var bytes = Serialize ( _data ) ;
105113 File . WriteAllBytes ( _savePath , bytes ) ;
106114 }
107115
@@ -113,15 +121,31 @@ private static void LoadFile()
113121 fileStream ? . Close ( ) ;
114122 }
115123
116- var loadBytes = File . ReadAllBytes ( _savePath ) ;
117- _data = SerializationUtility . DeserializeValue < Dictionary < string , Item > > ( loadBytes , DATA_FORMAT , _deserializationContext ) ;
124+ var bytes = File . ReadAllBytes ( _savePath ) ;
125+ _data = Deserialize < Dictionary < string , Item > > ( bytes ) ;
118126
119127 if ( _data == null )
120128 _data = new Dictionary < string , Item > ( INITIAL_SIZE ) ;
121129 }
122130
123131 private static void GeneratePath ( ) =>
124132 _savePath = Path . Combine ( Application . persistentDataPath , $ "{ FILE_NAME } _{ _currentProfileIndex } .data") ;
133+
134+ private static byte [ ] Serialize < T > ( T data )
135+ {
136+ var bytes = SerializationUtility . SerializeValue ( data , DATA_FORMAT , _serializationContext ) ;
137+ _serializationContext . ResetToDefault ( ) ;
138+
139+ return bytes ;
140+ }
141+
142+ private static T Deserialize < T > ( byte [ ] bytes )
143+ {
144+ var data = SerializationUtility . DeserializeValue < T > ( bytes , DATA_FORMAT , _deserializationContext ) ;
145+ _deserializationContext . Reset ( ) ;
146+
147+ return data ;
148+ }
125149 }
126150}
127151
0 commit comments