@@ -24,6 +24,13 @@ public class SaveLoadTestUnityObject : ScriptableObject
2424 public Quaternion rot ;
2525 }
2626
27+ [ Serializable ]
28+ public class SaveLoadDictionaryTestObject
29+ {
30+ public Dictionary < string , int > dict = new Dictionary < string , int > ( ) ;
31+ public string name = "" ;
32+ }
33+
2734 private static readonly string BaseDirectory = "GameData" ;
2835 private static readonly string SaveDirectory = "SaveData" ;
2936 private static readonly string TestEncryptionKey = "SaveLoadTestEncryptionKey" ;
@@ -169,6 +176,49 @@ public void CanSaveLoadUnityObject([Values] SerializationMethodType method)
169176 Object . Destroy ( manager ) ;
170177 }
171178
179+ [ Test ]
180+ public void CanSaveAndLoadDictionary ( [ Values (
181+ SerializationMethodType . Binary ,
182+ SerializationMethodType . BinaryEncrypted
183+ #if JSON_DOT_NET
184+ , SerializationMethodType . JsonDotNet ,
185+ SerializationMethodType . JsonDotNetEncrypted
186+ #endif
187+ ) ] SerializationMethodType method )
188+ {
189+ var manager = CreateManager ( method ) ;
190+
191+ var testObject = new SaveLoadDictionaryTestObject ( )
192+ {
193+ dict = new Dictionary < string , int >
194+ {
195+ { "one" , 1 } ,
196+ { "two" , 2 }
197+ } ,
198+ name = "Test" ,
199+ } ;
200+
201+ const string filename = "Testfile" ;
202+
203+ manager . Save ( testObject , filename ) ;
204+
205+ var filepath = $ "{ SaveLoadUtility . GetSavePath ( SaveDirectory , BaseDirectory ) } /{ filename } ";
206+ Assert . IsTrue ( File . Exists ( filepath ) ) ;
207+
208+ var loadedObject = manager . Load < SaveLoadDictionaryTestObject > ( filename ) ;
209+
210+ Assert . IsTrue ( loadedObject . name == testObject . name ) ;
211+ Assert . IsTrue ( loadedObject . dict . Count == testObject . dict . Count ) ;
212+ Assert . IsTrue ( loadedObject . dict . ContainsKey ( "one" ) ) ;
213+ Assert . IsTrue ( loadedObject . dict . ContainsKey ( "two" ) ) ;
214+ Assert . IsTrue ( loadedObject . dict [ "one" ] == 1 ) ;
215+ Assert . IsTrue ( loadedObject . dict [ "two" ] == 2 ) ;
216+
217+ manager . DeleteSave ( filename ) ;
218+ Assert . IsFalse ( File . Exists ( filepath ) ) ;
219+
220+ Object . Destroy ( manager ) ;
221+ }
172222
173223 }
174224}
0 commit comments