Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nockieboy · Sep 01, 2015 at 08:02 PM · serializationlistsclassessave-to-file

Save an array with custom classes

Okay, I've looked around but can't find anything that helps me out with this issue:

I have a list, containing several custom classes, identical in layout to the example below which is a snippet from one single file, OldSystem.cs :

 using UnityEngine;
 using UnityEngine.UI;
 using System;
 using System.Collections;
 using System.Collections.Generic;

 [Serializable]
 public enum DriveType {
     Floppy,
     HDD,
     SSD,
     Optical
 }
 
 [Serializable]
 public class File {
     public string fileName;
     public int fileSize;
     public List<string> contents = new List<string>();
 
     public File(string fName, int fSize)
     {
         fileName = fName;
         fileSize = fSize;
     }
 }
 
 [Serializable]
 public class Drive {
     public string letter;
     [SerializeField]
     public DriveType driveType;
     [SerializeField]
     public List<File> files = new List<File>();
 
     public Drive(string assignedLetter, DriveType type)
     {
         letter = assignedLetter;
         driveType = type;
     }
 }
 
 [RequireComponent(typeof(AudioSource))]
 public class OldSystem : MonoBehaviour {

     [SerializeField]
     public Drive[] drives;

     // Rest of main script code here
     ....
 
 }

I would like to be able to save the drives list from the OldSystem.cs MonoBehaviour somehow, as it is modified at runtime (either in the Editor or in the standalone build - but mostly the Editor at the moment) so that when I go back to the Editor, the changes are persistant.

As you can see, I've used a scattergun loaded with [Serializable] statements on the script, but to be honest I don't really understand what they do or how serialization works.

I would be more than happy if someone can point me in the direction of a script or tutorial or some guidance on how to save the drives list to an XML file or other type of file in the Assets folder, perhaps - so long as that approach would work with a standalone build as well.

Otherwise, any help or suggestions would be gratefully received. I'm certainly not a beginner at programming, but I'm very inexperienced at this particular sort of thing (serialization, getting Unity to save data etc.)

Have I even declared the DriveType enum, File class or Drive class in the right places?

Thanks! :D

Comment
Add comment
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

1 Reply

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

Answer by D3Duck · Sep 01, 2015 at 08:41 PM

First of all, the enum is declared outside a class, so it is now global - available everywhere. Usually its not a good practice though, since it could mess up code where you dont want it. If you put it inside a class then you can still reference it with the class name. The other declarations should definitely be fine.

As you said you are inexperienced with saving data, just check this out. It is quite lengthy but I watched it when first learning about saving and it is very good. You will then also see how easy it is to save an enum ;) (the save as saving everything else)

Just in case you really want to save as XML, then you can find tutorials on this as well, but definitely watch this first because I think it would be best.

Cheers and good luck.

Comment
Add comment · Show 1 · 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 nockieboy · Sep 03, 2015 at 12:09 PM 0
Share

Thanks very much for this, D3Duck. I had the time to sit down and watch the video you linked to this morning, and it was spot on. The first half about PlayerPrefs wasn't relevant, but once I'd seen how simple it is to use a FileStream and Binary Formatter to save/load data, I threw the functions together into an appropriate script in my project and it worked first time! I can now save/load custom classes!

Thanks again - you've saved me much head-scratching! :D

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

29 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 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 avatar image avatar image avatar image

Related Questions

Cleaning Up Classes In Lists 1 Answer

Saving data to export it later 0 Answers

Cannot serialize the list item? 2 Answers

How do I save; then load an array of Lists or a List of arrays (of ints)? 2 Answers

UNITYSCRIPT - Class's and Instances - Functions/Methods - Correct Usage 0 Answers


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