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 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
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
1
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
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

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

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

Need my function to work with different lists of different values (classes) 1 Answer

Creating a class wide array (c#) 1 Answer

Array of a custom class giving an error 1 Answer

How can I call a function of a class array from another function of a class 1 Answer

C# Array of Child Objects 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