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
1
Question by Gilles_aerts · Oct 17, 2015 at 11:34 PM · listplayerprefsdatabasedata storagedatabase handling

Saving Data (closed)

Hi ! Ik have a drinks class with 2 variables in it : string drinkName, float drinkPrice; Each value is filled in by InputFields and then gets saved in a Drinks list. All working so no problem there. However I wish to add drinks to this list without losing the progress each time I shut down my application.

I'm aware of the playerprefs method to save data but are there other ways to do so ? Perhaps better ways I could look into?

Thanks in advance

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

Answer by Halichoerus · Oct 18, 2015 at 09:13 AM

You can use data serialization and write/read your data to a file, I've recently added save/load for game options and progress and it was pretty easy. I would recommend this tutorial by unity, it explains the basics and gives you a working example you can recreate with little knowledge on the subject. Just skip ahead a bit to get past the player prefabs part.

Great thing about doing it this way is you just create your own data class which can contain any serializable data type, which is quite a few. Here's an edited snippet from my project, it's basically just the example from the tutorial I linked.

 using UnityEngine;
 using System.Collections;
 using System;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.IO;
 
 public class DataSaver : MonoBehaviour {
     public void SaveData() {
 
         MyData data = new Data();
         // add relevent data to my new data object e.g 
         data.exampleInt = 5;
 
         BinaryFormatter bf = new BinaryFormatter();
         FileStream file = File.Create(Application.persistentDataPath + "/SomeData");
         bf.Serialize(file, data);
         file.Close();
     }
 
     public void LoadData() {
         if (File.Exists(Application.persistentDataPath + "/SomeData")) {
             BinaryFormatter bf = new BinaryFormatter();
             FileStream file = File.Open(Application.persistentDataPath + "/PlayerOptions", FileMode.Open);
             OptionsData data = (OptionsData)bf.Deserialize(file);
             file.Close();
             // Do stuff with loaded data eg
             var someNewInt = data.exampleInt;
         }
     }
 }
 
 [Serializable]
 class MyData {
     // Any serializable data types you want to save/load e.g
     public int exampleInt;
 }


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

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

Related Questions

A node in a childnode? 1 Answer

Why PlayerPrefs not working although I use "HasKey" and Save() on android? 0 Answers

How to store 3D object(Furniture models) files (.fbx) and then instantiate one or more in runtime.? how to create database for it? 1 Answer

Scriptable Object's Data Gets Lost After Re-opening Unity!!! 1 Answer

problem updating info to data base 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