Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Kiloblargh · Nov 18, 2012 at 02:48 AM · arraysserializationsavelists

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.

Comment
Add comment · Show 8
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MountDoomTeam · Nov 18, 2012 at 07:05 AM 0
Share

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.

avatar image Kiloblargh · Nov 18, 2012 at 03:34 PM 0
Share

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[].

avatar image whydoidoit · Nov 18, 2012 at 03:38 PM 1
Share

See the article Saving Data #1 on Unity Gems

avatar image Kiloblargh · Nov 18, 2012 at 07:43 PM 0
Share

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." ?

avatar image whydoidoit · Nov 18, 2012 at 07:56 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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&lt;AssetFile&gt;();
         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;
 }

}

This method works very well in my custom EditorWindow, don't know if it works in any other situation.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image whydoidoit · Nov 19, 2012 at 08:41 AM 0
Share

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).

avatar image SONB · Nov 19, 2012 at 09:54 PM 0
Share

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.

avatar image whydoidoit · Nov 19, 2012 at 10:05 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges