• 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
Question by dnoparker · Jun 26, 2015 at 11:54 PM · c#arrayclass

Writing an class array to disk?

 using UnityEngine;
 using System.Collections;
 
 public class ItemData : MonoBehaviour {
 
     void Start (){
 
     Item[] ItemDatabase = {
             new Item ("Baseball", "This a baseball", 5),
             new Item ("Ladder", "You can climb this", 5),
             new Item ("Radio", "Plays sounds", 5),
             new Item ("Book", "You can read this", 5) 
         };
 }
 
 
     class Item {
 
         public string _Name {get;set;}
         public string _Description {get;set;}
         public float _Value {get;set;}
         
         public Item (string Name, string Description, float Value)
         {
             _Name = Name; _Description = Description; _Value = Value;
         }
     }
 
 }

What is the easiest and most beginner friendly way to save and load that array to the disk locally?

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Cherno · Jun 27, 2015 at 12:16 AM

You can use a BinaryFormatter.

Create an instance of a simple class which acts as your "save game" and holds all data that will be saved.

 [System.Serializable]
 public class Game {
      public string savegameName = "New Save Game",
      public ItemData.Item[] itemArray
 
 }

create the instance (in this case, done in your own script as it uses the ItemDataBase variable):

 Game newSaveGame = new Game() {
      savegameName = "My Saved Game";
      itemArray = ItemDatabase;
 
 };
 

Call the Save function and pass it the Game instance:

 SaveLoad.Save(newSaveGame);

To load, call the Load function and pass it the name of the saved file, it will return a deserialized Game instance which you can use to access the itemArray variable etc. in it.

 Game loadedGame = SaveLoad.Load("My Saved Game");

The SaveLoad static class:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 using System.Runtime.Serialization;
 //I don't remember which of these are necessary and which are not
 
 public static class SaveLoad {
 
 
     //it's static so we can call it from anywhere
     public static void Save(Game saveGame) {
         
         BinaryFormatter bf = new BinaryFormatter();
 
         //Application.persistentDataPath is a string, so if you wanted you can put that into debug.log if you want to know where save games are located
         FileStream file = File.Create (Application.persistentDataPath + saveGame.savegameName + ".sav"); //you can call it anything you want
         bf.Serialize(file, saveGame);
         file.Close();
         Debug.Log("Saved Game: " + saveGame.savegameName);
 
     }    
     
     public static Game Load(string gameToLoad) {
         if(File.Exists(Application.persistentDataPath + gameToLoad + ".sav")) {
             BinaryFormatter bf = new BinaryFormatter();
 
             FileStream file = File.Open(Application.persistentDataPath + gameToLoad + ".sav", FileMode.Open);
             Game loadedGame = (Game)bf.Deserialize(file);
             file.Close();
             Debug.Log("Loaded Game: " + loadedGame.savegameName);
             return loadedGame;
         }
         else {
             return null;
         }
     }
 }

Comment
dnoparker

People who like this

1 Show 0 · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Need help setting an array of my class 0 Answers

Set default length for an array of elements of a custom class in inspector 0 Answers

Why is my C# array of objects fully populated when some of them should be null? 3 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