- Home /
How do I save; then load an array of Lists or a List of arrays (of ints)?
I have one of each, both large, and populated with values calculated in Awake() at great CPU expense. So I want to calculate them once, then look for and load them every time after that.
I can't find a clear or satisfactory answer either here or in the docs. I just know that they have to be serialized and while that seems like something I should be able to achieve in three lines of code or less, every time I try to read up on how to serialize things I find myself wanting to bang my head on the keyboard.
http://stackoverflow.com/questions/381508/can-a-byte-array-be-written-to-a-file-in-c
http://forum.unity3d.com/threads/144043-C-How-save-data-from-array-in-file-on-disk i would have written them as a text file probably. or saved a unity object with data written in childobjects.
Thanks, but I knew about File.WriteAllBytes- I just don't know how to turn a List.< int >[], a List.< int >[,], or a List.< int[] > into a byte[].
I read that and think I'm on the right track but now this: highScores = (List.)b.Deserialize(m); makes no sense to me. And apparently not to the compiler either because I get a ";" expected error at that line. I've never seen that syntax before of a parameter in parentheses -before- something... does it mean the same as "highScore = b.Deserialize(m) as List." ?
What language are you using? That's a cast - it's possible I copied the wrong script file when I posted the JavaScript. That's how you do it in C# :S Yes you can use as List.()
Will fix the article.
Answer by Steve Steven · Nov 18, 2012 at 10:35 PM
I suggest you to use ArrayPrefs, which is developed by the Volunteer Moderator Eric5h5.
The first version of this has the minimum requirements for saving and loading array, but the second version is perfectly developed for any types of array, and this two versions are also available in CSharp and UnityScript.
This is the link to ArrayPrefs version 1, and this is the link to ArrayPrefs version 2. Enjoy!
Answer by SONB · Nov 19, 2012 at 03:39 AM
I'm using a ScriptableObject to save my Lists.
First, you need a class, which holds your List<> and will be saved as an .asset file: using UnityEngine; using System.Collections; using System.Collections.Generic;
[System.Serializable] public class AssetFile : ScriptableObject { public List<int> mySavedList = new List<int>(); }
Then, in your working code, you need to load your .asset. If it doesn't exist, you need to create a new one: using UnityEngine; using UnityEditor; using System.Collections;
public class MyClass : EditorWindow { public AssetFile myAssetFile; ...
void OnEnable()
{
myAssetFile = (AssetFile)AssetDatabase.LoadAssetAtPath("MyLists.asset", typeof(AssetFile));
if (!myAssetFile)
{
myAssetFile = ScriptableObject.CreateInstance<AssetFile>();
AssetDatabase.CreateAsset(myAssetFile, "MyLists.asset");
}
myAssetFile.hideFlags = HideFlags.NotEditable;</code></pre>
Then you can create your Lists, 'link' them with your .asset file, change them etc... Important: After you made any change in you List, make sure you saved them in your file. For this just call EditorUtility.SetDirty(...):
// Create a List<>
List<int> workingList = new List<int>();
// Do something with the List and put it into your file...
myAssetFile.mySavedList = workingList;
// Now your workingList is referenced in your file
// And you can see your data in the Inspector if you select your asset file
// Now that you made changes to your List, save them! Or they are lost when you close Unity
EditorUtility.SetDirty(myAssetFile);
// You can load your saved List as well
workingList = myAssetFile.mySavedList;
}
}
That works really well when you want them to be assets. The benefit of using BinaryFormatter is that it just makes files and it can save much more complicated things - like Dictionaries too. The downside for the OP is that you cannot save structs that way - so you have to copy them into classes. It would be no problem for a list of ints.
Your solution won't work in game because it is using editor scripting and the asset database which has been converted into real resources by the time the game is running (outside the Editor).
Yes, you're right. I forgot to mention protobuf-net, which I use to save my data as binary files. This library is compatible with the whole .NET family, iOS and more and supports Dictionaries, Lists, single dimension arrays etc. The best for me is, it is extensible: you can add new data to your old file classes (in serialization it's called 'versioning' or something like that). This means, the users of my plugin ('Simple TODO') can save their data in version 1.0 and load it with a later version, which supports new additional data types (backwards compatibility). It's pretty simple to use.
Yep - my UnitySerializer does that - handles $$anonymous$$D arrays too. I used to use protobuf - but I found it a bit wordy and a lot of the need for non-reflective serializers can be worked around (and are much more maintainable I$$anonymous$$HO). Of course Unity Serializer's raison d'etres is to save GameObject trees and $$anonymous$$onoBehaviours but it also handles all of that other stuff for the custom properties on the scripts.
Your answer
Follow this Question
Related Questions
Serialization Error 1 Answer
Strategy for saving game 1 Answer
How do I save a custom class of variables to playerprefs? 2 Answers
Character Positions Not Serializing or Loading Properly? 1 Answer
SQLite and Lists in C# 1 Answer