• 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 Brakespear · Jul 08, 2014 at 09:56 PM · c#javascriptloadingsavingclasses

C# Save/Load - Save entire class contents?

So, I'm trying to port a text adventure engine I originally wrote to run in browsers, using Javascript and jQuery, with a plugin thingy called jStorage for saving to the browser's local storage.

With the original, I was able to establish a series of objects without giving them any contents:

 var loc = {}
 var ctr = {}
 var dat = {}
 var str = {}

Then write each "location" in the game in neat little groups, like this:

 loc.INTRO1 = function(){}
 //Figure out if the player is entering area (set up location's descriptions etc), or performing action *from* area.
 
 ctr.INTRO1 = function(){}
 //Big fat switch for playerCommand, fired by loc.INTRO1 if player enters command *from* area.
 
 dat.INTRO1 = {}
 //Object containing all variables for area (state of items, characters, story events)
 
 str.INTRO1 = {}
 //Object containing all the strings used by this area, such as descriptions and dialogue.

Using this system, in conjunction with jStorage, I was able to save EVERY variable for EVERY area in the entire game, with this line:

 $.jStorage.set("storeDAT", dat);

Now, I just spent a while going over the Unity data persistence tutorial video, and recreated a (slightly modified) version of the C# script therein:

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class GameStorage : MonoBehaviour {
 
     public static GameStorage storage;
     public float health;
     public float experience;
     //Note to self: access by GameStorage.storage.health etc
 
     public void Save(){
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
         
         playerData data = new playerData();
         data.health = health;
         data.experience = experience;
         
         bf.Serialize(file, data);
         file.Close();
     }
 
     public void Load(){
         if(File.Exists(Application.persistentDataPath + "/playerInfo.dat")){
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
             playerData data = (playerData)bf.Deserialize(file);
             file.Close();
             health = data.health;
             experience = data.experience;
         }
     }
 
     [Serializable]
     class playerData {
         public float health;
         public float experience;
     }
 
 }

(I stripped out the DontDestroy stuff for the game object he uses, because my little text adventure is only going to need a single scene).

Now, I can see what this script does and how it does it... but I'm not really fluent in C# yet, and I'm trying to wrap my head around how I'd port/mimic my old system using this one.

So, after much rambling, my question is kinda simple:

Is it possible, using this sort of process, to save the contents of an entire class (instead of saving each variable one by one)?

Bonus/Alternate question: Does UnityScript support the kind of object usage shown above? Can I var up some empty objects, then add their contents, then save the overall object using that same C# function?

Any pointers you guys could give would be much appreciated (note: I'm aware of the existence of "Unity Serializer", but I'm a little reluctant to install such an extensive plugin just to save a pile of ints in a text adventure).

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 Landern · Jul 08, 2014 at 10:16 PM

Create PlayerData as its own class in PlayerData.cs. Use that class and instantiate an object, use members from that type throughout the project where appropriate. Serialize that object and save it out similarly to above but by reference to that PlayerData object.

Comment

People who like this

0 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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Save Player Health and stats 1 Answer

Save data from one scene to another? 0 Answers

Retrieving basic Strings and Ints from XML Files (JS) 0 Answers

Saving data to xml file (I have the loading down) 0 Answers

Saving & Loading the scene (or at least one array) via Javascript 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